package main

import (

	"fmt"

	"sort"
	"math/rand"
	"time"
	"bufio"
	"os"
)

func main() {

	rand.Seed(time.Now().UnixNano())
	w := bufio.NewWriter(os.Stdout)

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)
	//t = 1000

	//start := time.Now().UnixNano()

	for i := 0; i < t; i++ {

		var n, m int
		fmt.Fscan(r, &n, &m)
		//n = 300
		//m = 100000000

		x := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &x[j])
			//x[j] = 1 + rand.Intn(m - 1)
		}
		d := make([]string, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &d[j])
			/*v := rand.Intn(1)
			if v == 1 {
				d[j] = "R"
			} else {
				d[j] = "L"
			}*/

		}

		ans := solve(x, d, m)

		for i := 0 ; i < len(ans); i++ {
			fmt.Fprintf(w, "%d ", ans[i])
		}
		fmt.Fprintln(w, "\n")
	}
	w.Flush()

	//end := time.Now().UnixNano()
	//fmt.Println((end - start)/1000000)
}

type pair struct {
	v int
	i int
}

type ByV []pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func solve(x []int, d[]string, m int) []int64 {

	var pairs []pair

	for j := 0; j < len(x); j++ {
		pairs = append(pairs, pair{x[j], j})
	}

	sort.Sort(ByV(pairs))

	ans := make([]int64, len(x))
	for j := 0; j < len(ans); j++ {
		ans[j] = -1
	}

	var stack []int

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 0 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}
			}

			stack = stack[: len(stack) - 2]
		}
	}

	stack = []int{}

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 1 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}

			}

			stack = stack[: len(stack) - 2]
		}
	}

	return ans
}

package main

import (
	"bufio"
	. "fmt"
	. "os"
	"sort"
)

type robot struct {
	x, i  int
	right bool
}

func main() {
	in := bufio.NewReader(Stdin)
	out := bufio.NewWriter(Stdout)
	defer out.Flush()

	var T, n, m int
	var dir string
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		robots := [2][]robot{}
		pos := make([]int, n)
		for i := range pos {
			Fscan(in, &pos[i])
		}
		for i, x := range pos {
			Fscan(in, &dir)
			robots[x&1] = append(robots[x&1], robot{x, i, dir == "R"})
		}

		ans := make([]int, n)
		for _, a := range robots {
			sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })
			var l, r []robot
			for _, p := range a {
				if p.right {
					r = append(r, p)
				} else if len(r) == 0 {
					l = append(l, p)
				} else {
					q := r[len(r)-1]
					r = r[:len(r)-1]
					t := (p.x - q.x) / 2
					ans[p.i] = t
					ans[q.i] = t
				}
			}
			for len(l) > 1 {
				p, q := l[0], l[1]
				l = l[2:]
				t := (p.x + q.x) / 2
				ans[p.i] = t
				ans[q.i] = t
			}
			for len(r) > 1 {
				p, q := r[len(r)-1], r[len(r)-2]
				r = r[:len(r)-2]
				t := (2*m - p.x - q.x) / 2
				ans[p.i] = t
				ans[q.i] = t
			}
			if len(l) == 1 && len(r) == 1 {
				p, q := l[0], r[0]
				t := (2*m - q.x + p.x) / 2
				ans[p.i] = t
				ans[q.i] = t
			}
		}
		for _, t := range ans {
			if t == 0 {
				t = -1
			}
			Fprint(out, t, " ")
		}
		Fprintln(out)
	}
}

package main
import("bufio";."fmt";."os";"sort")

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	out:=bufio.NewWriter(Stdout)
	type car struct{x,i int;r bool}
	var T,n,m int;s:=""
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&m)
		a:=[2][]car{}
		b:=make([]int,n)
		for i:=range b{Fscan(in,&b[i])}
		for i,x:=range b{Fscan(in,&s);a[x&1]=append(a[x&1],car{x,i,s=="R"})}
		ans:=make([]int,n)
		for _,a:=range a{
			sort.Slice(a,func(i,j int)bool{return a[i].x<a[j].x})
			var l,r []car
			for _,p:=range a{
				if p.r{r=append(r,p)
				}else if len(r)==0{l=append(l,p)
				}else{
					q:=r[len(r)-1]
					r=r[:len(r)-1]
					t:=(p.x-q.x)/2
					ans[p.i]=t
					ans[q.i]=t
				}
			}
			for len(l)>1{
				p,q:=l[0],l[1]
				l=l[2:]
				t:=(p.x+q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			for len(r)>1{
				p,q:=r[len(r)-1],r[len(r)-2]
				r=r[:len(r)-2]
				t:=(2*m-p.x-q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			if len(l)+len(r)>1{
				p,q:=l[0],r[0]
				t:=(2*m-q.x+p.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
		}
		for _,v:=range ans{
			if v==0{v=-1}
			Fprint(out,v," ")
		}
		Fprintln(out)
	}
	out.Flush()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"strings"
)

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type pair struct {
		x, id int
		right bool
	}

	solve := func(Case int) {
		var n, m int
		var s string
		Fscan(in, &n, &m)
		a := [2][]pair{}
		tmp := make([]int, n)
		for i := range tmp {
			Fscan(in, &tmp[i])
		}
		for i, x := range tmp {
			Fscan(in, &s)
			a[x&1] = append(a[x&1], pair{x, i, s[0] == 'R'})
		}

		ans := make([]int, n)
		for i := range ans {
			ans[i] = -1
		}
		for _, a := range a {
			sort.Slice(a, func(i, j int) bool { return a[i].x < a[j].x })
			left := []pair{}
			right := []pair{}
			for _, p := range a {
				if p.right {
					right = append(right, p)
				} else if len(right) == 0 {
					left = append(left, p)
				} else {
					q := right[len(right)-1]
					right = right[:len(right)-1]
					t := (p.x - q.x) / 2
					ans[p.id] = t
					ans[q.id] = t
				}
			}
			for len(left) > 1 {
				p, q := left[0], left[1]
				left = left[2:]
				t := (p.x + q.x) / 2
				ans[p.id] = t
				ans[q.id] = t
			}
			for len(right) > 1 {
				p, q := right[len(right)-1], right[len(right)-2]
				right = right[:len(right)-2]
				t := (2*m - p.x - q.x) / 2
				ans[p.id] = t
				ans[q.id] = t
			}
			if len(left) == 1 && len(right) == 1 {
				p, q := left[0], right[0]
				t := (2*m - q.x + p.x) / 2
				ans[p.id] = t
				ans[q.id] = t
			}
		}
		for _, v := range ans {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

	T := 1
	Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

func main() { run(os.Stdin, os.Stdout) }

package main
import("bufio";."fmt";."os";"sort")

// github.com/EndlessCheng/codeforces-go
func main(){
	in:=bufio.NewReader(Stdin)
	out:=bufio.NewWriter(Stdout)
	type car struct{x,i int;r bool}
	var T,n,m int;s:=""
	for Fscan(in,&T);T>0;T--{
		Fscan(in,&n,&m)
		a:=[2][]car{}
		b:=make([]int,n)
		for i:=range b{Fscan(in,&b[i])}
		for i,x:=range b{Fscan(in,&s);a[x&1]=append(a[x&1],car{x,i,s=="R"})}
		ans:=make([]int,n)
		for _,a:=range a{
			sort.Slice(a,func(i,j int)bool{return a[i].x<a[j].x})
			var l,r []car
			for _,p:=range a{
				if p.r{r=append(r,p)
				}else if len(r)==0{l=append(l,p)
				}else{
					q:=r[len(r)-1]
					r=r[:len(r)-1]
					t:=(p.x-q.x)/2
					ans[p.i]=t
					ans[q.i]=t
				}
			}
			for len(l)>1{
				p,q:=l[0],l[1]
				l=l[2:]
				t:=(p.x+q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			for len(r)>1{
				p,q:=r[len(r)-1],r[len(r)-2]
				r=r[:len(r)-2]
				t:=(2*m-p.x-q.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
			if len(l)+len(r)>1{
				p,q:=l[0],r[0]
				t:=(2*m-q.x+p.x)/2
				ans[p.i]=t
				ans[q.i]=t
			}
		}
		for _,v:=range ans{
			if v==0{v=-1}
			Fprint(out,v,"")
		}
		Fprintln(out)
	}
	out.Flush()
}

package main

import (

	"fmt"

	"sort"
	"math/rand"
	"time"
	"bufio"
	"os"
)

func main() {

	rand.Seed(time.Now().UnixNano())
	w := bufio.NewWriter(os.Stdout)

	r := bufio.NewReader(os.Stdin)

	var t int

	fmt.Fscan(r, &t)
	//t = 1000

	start := time.Now().UnixNano()

	for i := 0; i < t; i++ {

		var n, m int
		fmt.Fscan(r, &n, &m)
		//n = 300
		//m = 100000000

		x := make([]int, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &x[j])
			//x[j] = 1 + rand.Intn(m - 1)
		}
		d := make([]string, n)

		for j := 0; j < n; j++ {
			fmt.Fscan(r, &d[j])
			/*v := rand.Intn(1)
			if v == 1 {
				d[j] = "R"
			} else {
				d[j] = "L"
			}*/

		}

		ans := solve(x, d, m)

		for i := 0 ; i < len(ans); i++ {
			fmt.Fprintf(w, "%d ", ans[i])
		}
		fmt.Fprintln(w, "\n")
	}
	w.Flush()

	end := time.Now().UnixNano()
	fmt.Println((end - start)/1000000)
}

type pair struct {
	v int
	i int
}

type ByV []pair

func (a ByV) Len() int           { return len(a) }
func (a ByV) Less(i, j int) bool { return a[i].v < a[j].v }
func (a ByV) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func solve(x []int, d[]string, m int) []int64 {

	var pairs []pair

	for j := 0; j < len(x); j++ {
		pairs = append(pairs, pair{x[j], j})
	}

	sort.Sort(ByV(pairs))

	ans := make([]int64, len(x))
	for j := 0; j < len(ans); j++ {
		ans[j] = -1
	}

	var stack []int

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 0 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}
			}

			stack = stack[: len(stack) - 2]
		}
	}

	stack = []int{}

	for i := 0 ; i < len(pairs); i++ {
		p := pairs[i]
		if p.v % 2 == 1 {
			if d[p.i] == "L" {
				if len(stack) != 0 {
					idx := stack[len(stack) - 1]
					stack = stack[ : len(stack) - 1]

					if d[idx] == "L" {
						t := (x[idx] + x[p.i])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					} else {
						t := (x[p.i] - x[idx])/2
						ans[p.i] = int64(t)
						ans[idx] = int64(t)
					}

				} else {
					stack = append(stack, p.i)
				}
			} else {
				stack = append(stack, p.i)
			}

		}
	}

	if len(stack) >= 2 {

		for len(stack) >= 2 {

			last := stack[len(stack) - 1]
			prev := stack[len(stack) - 2]

			if d[prev] == "R" {
				t := (m - x[last] + m - x[prev])/2
				ans[last] = int64(t)
				ans[prev] = int64(t)

			} else {

				if m - x[last] > x[prev] {

					d := (m - x[last]) - x[prev]

					t := (m - d)/2 + m - x[last]
					ans[last] = int64(t)
					ans[prev] = int64(t)
				} else {

					t := x[prev] + (m + (m - x[last]) - x[prev])/2
					ans[last] = int64(t)
					ans[prev] = int64(t)
				}

			}

			stack = stack[: len(stack) - 2]
		}
	}

	return ans
}


package main

import (
	"bufio"
	. "fmt"
	"os"
	"sort"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	abs := func(x int64) int64 {
		if x < 0 {
			return -x
		}
		return x
	}

	n, ans := 0, int64(1e18)
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Ints(a)
o:
	for c := int64(1); ; c++ {
		var s, p int64 = 0, 1
		for _, v := range a {
			if p > 1e10 {
				break o
			}
			s += abs(int64(v) - p)
			p *= c
		}
		if s < ans {
			ans = s
		}
	}
	Print(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }

const inf int64 = 100000000000000000

func add(a, b int64) int64 {
	if a+b > inf {
		return inf
	}
	return a + b
}

func mul(a, b int64) int64 {
	if inf/a > b {
		return a * b
	}
	return inf
}

func abs(x int64) int64 {
	if x < 0 {
		return -x
	}
	return x
}

func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func main() {
	defer writer.Flush()
	var n int
	scanf("%d\n", &n)
	// fmt.Println(n)
	a := make([]int, n)
	for i := range a {
		scanf("%d ", &a[i])
	}
	sort.Ints(a)

	if n < 3 {
		printf("%d\n", a[0] - 1)
	} else {
		var minCost int64 = 0
		for _, v := range a {
			minCost += int64(v)
		}
		minCost -= int64(n)
		var x int64 = 0
		for x = 1; ; x++ {
			var cost, powCur int64 = 0, 1
			for i := range a {
				cost = add(cost, abs(int64(a[i])-powCur))
				powCur = mul(powCur, x)
			}
			if powCur >= inf || powCur/x > minCost+int64(a[n-1]) {
				break
			}
			minCost = min(minCost, cost)
		}
		printf("%d\n", minCost)
	}
}

/*
URL:
https://codeforces.com/contest/1397/problem/B
*/

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

var (
	n int
	A []int64
)

func main() {
	n = readi()
	A = readlls(n)

	sort.Slice(A, func(i, j int) bool {
		return A[i] < A[j]
	})
	// PrintfDebug("%v\n", A)

	base := C1()

	if n > 60 {
		fmt.Println(base)
		return
	}

	ans := base
	for c := int64(2); c <= 100000; c++ {
		tmp := calc(c)
		ChMin(&ans, tmp)
	}
	fmt.Println(ans)
}

func calc(a int64) int64 {
	res := int64(0)

	cur := int64(1)
	res += AbsInt(A[0] - cur)

	// F := Kiriage(INF_BIT60, a)

	for i := 1; i <= n-1; i++ {
		// if cur > F {
		// 	return INF_BIT60
		// }
		if !IsProductLeq(cur, a, INF_BIT60) {
			return INF_BIT60
		}

		cur *= a
		res += AbsInt(A[i] - cur)
	}

	return res
}

// IsProductLeq returns whether a*b <= ub or not.
// IsProductLeq panics when it accepts negative integers.
func IsProductLeq(a, b, ub int64) bool {
	if a < 0 || b < 0 || ub < 0 {
		panic("IsProductLeq does not accept negative integers")
	}

	return a <= ub/b
}

// IsSumLeq returns whether a*b <= ub or not.
// IsSumLeq panics when it accepts negative integers.
func IsSumLeq(a, b, ub int64) bool {
	if a < 0 || b < 0 || ub < 0 {
		panic("IsSumLeq does not accept negative integers")
	}

	return a <= ub-b
}

func C1() int64 {
	ans := int64(0)
	for i := 0; i < n; i++ {
		ans += AbsInt(A[i] - 1)
	}
	return ans
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int64) int64 {
	return (a + (b - 1)) / b
}

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int64, target int64) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// AbsInt is integer version of math.Abs
func AbsInt(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

/*******************************************************************/

/********** common constants **********/

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

// modint can calculate a right residual whether value is positive or negative.
func modint(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// modll can calculate a right residual whether value is positive or negative.
func modll(val, m int64) int64 {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	reads = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  func() string
	stdout *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

func _readInt64() int64 {
	i, err := strconv.ParseInt(reads(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// readis returns an integer slice that has n integers.
func readis(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readi()
	}
	return b
}

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	return b
}

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

func _readFloat64() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func readfs(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = readf()
	}
	return b
}

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*
URL:
https://codeforces.com/contest/1397/problem/B
*/

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"sort"
	"strconv"
)

var (
	n int
	A []int64
)

func main() {
	n = readi()
	A = readlls(n)

	sort.Slice(A, func(i, j int) bool {
		return A[i] < A[j]
	})
	// PrintfDebug("%v\n", A)

	base := C1()

	if n > 60 {
		fmt.Println(base)
		return
	}

	ans := base
	for c := int64(2); c <= 100000; c++ {
		tmp := calc(c)
		ChMin(&ans, tmp)
	}
	fmt.Println(ans)
}

func calc(a int64) int64 {
	res := int64(0)

	cur := int64(1)
	res += AbsInt(A[0] - cur)

	F := Kiriage(INF_BIT60, a)

	for i := 1; i <= n-1; i++ {
		if cur > F {
			return INF_BIT60
		}

		cur *= a
		res += AbsInt(A[i] - cur)
	}

	return res
}

func C1() int64 {
	ans := int64(0)
	for i := 0; i < n; i++ {
		ans += AbsInt(A[i] - 1)
	}
	return ans
}

// Kiriage returns Ceil(a/b)
// a >= 0, b > 0
func Kiriage(a, b int64) int64 {
	return (a + (b - 1)) / b
}

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int64, target int64) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// AbsInt is integer version of math.Abs
func AbsInt(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

/*******************************************************************/

/********** common constants **********/

const (
	// General purpose
	MOD = 1000000000 + 7
	// MOD          = 998244353
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

// modint can calculate a right residual whether value is positive or negative.
func modint(val, m int) int {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

// modll can calculate a right residual whether value is positive or negative.
func modll(val, m int64) int64 {
	res := val % m
	if res < 0 {
		res += m
	}
	return res
}

/********** bufio setting **********/

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	reads = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := reads()
//i := readi()
//X := readis(n)
//S := readrs()
//a := readf()
//A := readfs(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*********** Input ***********/

var (
	// reads returns a WORD string.
	reads  func() string
	stdout *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// readi returns an integer.
func readi() int {
	return int(_readInt64())
}
func readi2() (int, int) {
	return int(_readInt64()), int(_readInt64())
}
func readi3() (int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64())
}
func readi4() (int, int, int, int) {
	return int(_readInt64()), int(_readInt64()), int(_readInt64()), int(_readInt64())
}

// readll returns as integer as int64.
func readll() int64 {
	return _readInt64()
}
func readll2() (int64, int64) {
	return _readInt64(), _readInt64()
}
func readll3() (int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64()
}
func readll4() (int64, int64, int64, int64) {
	return _readInt64(), _readInt64(), _readInt64(), _readInt64()
}

func _readInt64() int64 {
	i, err := strconv.ParseInt(reads(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// readis returns an integer slice that has n integers.
func readis(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readi()
	}
	return b
}

// readlls returns as int64 slice that has n integers.
func readlls(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = readll()
	}
	return b
}

// readf returns an float64.
func readf() float64 {
	return float64(_readFloat64())
}

func _readFloat64() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func readfs(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = readf()
	}
	return b
}

// readrs returns a rune slice.
func readrs() []rune {
	return []rune(reads())
}

/*********** Output ***********/

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

/*********** Debugging ***********/

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	n := in()
	a := make([]float64, n)

	for i := range a {
		a[i] = float64(in())
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	c := math.Ceil(math.Pow(a[n-1], 1.0/float64(n-1)))

	var minRes float64 = math.MaxFloat64

	for j := c; j >= 1; j-- {
		var res float64 = 0

		var val float64 = 1
		for i := range a {
			res += math.Abs(a[i]-val)
			if val > math.MaxFloat64/j {
				res =  math.MaxFloat64
				break
			}
			val *= j
		}

		if res <= minRes {
			minRes = res
		} else {
			break
		}
	}

	fmt.Println(uint64(minRes))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]uint64, n)

	for i := range a {
		a[i] = in()
	}

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	// var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1))) + 1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 1; ; i++ {
		var ccost uint64 = 0
		var val uint64 = 1

		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		} else {
			break
		}
	}

	fmt.Println(prevCost)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() int {
		sc.Scan()
		i, _ := strconv.Atoi(sc.Text())
		return i
	}

	n := in()
	a := make([]float64, n)

	for i := range a {
		a[i] = float64(in())
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	c := math.Ceil(math.Pow(a[n-1], 1.0/float64(n-1)))

	var minRes float64 = math.MaxFloat64

	for j := c; j >= 1; j-- {
		var res float64 = 0

		var val float64 = 1
		for i := range a {
			res += math.Abs(a[i]-val)
			val *= j
		}

		if res < minRes {
			minRes = res
		} else {
			break
		}
	}

	fmt.Println(uint64(minRes))
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]uint64, n)

	for i := range a {
		a[i] = in()
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	uval := math.Pow(float64(a[n-1]), 1.0/float64(n-1))
	val := uint64(math.Ceil(uval))

	var cost uint64 = 0
	var cost2 uint64 = 0

	var p uint64 = 1

	for i := range a {
		cost += abs(a[i], p)
		p *= val
	}

	p = 1

	for i := range a {
		cost2 += abs(a[i], p)
		p *= (val - 1)
	}

	if cost < cost2 {
		fmt.Println(cost)
	} else {
		fmt.Println(cost2)
	}

}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]uint64, n)

	for i := range a {
		a[i] = in()
	}

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1)))+1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 1; i < e; i++ {
		var ccost uint64 = 0
		var val uint64 = 1
		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		}
	}

	fmt.Println(prevCost)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]uint64, n)

	for i := range a {
		a[i] = in()
	}

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	// var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1))) + 1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 0; ; i++ {
		var ccost uint64 = 0
		var val uint64 = 1

		if  i == 0 {
			val--
		}

		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		} else {
			break
		}
	}

	fmt.Println(prevCost)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func calc(i, n uint64) uint64 {
	if i == 1 {
		return n
	}

	val := uint64(math.Pow(float64(i), float64(n)))
	val--
	val /= (i - 1)

	return val
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]uint64, n)

	for i := range a {
		a[i] = in()
	}

	var sum uint64 = 0

	for i := range a {
		sum += a[i]
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	var e uint64 = uint64(math.Pow(float64(sum), 1/float64(n-1))) + 1

	var prevCost, i uint64 = math.MaxUint64, 0
	for i = 1; i <= e; i++ {
		var ccost uint64 = 0
		var val uint64 = 1

		for j := range a {
			ccost += abs(a[j], val)
			val *= i
		}

		if ccost < prevCost {
			prevCost = ccost
		}
	}

	fmt.Println(prevCost)
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
)

func abs(i, j uint64) uint64 {
	if i < j {
		return j - i
	}

	return i - j
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	bf := make([]byte, 1024*1024)
	sc.Buffer(bf, 10*1024*1024)
	sc.Split(bufio.ScanWords)

	in := func() uint64 {
		sc.Scan()
		i, _ := strconv.ParseUint(sc.Text(), 10, 64)
		return i
	}

	n := in()
	a := make([]uint64, n)

	for i := range a {
		a[i] = in()
	}

	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})

	uval := math.Pow(float64(a[n-1]), 1.0/float64(n-1))
	val := uint64(math.Ceil(uval))

	var cost uint64 = 0
	var p uint64 = 1
	for i := range a {
		cost += abs(a[i], p)
		p *= val
	}

	fmt.Println(cost)

}


package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	for iCase := 0; iCase < nCases; iCase++ {
		n, k := 0, 0
		fmt.Fscan(bufIO, &n, &k)

		numbs := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(bufIO, &(numbs[i]))
		}

		for i := range numbs {
			if numbs[i]%k == 0 {
				numbs[i] = 0
			} else {
				numbs[i] = k - (numbs[i] % k)
			}
		}

		sort.Ints(numbs)
		//fmt.Println(numbs)
		firstI, first, count := 0, numbs[0], 0
		for i, numb := range numbs {
			if numb == 0 {
				continue
			} else {
				first, firstI = numb, i
				break
			}
		}
		if first == 0 {
			fmt.Println(0)
			continue
		}
		maxElem, maxCount := 0, 0
		for i := firstI + 1; i < len(numbs); i++ {
			numb := numbs[i]

			if numb == first {
				count++
				continue
			} else {
				if count >= maxCount {
					maxElem = first
					maxCount = count
				}

				first = numb
				count = 0
			}
		}
		if maxCount <= count {
			maxCount = count
			maxElem = first
		}
		ans := int64(maxElem) + int64(maxCount)*int64(k)

		fmt.Println(ans + 1)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

type data struct{ n, s int64 }
type mySlice []data

func (p mySlice) Len() int           { return len(p) }
func (p mySlice) Less(i, j int) bool { return p[i].n < p[j].n }
func (p mySlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

func main() {
	reader := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscanf(reader, "%d\n", &t)

	var n int
	var k int64

	for ; t > 0; t-- {
		fmt.Fscanf(reader, "%d %d\n", &n, &k)
		var temp int64
		res := make(map[int64]int, n)
		for i := 1; i <= n; i++ {
			if i != n {
				fmt.Fscanf(reader, "%d ", &temp)
			} else {
				fmt.Fscanf(reader, "%d\n", &temp)
			}

			temp = temp % k
			if temp > 0 {
				res[k-temp]++
			}
		}

		mysort := make(mySlice, 0, len(res))
		for key, value := range res {
			mysort = append(mysort, data{n: key, s: int64(value)})
		}
		sort.Sort(mysort)

		maxCrc := int64(0)
		maxNum := int64(0)

		for i := 0; i < len(mysort); i++ {
			if mysort[i].s >= maxCrc {
				maxCrc = mysort[i].s
				maxNum = mysort[i].n
			}
		}

		if maxCrc == 0 && maxNum == 0 {
			fmt.Println(0)
		} else {
			fmt.Println(maxNum + 1 + (maxCrc-1)*k)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

func printf(f string, a ...interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	t := sint64()
	for ; t > 0; t-- {
		n := sint64()
		k := sint64()
		a := make([]int64, n)
		for i := range a {
			a[i] = sint64()
		}
		printf("%d\n", sol(a, k))
	}
}

func sol(a []int64, k int64) int64 {
	counts := map[int64]int64{}
	for i := range a {
		if a[i]%k == 0 {
			continue
		}
		toAdd := (k * ((a[i] / k) + 1)) - a[i]
		counts[toAdd]++
	}
	ret := int64(0)
	for i := range counts {
		ret = max(ret, k*(counts[i]-1)+int64(i))
	}
	if ret == 0 {
		return 0
	}
	return ret + 1
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 1024*1024)
	scanner.Buffer(buffer, 1024*1024)
	scanner.Split(bufio.ScanWords)

	input := func() uint64 {
		scanner.Scan()
		i, _ := strconv.ParseUint(scanner.Text(), 10, 64)
		return i
	}

	var t, n, k, x uint64

	t = input()

	for t > 0 {
		t--

		n = input()
		k = input()

		rem := make(map[uint64]uint64)

		var i uint64
		for i = 0; i < n; i++ {
			x = input()
			rem[x%k]++
		}

		var max uint64 = 0

		for r, c := range rem {

			if r == 0 {
				continue
			}

			val := k*(c-1) + (k + 1 - r)
			if max < val {
				max = val
			}
		}

		fmt.Println(max)

	}
}

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
)

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var t int
	var n, k, x int
	var num map[int]int
	var res int64
	scanf("%d", &t)

	for ; t > 0; t-- {
		scanf("\n%d %d\n", &n, &k)
		num = make(map[int]int)
		res = -1
		for ; n > 0; n-- {
			scanf("%d", &x)
			x = k - x%k
			if x != k {
				res = max(res, int64(num[x])*int64(k)+int64(x))
				num[x]++
			}
		}
		printf("%d\n", res+1)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		strs := strings.Split(string(line), " ")
		n, _ := strconv.ParseInt(strs[0], 10, 64)
		k, _ := strconv.ParseInt(strs[1], 10, 64)
		line = readLine(reader)
		ans := strings.Split(string(line), " ")
		m := make(map[int64]int)
		for i := int64(0); i < n; i++ {
			ai, _ := strconv.ParseInt(ans[i], 10, 64)
			left := ai % k
			if left == 0 {
				continue
			}
			left = k - left
			m[left] = m[left] + 1
		}
		max := int64(0)
		// fmt.Println(m)
		if len(m) == 0 {
			fmt.Println(0)
			continue
		}
		for key, val := range m {
			temp := key + int64(val - 1) * k
			if temp > max {
				max = temp
			}
		}
		fmt.Println(max + 1)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const MAX_N int = 2e5 + 10

var read *bufio.Reader = bufio.NewReader(os.Stdin)
var write *bufio.Writer = bufio.NewWriter(os.Stdout)
var num [MAX_N]int
var cnt map[int]int

func main() {
	defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(read, &n, &k)
		cnt = make(map[int]int, 0)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &num[i])
			if num[i] % k != 0 {
				cnt[k - num[i] % k]++
			}
		}
		var ans int = 0
		cnt[0] = 0
		for key, value := range cnt {
			if value > cnt[ans] || (value == cnt[ans] && key > ans) {
				ans = key
			}
		}
		//fmt.Fprintln(write, cnt)
		if ans == 0 {
			fmt.Fprintln(write, 0)
		} else {
			fmt.Fprintln(write, int64(cnt[ans] - 1) * int64(k) + int64(ans) + 1)
		}
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var nTest, n int
var k int64
var a [200001]int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &k)
		cnt := map[int64]int{}
		mx := int64(0)
		vmx := int64(0)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i] %= int64(k)
			if a[i] != 0 {
				a[i] = int64(k) - a[i]
			}

		}

		sort.Slice(a[:n], func(i, j int) bool {
			return a[i] < a[j]
		})

		// fmt.Println(a[:n])
		for i := 0; i < n; i++ {
			if a[i] != 0 {
				cnt[a[i]]++
			}

			if cnt[a[i]] >= int(mx) {
				mx = int64(cnt[a[i]])
				vmx = a[i]
			}
		}

		// fmt.Println(mx, vmx)
		var res int64
		if mx == 0 {
			res = 0
		} else {
			res = k*(mx-1) + vmx + 1
		}
		fmt.Fprintln(out, res)

	}

	out.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	tc := readNum(reader)

	for tc > 0 {
		tc--
		n, k := readTwoNums(reader)
		A := readNNums(reader, n)
		fmt.Println(solve(n, k, A))
	}
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n, k int, A []int) int64 {
	//change A to A[i] - k * A[i]/ k
	mem := make(map[int]int)

	for i := 0; i < n; i++ {
		if A[i]%k == 0 {
			continue
		}
		r := k - A[i]%k
		//A[i] needs at least r operation from 0
		mem[r]++
	}

	if len(mem) == 0 {
		return 0
	}
	K := int64(k)
	var res int64
	for r, v := range mem {
		res = max(res, int64(v-1)*K+int64(r))
	}

	return res + 1
}

func max(a, b int64) int64 {
	if a >= b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
)

func min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscanf(in, "%v\n", &t)
	for now := 0; now < t; now++ {
		var n, k int64
		fmt.Fscanf(in, "%v %v\n", &n, &k)
		mas := make([]int64, n)
		m := make(map[int64]int64)
		var i int64
		for ;i < n;i++ {
			fmt.Fscanf(in, "%v", &mas[i])
			m[(k - (mas[i] % k)) % k]++
		}
		fmt.Fscanf(in, "\n")
		var mx, idx int64
		for ind, i := range m {
			if ind == 0 {
				continue
			}
			if i > mx {
				idx = int64(ind)
				mx = i
			} else if (mx == i) && (ind > idx){
				idx = int64(ind)
				mx = i
			}
		}
		if idx == 0 {
			fmt.Fprintln(out, 0)
			continue
		}
		fmt.Fprintln(out, (mx - 1) * (k) + idx + 1)

	}
	return
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[a[i]]++
			}
		}

		ans := int64(0)
		for key, cnt := range mp {
			if int64(cnt) * int64(k) - int64(key) > ans {
				ans = int64(cnt) * int64(k) - int64(key)
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

const MAX_N int = 2e5 + 10

var read *bufio.Reader = bufio.NewReader(os.Stdin)
var write *bufio.Writer = bufio.NewWriter(os.Stdout)
var num [MAX_N]int
var cnt map[int]int

func main() {
	defer write.Flush()
	var t int
	fmt.Fscan(read, &t)
	for ; t > 0; t-- {
		var n, k int
		fmt.Fscan(read, &n, &k)
		cnt = make(map[int]int, 0)
		for i := 0; i < n; i++ {
			fmt.Fscan(read, &num[i])
			if num[i] % k != 0 {
				cnt[k - num[i] % k]++
			}
		}
		var ans int = 0
		cnt[0] = 0
		for key, value := range cnt {
			if value > cnt[ans] || (value == cnt[ans] && key > ans) {
				ans = key
			}
		}
		//fmt.Fprintln(write, cnt)
		if ans == 0 {
			fmt.Fprintln(write, 0)
		} else {
			fmt.Fprintln(write, (cnt[ans] - 1) * k + ans + 1)
		}
	}
}

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

// An IntHeap is a min-heap of ints.
type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	for iCase := 0; iCase < nCases; iCase++ {
		n, k := 0, 0
		fmt.Fscan(bufIO, &n, &k)

		numbs := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(bufIO, &(numbs[i]))
		}

		for i := range numbs {
			//fmt.Println(numbs[i])
			if numbs[i]%k == 0 {
				numbs[i] = 0
			} else {
				numbs[i] = k - (numbs[i] % k)
			}
		}
		numbHeap := IntHeap(numbs)
		heap.Init(&numbHeap)

		ans, lastTop := 0, -1
		for numbHeap.Len() > 0 {
			top := heap.Pop(&numbHeap).(int)
			if top == 0 {
				continue
			}
			if top+1 > ans {
				ans = top + 1
			}
			if top == lastTop {
				heap.Push(&numbHeap, top+k)
			}
			lastTop = top
		}
		fmt.Println(ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

func main() {
	nCases := 0
	bufIO := bufio.NewReader(os.Stdin)
	fmt.Scanln(&nCases)

	for iCase := 0; iCase < nCases; iCase++ {
		n, k := 0, 0
		fmt.Fscan(bufIO, &n, &k)

		numbs := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(bufIO, &(numbs[i]))
		}

		for i := range numbs {
			if numbs[i]%k == 0 {
				numbs[i] = 0
			} else {
				numbs[i] = k - (numbs[i] % k)
			}
		}

		sort.Ints(numbs)
		//fmt.Println(numbs)
		firstI, first, count := 0, numbs[0], 0
		for i, numb := range numbs {
			if numb == 0 {
				continue
			} else {
				first, firstI = numb, i
				break
			}
		}
		if first == 0 {
			fmt.Println(0)
			continue
		}
		maxElem, maxCount := 0, 0
		for i := firstI + 1; i < len(numbs); i++ {
			numb := numbs[i]

			if numb == first {
				count++
				continue
			} else {
				maxElem = first
				maxCount = count

				first = numb
				count = 0
			}
		}
		if maxCount <= count {
			maxCount = count
			maxElem = first
		}
		ans := int64(maxElem) + int64(maxCount)*int64(k)

		fmt.Println(ans + 1)
	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buffer := make([]byte, 1024*1024)
	scanner.Buffer(buffer, 1024*1024)
	scanner.Split(bufio.ScanWords)

	input := func() int {
		scanner.Scan()
		i, _ := strconv.Atoi(scanner.Text())
		return i
	}

	var t, n, k, x int

	t = input()

	for t > 0 {
		t--

		n = input()
		k = input()

		rem := make(map[int]int)

		for i := 0; i < n; i++ {
			x = input()
			rem[x%k]++
		}

		max := 0

		for r, c := range rem {

			if r == 0 {
				continue
			}

			val := k*(c-1) + (k + 1 - r)
			if max < val {
				max = val
			}
		}

		fmt.Println(max)

	}
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var nTest, n int
var k int64
var a [200001]int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &k)
		var cnt [200001]int
		mx := int64(0)
		vmx := int64(0)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i] %= int64(k)
			if a[i] != 0 {
				a[i] = int64(k) - a[i]
			}
			cnt[a[i]]++

			if cnt[a[i]] >= int(mx) {
				mx = int64(cnt[a[i]])

				if vmx < a[i] {
					vmx = a[i]
				}
			}
		}
		if vmx == 0 {
			fmt.Fprintln(out, 0)
			continue
		}
		res := k*(mx-1) + vmx + 1
		fmt.Fprintln(out, res)

	}

	out.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
)

var nTest, n int
var k int64
var a [200001]int64

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &nTest)
	for t := 0; t < nTest; t++ {
		fmt.Fscan(in, &n, &k)
		var cnt [200001]int
		mx := int64(0)
		vmx := int64(0)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			a[i] %= int64(k)
			if a[i] != 0 {
				a[i] = int64(k) - a[i]
			}

		}

		sort.Slice(a[:n], func(i, j int) bool {
			return a[i] < a[j]
		})

		for i := 0; i < n; i++ {
			cnt[a[i]]++

			if cnt[a[i]] >= int(mx) {
				mx = int64(cnt[a[i]])
				vmx = a[i]
			}
		}
		if vmx == 0 {
			fmt.Fprintln(out, 0)
			continue
		}

		res := k*(mx-1) + vmx + 1
		fmt.Fprintln(out, res)

	}

	out.Flush()
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[k-a[i]]++
			}
		}

		ans := 0
		exist := make(map[int]int)
		for key, cnt := range mp {
			cur := key
			for i := 0; i < cnt; i++ {
				for exist[cur] > 0 {
					cur += k
				}
				exist[cur]++
				if cur > ans {
					ans = cur
				}
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[a[i]]++
			}
		}

		ans := int64(0)
		for key, cnt := range mp {
			if int64(cnt * k) - int64(key) > ans {
				ans = int64(cnt * k) - int64(key)
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	t := NextInt()
	for t > 0 {
		t--
		n, k := NextInt(), NextInt()
		a := make([]int, n)

		mp := make(map[int]int)
		for i := 0; i < n; i++ {
			a[i] = NextInt()
			a[i] %= k
			if a[i] > 0 {
				mp[a[i]]++
			}
		}

		ans := 0
		for key, cnt := range mp {
			if cnt * k - key > ans {
				ans = cnt * k - key
			}
		}
		if ans > 0 {
			ans++
		}
		fmt.Println(ans)

	}
}

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Buffer(make([]byte, 256), 1e9)
	sc.Split(bufio.ScanWords)
}

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}


package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
					if inOffice == 0 {
						ans = append(ans, cur)
						cur = 0
						round++
					}
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		if inOffice != 0 {
			impossible = true
		}
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n int
var A []int

func main() {
	n = ReadInt()
	A = ReadIntSlice(n)

	if n%2 == 1 {
		fmt.Println(-1)
		return
	}

	// memo := [1000000+5]int{}
	memo := make(map[int]int)  // オフィス
	times := make(map[int]int) // ある一日の入場回数
	answers := []int{}
	count := 0
	for i := 0; i < n; i++ {
		a := A[i]

		if a < 0 {
			a = -a
			// 退出
			if _, ok := memo[a]; ok {
				delete(memo, a)
			} else {
				// 入場前退出のためアウト
				fmt.Println(-1)
				return
			}
		} else {
			// 入場
			if times[a] == 0 {
				memo[a] = 1
				times[a] = 1
			} else {
				// 一日に2回登場したのでアウト
				fmt.Println(-1)
				return
			}
		}

		count++
		// 空室判定
		if len(memo) == 0 {
			// 空室なので次の日へリセット
			answers = append(answers, count)
			count = 0
			memo = make(map[int]int)
			times = make(map[int]int)
		}
		// else {
		// 	// 継続
		// 	count++
		// }
	}

	if len(memo) != 0 {
		fmt.Println(-1)
		return
	}

	fmt.Println(len(answers))
	fmt.Println(PrintIntsLine(answers...))
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
				} else {
					status[a[i]] = round * 3 + 1
					cur++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		for _, v := range status {
			if v % 3 == 1 {
				impossible = true
			}
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
				} else {
					status[a[i]] = round * 3 + 1
					cur++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		if inOffice != 0 {
			impossible = true
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make(map[int]int) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		for _, v := range status {
			if v % 3 == 1 {
				impossible = true
			}
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

func minstep(a, b byte) int {
	return min(int(a-b+26)%26, int(b-a+26)%26)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	q := 1
	//fmt.Fscanf(in, "%d\n", &q)
	for ii := 0; ii < q; ii++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &a[i])
		}
		fmt.Fscanf(in, "\n")
		ans := []int{}
		cur := 0
		status := make([]int, 2000000) // 0 not 1 in 2 out
		round := 0
		impossible := false
		inOffice := 0
		for i := 0; i < n; i++ {
			if a[i] > 0 {
				if status[a[i]] == round * 3 + 1 {
					impossible = true
					break
				} else if status[a[i]] == round * 3 + 2 {
					if inOffice != 0 {
						impossible = true
						break
					}
					ans = append(ans, cur)
					cur = 1
					round++
					status[a[i]] = round * 3 + 1
					inOffice++
				} else {
					status[a[i]] = round * 3 + 1
					cur++
					inOffice++
				}
			} else if a[i] < 0 {
				if status[-a[i]] != round * 3 +1 {
					impossible = true
					break
				} else {
					status[-a[i]] = round * 3 + 2
					cur++
					inOffice--
				}
			}
			//fmt.Println(a[i], status, cur, ans)
		}
		if inOffice != 0 {
			impossible = true
		}
		ans = append(ans, cur)
		if impossible {
			fmt.Fprintf(out, "-1\n")
		} else {
			fmt.Fprintf(out, "%d\n", len(ans))
			for i := 0; i < len(ans)-1; i++ {
				fmt.Fprintf(out, "%d ", ans[i])
			}
			fmt.Fprintf(out, "%d\n", ans[len(ans)-1])
		}
	}
	return
}

package main

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

/*********** DP sub-functions ***********/

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

// ChMax accepts a pointer of integer and a target value.
// If target value is LARGER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMax(updatedValue *int, target int) bool {
	if *updatedValue < target {
		*updatedValue = target
		return true
	}
	return false
}

// NthBit returns nth bit value of an argument.
// n starts from 0.
func NthBit(num, nth int) int {
	return num >> uint(nth) & 1
}

// OnBit returns the integer that has nth ON bit.
// If an argument has nth ON bit, OnBit returns the argument.
func OnBit(num, nth int) int {
	return num | (1 << uint(nth))
}

// OffBit returns the integer that has nth OFF bit.
// If an argument has nth OFF bit, OffBit returns the argument.
func OffBit(num, nth int) int {
	return num & ^(1 << uint(nth))
}

// PopCount returns the number of ON bit of an argument.
func PopCount(num int) int {
	res := 0

	for i := 0; i < 70; i++ {
		if ((num >> uint(i)) & 1) == 1 {
			res++
		}
	}

	return res
}

/*********** Arithmetic ***********/

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// Sum returns multiple integers sum.
func Sum(integers ...int) int {
	s := 0

	for _, i := range integers {
		s += i
	}

	return s
}

// PowInt is integer version of math.Pow
// PowInt calculate a power by Binary Power (二分累乗法(O(log e))).
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}

	if e == 0 {
		return 1
	}

	if e%2 == 0 {
		halfE := e / 2
		half := PowInt(a, halfE)
		return half * half
	}

	return a * PowInt(a, e-1)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// Gcd returns the Greatest Common Divisor of two natural numbers.
// Gcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Gcd uses the Euclidean Algorithm.
func Gcd(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}
	if a < b {
		a, b = b, a
	}

	// Euclidean Algorithm
	for b > 0 {
		div := a % b
		a, b = b, div
	}

	return a
}

// Lcm returns the Least Common Multiple of two natural numbers.
// Lcd only accepts two natural numbers (a, b >= 1).
// 0 or negative number causes panic.
// Lcd uses the Euclidean Algorithm indirectly.
func Lcm(a, b int) int {
	if a <= 0 || b <= 0 {
		panic(errors.New("[argument error]: Gcd only accepts two NATURAL numbers"))
	}

	// a = a'*gcd, b = b'*gcd, a*b = a'*b'*gcd^2
	// a' and b' are relatively prime numbers
	// gcd consists of prime numbers, that are included in a and b
	gcd := Gcd(a, b)

	// not (a * b / gcd), because of reducing a probability of overflow
	return (a / gcd) * b
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		// str := strconv.FormatInt(A[i], 10)  // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60

var n int
var A []int

func main() {
	n = ReadInt()
	A = ReadIntSlice(n)

	if n%2 == 1 {
		fmt.Println(-1)
		return
	}

	// memo := [1000000+5]int{}
	memo := make(map[int]int)  // オフィス
	times := make(map[int]int) // ある一日の入場回数
	answers := []int{}
	count := 0
	for i := 0; i < n; i++ {
		a := A[i]

		if a < 0 {
			a = -a
			// 退出
			if _, ok := memo[a]; ok {
				delete(memo, a)
			} else {
				// 入場前退出のためアウト
				fmt.Println(-1)
				return
			}
		} else {
			// 入場
			if times[a] == 0 {
				memo[a] = 1
				times[a] = 1
			} else {
				// 一日に2回登場したのでアウト
				fmt.Println(-1)
				return
			}
		}

		count++
		// 空室判定
		if len(memo) == 0 {
			// 空室なので次の日へリセット
			answers = append(answers, count)
			count = 0
			memo = make(map[int]int)
			times = make(map[int]int)
		}
		// else {
		// 	// 継続
		// 	count++
		// }
	}

	fmt.Println(len(answers))
	fmt.Println(PrintIntsLine(answers...))
}

// MODはとったか？
// 遷移だけじゃなくて最後の最後でちゃんと取れよ？

/*******************************************************************/


package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1580A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]string, n)
		sum := make([][]int, n+1)
		sum[0] = make([]int, m+1)
		for i := range a {
			Fscan(in, &a[i])
			sum[i+1] = make([]int, m+1)
			for j, v := range a[i] {
				sum[i+1][j+1] = sum[i+1][j] + sum[i][j+1] - sum[i][j] + int(v&1)
			}
		}
		query := func(r1, c1, r2, c2 int) int {
			r2++
			c2++
			return sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1]
		}

		const minR, minC, inf = 5, 4, 99
		ans := inf
		for i := minR - 1; i < n; i++ {
			for j := i - minR + 1; j >= 0; j-- {
				mi := inf
				for k := minC - 1; k < m; k++ {
					mi = query(j+1, k-1, i-1, k-1) + int((a[j][k-1]&1^1)+(a[i][k-1]&1^1)) +
						min(mi, query(j+1, k-2, i-1, k-2)+int((a[j][k-2]&1^1)+(a[i][k-2]&1^1))+i-j-1-query(j+1, k-3, i-1, k-3))
					ans = min(ans, mi+i-j-1-query(j+1, k, i-1, k))
				}
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1580A(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) {
	suc, err := fmt.Fscanf(reader, f, a...)
	if err != nil {
		fmt.Println(suc, err)
	}
}

const maxn = 409

var sum [maxn][maxn]int
var f [maxn]int

func GetSum(lx, ly, rx, ry int) int {
	return sum[rx][ry] - sum[rx][ly-1] - sum[lx-1][ry] + sum[lx-1][ly-1]
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	//printf("%d\n", t)
	for ; t > 0; t-- {
		//for i := 0; i < maxn; i++ {
		//	f[i] = 0
		//	for j := 0; j < maxn; j++ {
		//		sum[i][j] = 0
		//	}
		//}
		var n, m int
		ans := 99
		scanf("%d %d\n", &n, &m)
		for i := 1; i <= n; i++ {
			var s string
			scanf("%s\n", &s)
			for k, c := range s {
				if '1' == c {
					sum[i][k+1] = 1
				} else {
					sum[i][k+1] = 0
				}
			}
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]
				//fmt.Printf("%d ", sum[i][j])
			}
			//fmt.Println()
		}
		for i := 1; i <= n; i++ {
			for j := i + 4; j <= n; j++ {
				for k := 4; k <= m; k++ {
					f[k] = GetSum(i+1, 1, j-1, k-1) - GetSum(i, 1, i, k-1) - GetSum(j, 1, j, k-1) - GetSum(i+1, k, j-1, k) + (k << 1) + j - i - 3
					//fmt.Printf("f[%d] = %d\n", k, f[k])
				}
				for k := m - 1; k != 3; k-- {
					if f[k+1] < f[k] {
						f[k] = f[k+1]
					}
				}
				for k := 1; k != m-2; k++ {
					cur := f[k+3] - GetSum(i+1, 1, j-1, k) + GetSum(i, 1, i, k) + GetSum(j, 1, j, k) - GetSum(i+1, k, j-1, k) - (k << 1) + j - i - 1
					//fmt.Printf("%d %d %d %d\n", GetSum(i+1, 1, j-1, k), GetSum(i, 1, i, k), GetSum(j, 1, j, k), GetSum(i+1, k, j-1, k))
					//fmt.Printf("%d %d %d\n", k, f[k+3], cur)

					if cur < ans {
						ans = cur
					}
				}
			}
		}
		printf("%d\n", ans)
		//fmt.Println(n, m)
		//for i := 1; i <= n; i++ {
		//	for j := 1; j <= m; j++ {
		//		fmt.Printf("%d ", sum[i][j])
		//	}
		//	fmt.Println()
		//}
	}
}

/*
2
5 4
1000
0000
0110
0000
0001
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

/*
1
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1580A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]string, n)
		sum := make([][]int, n+1)
		sum[0] = make([]int, m+1)
		for i := range a {
			Fscan(in, &a[i])
			sum[i+1] = make([]int, m+1)
			for j, v := range a[i] {
				sum[i+1][j+1] = sum[i+1][j] + sum[i][j+1] - sum[i][j] + int(v&1)
			}
		}
		query := func(r1, c1, r2, c2 int) int { r2++; c2++; return sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1] }

		const minR, minC, inf = 5, 4, 99
		ans := inf
		for i := minR - 1; i < n; i++ {
			for j := i - minR + 1; j >= 0; j-- {
				mi := inf
				for k := minC - 1; k < m; k++ {
					mi = query(j+1, k-1, i-1, k-1) + int((a[j][k-1]&1^1)+(a[i][k-1]&1^1)) + // 第 k-1 列
						min(mi, query(j+1, k-minC+2, i-1, k-2)+ // 第 k-2 至 k-minC+2 列的中间部分
							minC-3-query(j, k-minC+2, j, k-2)+ // 的第 j 行
							minC-3-query(i, k-minC+2, i, k-2)+ // 的第 i 行
							i-j-1-query(j+1, k-minC+1, i-1, k-minC+1)) // 第 k-minC+1 列（去掉两角）
					ans = min(ans, mi+i-j-1-query(j+1, k, i-1, k))     // 第 k 列（去掉两角）
				}
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1580A(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF1580A(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n, m int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n, &m)
		a := make([]string, n)
		sum := make([][]int, n+1)
		sum[0] = make([]int, m+1)
		for i := range a {
			Fscan(in, &a[i])
			sum[i+1] = make([]int, m+1)
			for j, v := range a[i] {
				sum[i+1][j+1] = sum[i+1][j] + sum[i][j+1] - sum[i][j] + int(v&1)
			}
		}
		query := func(r1, c1, r2, c2 int) int {
			r2++
			c2++
			return sum[r2][c2] - sum[r2][c1] - sum[r1][c2] + sum[r1][c1]
		}

		const minR, minC, inf = 5, 4, 99
		ans := inf
		for i := minR - 1; i < n; i++ {
			for j := i - minR + 1; j >= 0; j-- {
				mi := inf
				for k := minC - 1; k < m; k++ {
					mi = query(j+1, k-1, i-1, k-1) + int((a[j][k-1]&1^1)+(a[i][k-1]&1^1)) +
						min(mi, query(j+1, k-minC+2, i-1, k-2)+
							minC-3-query(j, k-minC+2, j, k-2)+
							minC-3-query(i, k-minC+2, i, k-2)+
							i-j-1-query(j+1, k-3, i-1, k-3))
					ans = min(ans, mi+i-j-1-query(j+1, k, i-1, k))
				}
			}
		}
		Fprintln(out, ans)
	}
}

func main() { CF1580A(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const maxn = 409

var sum [maxn][maxn]int
var f [maxn]int

func GetSum(lx, ly, rx, ry int) int {
	return sum[rx][ry] - sum[rx][ly-1] - sum[lx-1][ry] + sum[lx-1][ly-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	read_int := func() (x int) {
		c, _ := in.ReadByte()
		for ; c < '0'; c, _ = in.ReadByte() {
		}
		for ; c >= '0'; c, _ = in.ReadByte() {
			x = x*10 + int(c-'0')
		}
		return
	}
	t = read_int()
	for ; t > 0; t-- {
		for i := 0; i < maxn; i++ {
			f[i] = 0
			for j := 0; j < maxn; j++ {
				sum[i][j] = 0
			}
		}
		var n, m int
		ans := 99
		n = read_int()
		m = read_int()
		for i := 1; i <= n; i++ {
			s, _ := in.ReadString('\n')
			for k, c := range s {
				if '1' == c {
					sum[i][k+1] = 1
				} else {
					sum[i][k+1] = 0
				}
			}
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]
				fmt.Printf("%d ", sum[i][j])
			}
			fmt.Println()
		}
		for i := 1; i <= n; i++ {
			for j := i + 4; j <= n; j++ {
				for k := 4; k <= m; k++ {
					f[k] = GetSum(i+1, 1, j-1, k-1) - GetSum(i, 1, i, k-1) - GetSum(j, 1, j, k-1) - GetSum(i+1, k, j-1, k) + (k << 1) + j - i - 3
					fmt.Printf("f[%d] = %d\n", k, f[k])
				}
				for k := m - 1; k != 3; k-- {
					if f[k+1] < f[k] {
						f[k] = f[k+1]
					}
				}
				for k := 1; k != m-2; k++ {
					cur := f[k+3] - GetSum(i+1, 1, j-1, k) + GetSum(i, 1, i, k) + GetSum(j, 1, j, k) - GetSum(i+1, k, j-1, k) - (k << 1) + j - i - 1
					fmt.Printf("%d %d %d %d\n", GetSum(i+1, 1, j-1, k), GetSum(i, 1, i, k), GetSum(j, 1, j, k), GetSum(i+1, k, j-1, k))
					fmt.Printf("%d %d %d\n", k, f[k+3], cur)

					if cur < ans {
						ans = cur
					}
				}
			}
		}
		out.WriteString(strconv.Itoa(ans) + "\n")
		//fmt.Println(n, m)
		//for i := 1; i <= n; i++ {
		//	for j := 1; j <= m; j++ {
		//		fmt.Printf("%d ", sum[i][j])
		//	}
		//	fmt.Println()
		//}
	}
}

/*
2
5 4
1000
0000
0110
0000
0001

9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

/*
1
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

package main

import (
	"bufio"
	"os"
	"strconv"
)

const maxn = 409

var sum [maxn][maxn]int
var f [maxn]int

func GetSum(lx, ly, rx, ry int) int {
	return sum[rx][ry] - sum[rx][ly-1] - sum[lx-1][ry] + sum[lx-1][ly-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	read_int := func() (x int) {
		c, _ := in.ReadByte()
		for ; c < '0'; c, _ = in.ReadByte() {
		}
		for ; c >= '0'; c, _ = in.ReadByte() {
			x = x*10 + int(c-'0')
		}
		return
	}
	t = read_int()
	for ; t > 0; t-- {
		for i := 0; i < maxn; i++ {
			f[i] = 0
			for j := 0; j < maxn; j++ {
				sum[i][j] = 0
			}
		}
		var n, m int
		ans := 99
		n = read_int()
		m = read_int()
		for i := 1; i <= n; i++ {
			s, _ := in.ReadString('\n')
			for k, c := range s {
				if '1' == c {
					sum[i][k+1] = 1
				} else {
					sum[i][k+1] = 0
				}
			}
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]
				//fmt.Printf("%d ", sum[i][j])
			}
			//fmt.Println()
		}
		for i := 1; i <= n; i++ {
			for j := i + 4; j <= n; j++ {
				for k := 4; k <= m; k++ {
					f[k] = GetSum(i+1, 1, j-1, k-1) - GetSum(i, 1, i, k-1) - GetSum(j, 1, j, k-1) - GetSum(i+1, k, j-1, k) + (k << 1) + j - i - 3
					//fmt.Printf("f[%d] = %d\n", k, f[k])
				}
				for k := m - 1; k != 3; k-- {
					if f[k+1] < f[k] {
						f[k] = f[k+1]
					}
				}
				for k := 1; k != m-2; k++ {
					cur := f[k+3] - GetSum(i+1, 1, j-1, k) + GetSum(i, 1, i, k) + GetSum(j, 1, j, k) - GetSum(i+1, k, j-1, k) - (k << 1) + j - i - 1
					//fmt.Printf("%d %d %d %d\n", GetSum(i+1, 1, j-1, k), GetSum(i, 1, i, k), GetSum(j, 1, j, k), GetSum(i+1, k, j-1, k))
					//fmt.Printf("%d %d %d\n", k, f[k+3], cur)

					if cur < ans {
						ans = cur
					}
				}
			}
		}
		out.WriteString(strconv.Itoa(ans) + "\n")
		//fmt.Println(n, m)
		//for i := 1; i <= n; i++ {
		//	for j := 1; j <= m; j++ {
		//		fmt.Printf("%d ", sum[i][j])
		//	}
		//	fmt.Println()
		//}
	}
}

/*
2
5 4
1000
0000
0110
0000
0001

9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

/*
1
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

package main

import (
	"bufio"
	"os"
	"strconv"
)

const maxn = 409

var sum [maxn][maxn]int
var f [maxn]int

func GetSum(lx, ly, rx, ry int) int {
	return sum[rx][ry] - sum[rx][ly-1] - sum[lx-1][ry] + sum[lx-1][ly-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	read_int := func() (x int) {
		c, _ := in.ReadByte()
		for ; c < '0'; c, _ = in.ReadByte() {
		}
		for ; c >= '0'; c, _ = in.ReadByte() {
			x = x*10 + int(c-'0')
		}
		return
	}
	t = read_int()
	for ; t > 0; t-- {
		var n, m int
		ans := 999999
		n = read_int()
		m = read_int()
		for i := 1; i <= n; i++ {
			s, _ := in.ReadString('\n')
			for k, c := range s {
				if '1' == c {
					sum[i][k+1] = 1
				} else {
					sum[i][k+1] = 0
				}
			}
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]
				//fmt.Printf("%d ", sum[i][j])
			}
			//fmt.Println()
		}
		for i := 1; i <= n; i++ {
			for j := i + 4; j <= n; j++ {
				for k := 4; k <= m; k++ {
					f[k] = GetSum(i+1, 1, j-1, k-1) - GetSum(i, 1, i, k-1) - GetSum(j, 1, j, k-1) - GetSum(i+1, k, j-1, k) + (k << 1) + j - i - 3
					//fmt.Printf("f[%d] = %d\n", k, f[k])
				}
				for k := m - 1; k != 3; k-- {
					if f[k+1] < f[k] {
						f[k] = f[k+1]
					}
				}
				for k := 1; k != m-2; k++ {
					cur := f[k+3] - GetSum(i+1, 1, j-1, k) + GetSum(i, 1, i, k) + GetSum(j, 1, j, k) - GetSum(i+1, k, j-1, k) - (k << 1) + j - i - 1
					//fmt.Printf("%d %d %d %d\n", GetSum(i+1, 1, j-1, k), GetSum(i, 1, i, k), GetSum(j, 1, j, k), GetSum(i+1, k, j-1, k))
					//fmt.Printf("%d %d %d\n", k, f[k+3], cur)

					if cur < ans {
						ans = cur
					}
				}
			}
		}
		out.WriteString(strconv.Itoa(ans) + "\n")
		//fmt.Println(n, m)
		//for i := 1; i <= n; i++ {
		//	for j := 1; j <= m; j++ {
		//		fmt.Printf("%d ", sum[i][j])
		//	}
		//	fmt.Println()
		//}
	}
}

/*
2
5 4
1000
0000
0110
0000
0001

9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

/*
1
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

package main

import (
	"bufio"
	"os"
	"strconv"
)

const maxn = 409

var sum [maxn][maxn]int
var f [maxn]int

func GetSum(lx, ly, rx, ry int) int {
	return sum[rx][ry] - sum[rx][ly-1] - sum[lx-1][ry] + sum[lx-1][ly-1]
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	read_int := func() (x int) {
		c, _ := in.ReadByte()
		for ; c < '0'; c, _ = in.ReadByte() {
		}
		for ; c >= '0'; c, _ = in.ReadByte() {
			x = x*10 + int(c-'0')
		}
		return
	}
	t = read_int()
	for ; t > 0; t-- {
		var n, m int
		ans := 99
		n = read_int()
		m = read_int()
		for i := 1; i <= n; i++ {
			s, _ := in.ReadString('\n')
			for k, c := range s {
				if '1' == c {
					sum[i][k+1] = 1
				} else {
					sum[i][k+1] = 0
				}
			}
		}
		for i := 1; i <= n; i++ {
			for j := 1; j <= m; j++ {
				sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]
				//fmt.Printf("%d ", sum[i][j])
			}
			//fmt.Println()
		}
		for i := 1; i <= n; i++ {
			for j := i + 4; j <= n; j++ {
				for k := 4; k <= m; k++ {
					f[k] = GetSum(i+1, 1, j-1, k-1) - GetSum(i, 1, i, k-1) - GetSum(j, 1, j, k-1) - GetSum(i+1, k, j-1, k) + (k << 1) + j - i - 3
					//fmt.Printf("f[%d] = %d\n", k, f[k])
				}
				for k := m - 1; k != 3; k-- {
					if f[k+1] < f[k] {
						f[k] = f[k+1]
					}
				}
				for k := 1; k != m-2; k++ {
					cur := f[k+3] - GetSum(i+1, 1, j-1, k) + GetSum(i, 1, i, k) + GetSum(j, 1, j, k) - GetSum(i+1, k, j-1, k) - (k << 1) + j - i - 1
					//fmt.Printf("%d %d %d %d\n", GetSum(i+1, 1, j-1, k), GetSum(i, 1, i, k), GetSum(j, 1, j, k), GetSum(i+1, k, j-1, k))
					//fmt.Printf("%d %d %d\n", k, f[k+3], cur)

					if cur < ans {
						ans = cur
					}
				}
			}
		}
		out.WriteString(strconv.Itoa(ans) + "\n")
		//fmt.Println(n, m)
		//for i := 1; i <= n; i++ {
		//	for j := 1; j <= m; j++ {
		//		fmt.Printf("%d ", sum[i][j])
		//	}
		//	fmt.Println()
		//}
	}
}

/*
2
5 4
1000
0000
0110
0000
0001

9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/

/*
1
9 9
001010001
101110100
000010011
100000001
101010101
110001111
000001111
111100000
000110000

*/