ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.Chris tried filling in random numbers but it didn't work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal —  and the secondary diagonal — ) are equal. Chris doesn't know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid. n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive. It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead. If there are multiple solutions, you may print any of them.

NoteIn the first sample case, we can fill in 9 into the empty cell to make the resulting grid a magic square. Indeed, The sum of numbers in each row is:4 + 9 + 2 = 3 + 5 + 7 = 8 + 1 + 6 = 15.The sum of numbers in each column is:4 + 3 + 8 = 9 + 5 + 1 = 2 + 7 + 6 = 15.The sum of numbers in the two diagonals is:4 + 5 + 6 = 2 + 5 + 8 = 15.In the third sample case, it is impossible to fill a number in the empty square such that the resulting grid is a magic square.

package main

import "fmt"
import "bufio"
import "os"

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

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

	a := make([][]int64, n)
	var iz, jz int

	for i := 0; i < n; i++ {
		a[i] = make([]int64, n)
		for j := 0; j < n; j++ {
			fmt.Fscanf(buf, "%d\n", &a[i][j])
			if a[i][j] == 0 {
				iz = i
				jz = j
			}
		}
	}

	sum := int64(0)

	if n == 1 {
		a[iz][jz] = 1
		sum = 1
	} else {
		k := 0
		for k == iz {
			k++
		}
		sumz := int64(0)
		for j := 0; j < n; j++ {
			sum += a[k][j]
			sumz += a[iz][j]
		}
		a[iz][jz] = sum - sumz
	}

	found := (a[iz][jz] >= 1)

	sd1 := int64(0)
	sd2 := int64(0)

	for i := 0; i < n; i++ {
		srow := int64(0)
		scol := int64(0)
		for j := 0; j < n; j++ {
			srow += a[i][j]
			scol += a[j][i]
		}
		if (srow != sum) || (scol != sum) {
			found = false
			break
		}
		sd1 += a[i][i]
		sd2 += a[i][n-1-i]
	}

	if (sd1 != sum) || (sd2 != sum) {
		found = false
	}

	if found {
		fmt.Println(a[iz][jz])
	} else {
		fmt.Println(-1)
	}
}

package main

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

func main() {
	var n, tmp, fcolsum, xcolsum int64
	var zi, zj int64

	in := bufio.NewReaderSize(os.Stdin, 1024*1024)
	fmt.Fscan(in, &n)

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

	mass := make([][]int64, n)
	for i := int64(0); i < n; i++ {
		mass[i] = make([]int64, n)

		sum := int64(0)
		zero := false

		for j := int64(0); j < n; j++ {
			fmt.Fscan(in, &tmp)
			mass[i][j] = tmp

			if !zero {
				zero = (tmp == 0)

				if zero {
					zi, zj = i, j
				}
			}

			sum = sum + tmp
		}

		if zero {
			xcolsum = sum
		} else {
			if fcolsum == 0 {
				fcolsum = sum
			} else {
				if fcolsum != sum {
					fmt.Println(-1)
					return
				}
			}
		}
	}

	mass[zi][zj] = fcolsum - xcolsum
	if mass[zi][zj] <= 0 {
		fmt.Println(-1)
		return
	}

	// rows
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[j][i]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// diag 1 & diag 2
	sum := int64(0)
	sum2 := int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][i]
		sum2 = sum2 + mass[i][n - i - 1]
	}

	if fcolsum != sum || fcolsum != sum2 {
		fmt.Println(-1)
		return
	}

	fmt.Println(mass[zi][zj])
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	if n == 1 {
		var trash int
		if _, err := fmt.Scanf("%d\n", &trash); err != nil {
			return
		}
		fmt.Println(1)
		return
	}

	array := make([][]int64, n+1)
	array[n] = make([]int64, n+1)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	e := [2]int{-1, -1}
	diagonal := [2]int64{0, 0}
	target := int64(-1)
	met_zero := false

	for i := 0; i < n; i++ {
		array[i] = make([]int64, n+1)
		r_sum := int64(0)
		for j := 0; j < n && scanner.Scan(); j++ {
			in, err := strconv.Atoi(scanner.Text())
			if err != nil {
				return
			}
			a := int64(in)
			array[i][j] = a
			r_sum += a
			array[n][j] += a

			if i == j {
				diagonal[0] += a
			}
			if i+j == n-1 {
				diagonal[1] += a
			}
			if a == 0 {
				e[0] = i
				e[1] = j
				met_zero = true
			}
		}
		array[i][n] = r_sum
		if target < 0 && !met_zero {
			target = r_sum
		}
		if met_zero {
			met_zero = false
		}
	}

	magic := target - array[n][e[1]]
	if target != array[e[0]][n]+magic {
		fmt.Println(-1)
		return
	}

	if e[0] != e[1] {
		if diagonal[0] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[0]+magic != target {
		fmt.Println(-1)
		return
	}
	if e[0]+e[1] != n-1 {
		if diagonal[1] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[1]+magic != target {
		fmt.Println(-1)
		return
	}

	check_ok := true
	for i := 0; i < n; i++ {
		if i != e[0] {
			if array[i][n] != target {
				check_ok = false
				break
			}
		}
		if i != e[1] {
			if array[n][i] != target {
				check_ok = false
				break
			}
		}
	}

	if !check_ok || magic <= 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(magic)
	}
}

// Codeforces 711 B
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"strconv"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
    var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
    for i:=0; i<len(ss); i++ {
        a[i], err = strconv.Atoi(ss[i])
        if err != nil {
            fmt.Println("n atoi failure", err)
            panic(err)
        }
    }
    return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
    a := make([]int, n)
    s, err := in.ReadString('\n')
    if err != nil {
        fmt.Println("read failure", err)
        panic(err)
    }
    ss := strings.Split(strings.Trim(s, " \n\r"), " ")
    for j:=0; j<n; j++ {
        aa,err := strconv.Atoi(ss[j])
        if err != nil {
            fmt.Println("aa conv failed", err)
            panic(err)
        }
        a[j] = aa
    }
    return a
}

var n,ii,jj int
var sm,val,sum,altSum int64
var aa [][]int
var ok bool

func readData() {
 	in = bufio.NewReader(os.Stdin)
	// reading n
    n,_,_,_,_ = readFive()
    aa = make([][]int, n)
    for i:=0; i<n; i++ {
        aa[i] = make([]int, n)
        aaa := readLineNumbs(n)
        for j:=0; j<n; j++ {
            aa[i][j] = aaa[j]
            if aa[i][j]==0 {
                ii = i
                jj = j
            }
        }
    }
}

func checkSum(sm int64, alt bool) {
    if !ok {
        return
    }
    if alt {
        if altSum==-1 {
            altSum = sm
        } else {
            if altSum != sm {
                ok = false
            }
        }
    } else {
        if sum==-1 {
            sum = sm
        } else {
            if sum != sm {
                ok = false
            }
        }
    }
}

func solve() {
    sum = -1
    altSum = -1
    ok = true
    // rows
    for i:=0; i<n; i++ {
        sm = 0
        for j:=0; j<n; j++ {
            sm += int64(aa[i][j])
        }
        alt := (i==ii)
        checkSum(sm, alt)
    }
    // columns
    for j:=0; j<n; j++ {
        sm = 0
        for i:=0; i<n; i++ {
            sm += int64(aa[i][j])
        }
        alt := (j==jj)
        checkSum(sm, alt)
    }
    // main diagonal
    sm = 0
    for i:=0; i<n; i++ {
        sm += int64(aa[i][i])
    }
    alt := (ii==jj)
    checkSum(sm, alt)
    // other diagonal
    sm = 0
    for i:=0; i<n; i++ {
        sm += int64(aa[i][n-i-1])
    }
    alt = (ii==(n-jj-1))
    checkSum(sm, alt)
}

func printRes() {
    if n==1 {
        val = 1
    } else if ok && (sum > altSum) {
        val = sum - altSum
    } else {
        val = -1
    }
    fmt.Println(val)
}

func main() {
 	readData()

    solve()
    printRes()
}

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	if N == 1 {
		fmt.Println(1)
		return
	}

	A := make([][]int64, N)
	sm := make([]int64, 2 * N + 2)
	zCheck := make([]bool, 2 * N + 2)
	for i := 0; i < N; i++ {
		A[i] = in.NextInt64Array()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zCheck[i] = true
			}
			sm[i] += A[i][j]
		}
	}
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			if A[j][i] == 0 {
				zCheck[i + N] = true
			}
			sm[i + N] += A[j][i]
		}
	}

	for i := 0; i < N; i++ {
		if A[i][i] == 0 {
			zCheck[2 * N ] = true
		}
		sm[2 * N] += A[i][i]
	}

	for i := 0; i < N; i++ {
		if A[i][N - i - 1] == 0 {
			zCheck[2 * N + 1 ] = true
		}

		sm[2 * N + 1] += A[i][N - i - 1]
	}

	mp := map[int64]struct{}{}
	for i := 0; i < len(sm); i++ {
		mp[sm[i]] = struct{}{}
	}

	if len(mp) != 2 {
		fmt.Println(-1)
		return
	}

	amn := int64(-1)
	amx := int64(0)
	for i := 0; i < len(sm); i++ {
		if !zCheck[i] {
			if amn == -1 || amn > sm[i] {
				amn = sm[i]
			}
			if amx < sm[i] {
				amx = sm[i]
			}
		}
	}
	//fmt.Printf("%+v", sm)
	//println(amn, amx)
	if amn != amx {
		fmt.Println(-1)
		return
	}

	mn := int64(-1)
	mx := int64(0)
	for i := 0; i < len(sm); i++ {
		if zCheck[i] {
			if mn == -1 || mn > sm[i] {
				mn = sm[i]
			}
			if mx < sm[i] {
				mx = sm[i]
			}
		}
	}
	if mn != mx || amn <= mn {
		fmt.Println(-1)
		return
	}
	fmt.Println(amn - mn)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

func checkDigonal(A [][]int64, csum int64) bool {
	var sum1, sum2 int64
	N := len(A)
	for i := 0; i < N; i++ {
		sum1 += A[i][i]
		sum2 += A[N-1-i][i]
	}
	if sum1 == sum2 && sum1 == csum {
		return true
	}
	return false
}

func checkCol(A [][]int64, csum int64) bool {
	N := len(A)

	for j := 0; j < N; j++ {
		var sum int64
		for i := 0; i < N; i++ {
			sum += A[i][j]
		}

		if sum != csum {
			return false
		}
	}
	return true
}

func checkRow(A [][]int64, csum int64) bool {
	N := len(A)

	for i := 0; i < N; i++ {
		var sum int64
		for j := 0; j < N; j++ {
			sum += A[i][j]
		}
		if sum != csum {
			return false
		}
	}
	return true
}

func main() {
	fsc := NewFastScanner()
	N := fsc.NextInt()
	var ei, ej int
	A := make([][]int64, N)
	for i := 0; i < N; i++ {
		A[i] = make([]int64, N)
	}
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			a := fsc.NextInt64()
			if a == 0 {
				ei = i
				ej = j
			}
			A[i][j] = a
		}
	}
	//row
	var ci int
	ci = -1
	for i := 0; i < N; i++ {
		if i != ei {
			ci = i
			break
		}
	}
	var csum int64
	if ci == -1 {
		fmt.Println(114514)
		os.Exit(0)
	} else {

		csum = 0
		for j := 0; j < N; j++ {
			csum += A[ci][j]
		}
		var esum int64
		esum = 0
		for j := 0; j < N; j++ {
			esum += A[ei][j]
		}
		if csum-esum < 1 {
			fmt.Println(-1)
			os.Exit(0)
		}
		A[ei][ej] = csum - esum
	}

	if checkCol(A, csum) && checkRow(A, csum) && checkDigonal(A, csum) {
		fmt.Println(A[ei][ej])
	} else {
		fmt.Println(-1)
	}
}

//template
type FastScanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewFastScanner() *FastScanner {
	rdr := bufio.NewReaderSize(os.Stdin, 2028)
	return &FastScanner{r: rdr}
}

func (s *FastScanner) Next() string {
	s.Pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}

func (s *FastScanner) NextInt() int {
	val, err := strconv.Atoi(s.Next())
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) NextInt64() int64 {
	val, err := strconv.ParseInt(s.Next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return val
}
func (s *FastScanner) Pre() {
	if s.p >= len(s.buf) {
		s.ReadLine()
		s.p = 0
	}
}

func (s *FastScanner) ReadLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, err := s.r.ReadLine()
		if err != nil {
			panic(err.Error())
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	a := make([][]int64, n)
	var iz, jz int

	for i := 0; i < n; i++ {
		a[i] = make([]int64, n)
		for j := 0; j < n; j++ {
			fmt.Scan(&a[i][j])
			if a[i][j] == 0 {
				iz = i
				jz = j
			}
		}
	}

	sum := int64(0)

	if n == 1 {
		a[iz][jz] = 1
		sum = 1
	} else {
		k := 0
		for k == iz {
			k++
		}
		sumz := int64(0)
		for j := 0; j < n; j++ {
			sum += a[k][j]
			sumz += a[iz][j]
		}
		a[iz][jz] = sum - sumz
	}

	found := true

	sd1 := int64(0)
	sd2 := int64(0)

	for i := 0; i < n; i++ {
		srow := int64(0)
		scol := int64(0)
		for j := 0; j < n; j++ {
			srow += a[i][j]
			scol += a[j][i]
		}
		if (srow != sum) || (scol != sum) {
			found = false
			break
		}
		sd1 += a[i][i]
		sd2 += a[i][n-1-i]
	}

	if (sd1 != sum) || (sd2 != sum) {
		found = false
	}

	if found {
		fmt.Println(a[iz][jz])
	} else {
		fmt.Println(-1)
	}
}

package main

import "fmt"

func main() {
	var n, tmp, fcolsum, xcolsum int64
	var zi, zj int64
	fmt.Scanln(&n)

	mass := make([][]int64, n)

	for i := int64(0); i < n; i++ {
		mass[i] = make([]int64, n)

		sum := int64(0)
		zero := false

		for j := int64(0); j < n; j++ {
			fmt.Scan(&tmp)
			mass[i][j] = tmp

			if !zero {
				zero = (tmp == 0)

				if zero {
					zi, zj = i, j
				}
			}

			sum = sum + tmp
		}

		if zero {
			xcolsum = sum
		} else {
			if fcolsum != 0 && fcolsum != sum {
				fmt.Println(-1)
				return
			}
			fcolsum = sum
		}
	}

	mass[zi][zj] = fcolsum - xcolsum

	// rows
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[i][j]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// diag 1
	sum := int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][i]
	}

	if fcolsum != sum {
		fmt.Println(-1)
		return
	}

	// diag 2
	sum = int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][n - i - 1]
	}

	if fcolsum != sum {
		fmt.Println(-1)
		return
	}

	fmt.Println(mass[zi][zj])
}

package main

import "fmt"

func main() {
	var n, tmp, fcolsum, xcolsum int64
	var zi, zj int64
	fmt.Scanln(&n)

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

	mass := make([][]int64, n)

	for i := int64(0); i < n; i++ {
		mass[i] = make([]int64, n)

		sum := int64(0)
		zero := false

		for j := int64(0); j < n; j++ {
			fmt.Scan(&tmp)
			mass[i][j] = tmp

			if !zero {
				zero = (tmp == 0)

				if zero {
					zi, zj = i, j
				}
			}

			sum = sum + tmp
		}

		if zero {
			xcolsum = sum
		} else {
			if fcolsum != 0 && fcolsum != sum {
				fmt.Println(-1)
				return
			}
			fcolsum = sum
		}
	}

	mass[zi][zj] = fcolsum - xcolsum

	// filds
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[i][j]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// rows
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[j][i]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// diag 1
	sum := int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][i]
	}

	if fcolsum != sum {
		fmt.Println(-1)
		return
	}

	// diag 2
	sum = int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][n - i - 1]
	}

	if fcolsum != sum {
		fmt.Println(-1)
		return
	}

	fmt.Println(mass[zi][zj])
}

package main

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

func main() {
	var n, tmp, fcolsum, xcolsum int64
	var zi, zj int64

	in := bufio.NewReaderSize(os.Stdin, 1024*1024)
	fmt.Fscan(in, &n)

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

	mass := make([][]int64, n)
	for i := int64(0); i < n; i++ {
		mass[i] = make([]int64, n)

		sum := int64(0)
		zero := false

		for j := int64(0); j < n; j++ {
			fmt.Fscan(in, &tmp)
			mass[i][j] = tmp

			if !zero {
				zero = (tmp == 0)

				if zero {
					zi, zj = i, j
				}
			}

			sum = sum + tmp
		}

		if zero {
			xcolsum = sum
		} else {
			if fcolsum != 0 && fcolsum != sum {
				fmt.Println(-1)
				return
			}
			fcolsum = sum
		}
	}

	mass[zi][zj] = fcolsum - xcolsum
	if mass[zi][zj] == 0 {
		fmt.Println(-1)
		return
	}

	// rows
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[j][i]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// diag 1 & diag 2
	sum := int64(0)
	sum2 := int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][i]
		sum2 = sum2 + mass[i][n - i - 1]
	}

	if fcolsum != sum || fcolsum != sum2 {
		fmt.Println(-1)
		return
	}

	fmt.Println(mass[zi][zj])
}

package main

import "fmt"

func main() {
	var n, tmp, fcolsum, xcolsum int64
	var zi, zj int64
	fmt.Scanln(&n)

	mass := make([][]int64, n)

	for i := int64(0); i < n; i++ {
		mass[i] = make([]int64, n)

		sum := int64(0)
		zero := false

		for j := int64(0); j < n; j++ {
			fmt.Scan(&tmp)
			mass[i][j] = tmp

			if !zero {
				zero = (tmp == 0)

				if zero {
					zi, zj = i, j
				}
			}

			sum = sum + tmp
		}

		if zero {
			xcolsum = sum
		} else {
			if fcolsum != 0 && fcolsum != sum {
				fmt.Println(-1)
				return
			}
			fcolsum = sum
		}
	}

	mass[zi][zj] = fcolsum - xcolsum

	// filds
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[i][j]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// rows
	for i := int64(0); i < n; i++ {
		sum := int64(0)
		for j := int64(0); j < n; j++ {
			sum = sum + mass[j][i]
		}

		if fcolsum != sum {
			fmt.Println(-1)
			return
		}
	}

	// diag 1
	sum := int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][i]
	}

	if fcolsum != sum {
		fmt.Println(-1)
		return
	}

	// diag 2
	sum = int64(0)
	for i := int64(0); i < n; i++ {
		sum = sum + mass[i][n - i - 1]
	}

	if fcolsum != sum {
		fmt.Println(-1)
		return
	}

	fmt.Println(mass[zi][zj])
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	if n == 1 {
		var trash int
		if _, err := fmt.Scanf("%d\n", &trash); err != nil {
			return
		}
		fmt.Println(1)
		return
	}

	array := make([][]int, n+1)
	array[n] = make([]int, n+1)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	e := [2]int{-1, -1}
	diagonal := [2]int{0, 0}
	target := -1
	met_zero := false

	for i := 0; i < n; i++ {
		array[i] = make([]int, n+1)
		r_sum := 0
		for j := 0; j < n && scanner.Scan(); j++ {
			a, err := strconv.Atoi(scanner.Text())
			if err != nil {
				return
			}
			array[i][j] = a
			r_sum += a
			array[n][j] += a

			if i == j {
				diagonal[0] += a
			}
			if i+j == n-1 {
				diagonal[1] += a
			}
			if a == 0 {
				e[0] = i
				e[1] = j
				met_zero = true
			}
		}
		array[i][n] = r_sum
		if target < 0 && !met_zero {
			target = r_sum
		}
		if met_zero {
			met_zero = false
		}
	}

	magic := target - array[n][e[1]]
	if target != array[e[0]][n]+magic {
		fmt.Println(-1)
		return
	}

	if e[0] != e[1] {
		if diagonal[0] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[0]+magic != target {
		fmt.Println(-1)
		return
	}
	if e[0]+e[1] != n-1 {
		if diagonal[1] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[1]+magic != target {
		fmt.Println(-1)
		return
	}

	check_ok := true
	for i := 0; i < n; i++ {
		if i != e[0] {
			if array[i][n] != target {
				check_ok = false
				break
			}
		}
		if i != e[1] {
			if array[n][i] != target {
				check_ok = false
				break
			}
		}
	}

	if !check_ok || magic == 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(magic)
	}
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	if n == 1 {
		var trash int
		if _, err := fmt.Scanf("%d\n", &trash); err != nil {
			return
		}
		fmt.Println(1)
		return
	}

	array := make([][]int64, n+1)
	array[n] = make([]int64, n+1)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	e := [2]int{-1, -1}
	diagonal := [2]int64{0, 0}
	target := int64(-1)
	met_zero := false

	for i := 0; i < n; i++ {
		array[i] = make([]int64, n+1)
		r_sum := int64(0)
		for j := 0; j < n && scanner.Scan(); j++ {
			in, err := strconv.Atoi(scanner.Text())
			if err != nil {
				return
			}
			a := int64(in)
			array[i][j] = a
			r_sum += a
			array[n][j] += a

			if i == j {
				diagonal[0] += a
			}
			if i+j == n-1 {
				diagonal[1] += a
			}
			if a == 0 {
				e[0] = i
				e[1] = j
				met_zero = true
			}
		}
		array[i][n] = r_sum
		if target < 0 && !met_zero {
			target = r_sum
		}
		if met_zero {
			met_zero = false
		}
	}

	magic := target - array[n][e[1]]
	if target != array[e[0]][n]+magic {
		fmt.Println(-1)
		return
	}

	if e[0] != e[1] {
		if diagonal[0] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[0]+magic != target {
		fmt.Println(-1)
		return
	}
	if e[0]+e[1] != n-1 {
		if diagonal[1] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[1]+magic != target {
		fmt.Println(-1)
		return
	}

	check_ok := true
	for i := 0; i < n; i++ {
		if i != e[0] {
			if array[i][n] != target {
				check_ok = false
				break
			}
		}
		if i != e[1] {
			if array[n][i] != target {
				check_ok = false
				break
			}
		}
	}

	if !check_ok || magic == 0 {
		fmt.Println(-1)
	} else {
		fmt.Println(magic)
	}
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	if n == 1 {
		var trash int
		if _, err := fmt.Scanf("%d\n", &trash); err != nil {
			return
		}
		fmt.Println(1)
		return
	}

	array := make([][]int, n+1)
	array[n] = make([]int, n+1)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	e := [2]int{-1, -1}
	diagonal := [2]int{0, 0}
	target := -1
	met_zero := false

	for i := 0; i < n; i++ {
		array[i] = make([]int, n+1)
		r_sum := 0
		for j := 0; j < n && scanner.Scan(); j++ {
			a, err := strconv.Atoi(scanner.Text())
			if err != nil {
				return
			}
			array[i][j] = a
			r_sum += a
			array[n][j] += a

			if i == j {
				diagonal[0] += a
			}
			if i+j == n-1 {
				diagonal[1] += a
			}
			if a == 0 {
				e[0] = i
				e[1] = j
				met_zero = true
			}
		}
		array[i][n] = r_sum
		if target < 0 && !met_zero {
			target = r_sum
		}
		if met_zero {
			met_zero = false
		}
	}

	magic := target - array[n][e[1]]
	if target != array[e[0]][n]+magic {
		fmt.Println(-1)
		return
	}

	if e[0] != e[1] {
		if diagonal[0] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[0]+magic != target {
		fmt.Println(-1)
		return
	}
	if e[0]+e[1] != n-1 {
		if diagonal[1] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[1]+magic != target {
		fmt.Println(-1)
		return
	}

	check_ok := true
	for i := 0; i < n; i++ {
		if i != e[0] {
			if array[i][n] != target {
				check_ok = false
				break
			}
		}
		if i != e[1] {
			if array[n][i] != target {
				check_ok = false
				break
			}
		}
	}

	if !check_ok {
		fmt.Println(-1)
	} else {
		fmt.Println(magic)
	}
}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	array := make([][]int, n+1)
	array[n] = make([]int, n+1)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	e := [2]int{-1, -1}
	diagonal := [2]int{0, 0}
	target := -1
	met_zero := false

	for i := 0; i < n; i++ {
		array[i] = make([]int, n+1)
		r_sum := 0
		for j := 0; j < n && scanner.Scan(); j++ {
			a, err := strconv.Atoi(scanner.Text())
			if err != nil {
				return
			}
			array[i][j] = a
			r_sum += a
			array[n][j] += a

			if i == j {
				diagonal[0] += a
			}
			if i+j == n-1 {
				diagonal[1] += a
			}
			if a == 0 {
				e[0] = i
				e[1] = j
				met_zero = true
			}
		}
		array[i][n] = r_sum
		if target < 0 && !met_zero {
			target = r_sum
		}
		if met_zero {
			met_zero = false
		}
	}

	magic := target - array[n][e[1]]
	if target != array[e[0]][n]+magic {
		fmt.Println(-1)
		return
	}

	if e[0] != e[1] {
		if diagonal[0] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[0]+magic != target {
		fmt.Println(-1)
		return
	}
	if e[0]+e[1] != n-1 {
		if diagonal[1] != target {
			fmt.Println(-1)
			return
		}
	} else if diagonal[1]+magic != target {
		fmt.Println(-1)
		return
	}

	check_ok := true
	for i := 0; i < n; i++ {
		if i != e[0] {
			if array[i][n] != target {
				check_ok = false
				break
			}
		}
		if i != e[1] {
			if array[n][i] != target {
				check_ok = false
				break
			}
		}
	}

	if !check_ok {
		fmt.Println(-1)
	} else {
		fmt.Println(magic)
	}
}

// Codeforces 711 B
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"strconv"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
    var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
    for i:=0; i<len(ss); i++ {
        a[i], err = strconv.Atoi(ss[i])
        if err != nil {
            fmt.Println("n atoi failure", err)
            panic(err)
        }
    }
    return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
    a := make([]int, n)
    s, err := in.ReadString('\n')
    if err != nil {
        fmt.Println("read failure", err)
        panic(err)
    }
    ss := strings.Split(strings.Trim(s, " \n\r"), " ")
    for j:=0; j<n; j++ {
        aa,err := strconv.Atoi(ss[j])
        if err != nil {
            fmt.Println("aa conv failed", err)
            panic(err)
        }
        a[j] = aa
    }
    return a
}

var n,ii,jj int
var val,sum,altSum int
var aa [][]int
var ok bool

func readData() {
 	in = bufio.NewReader(os.Stdin)
	// reading n
    n,_,_,_,_ = readFive()
    aa = make([][]int, n)
    for i:=0; i<n; i++ {
        aa[i] = make([]int, n)
        aaa := readLineNumbs(n)
        for j:=0; j<n; j++ {
            aa[i][j] = aaa[j]
            if aa[i][j]==0 {
                ii = i
                jj = j
            }
        }
    }
}

func checkSum(sm int, alt bool) {
    if !ok {
        return
    }
    if alt {
        if altSum==-1 {
            altSum = sm
        } else {
            if altSum != sm {
                ok = false
            }
        }
    } else {
        if sum==-1 {
            sum = sm
        } else {
            if sum != sm {
                ok = false
            }
        }
    }
}

func solve() {
    sum = -1
    altSum = -1
    ok = true
    // rows
    for i:=0; i<n; i++ {
        sm := 0
        for j:=0; j<n; j++ {
            sm += aa[i][j]
        }
        alt := (i==ii)
        checkSum(sm, alt)
    }
    // columns
    for j:=0; j<n; j++ {
        sm := 0
        for i:=0; i<n; i++ {
            sm += aa[i][j]
        }
        alt := (j==jj)
        checkSum(sm, alt)
    }
    // main diagonal
    sm := 0
    for i:=0; i<n; i++ {
        sm += aa[i][i]
    }
    alt := (ii==jj)
    checkSum(sm, alt)
    // other diagonal
    sm = 0
    for i:=0; i<n; i++ {
        sm += aa[i][n-i-1]
    }
    alt = (ii==(n-jj-1))
    checkSum(sm, alt)
}

func printRes() {
    if n==1 {
        val = 1
    } else if ok && (sum > altSum) {
        val = sum - altSum
    } else {
        val = -1
    }
    fmt.Println(val)
}

func main() {
 	readData()

    solve()
    printRes()
}

// Codeforces 711 B
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
	"strconv"
)
var in *bufio.Reader

func readFive() (int,int,int,int,int) {
    var a [5]int
	s, err := in.ReadString('\n')
	if err != nil {
		fmt.Println("first read failure", err)
		panic(err)
	}
	ss := strings.Split(strings.Trim(s, " \n\r"), " ")
    for i:=0; i<len(ss); i++ {
        a[i], err = strconv.Atoi(ss[i])
        if err != nil {
            fmt.Println("n atoi failure", err)
            panic(err)
        }
    }
    return a[0],a[1],a[2],a[3],a[4]
}

func readLineNumbs(n int) []int {
    a := make([]int, n)
    s, err := in.ReadString('\n')
    if err != nil {
        fmt.Println("read failure", err)
        panic(err)
    }
    ss := strings.Split(strings.Trim(s, " \n\r"), " ")
    for j:=0; j<n; j++ {
        aa,err := strconv.Atoi(ss[j])
        if err != nil {
            fmt.Println("aa conv failed", err)
            panic(err)
        }
        a[j] = aa
    }
    return a
}

var n,ii,jj int
var val,sum,altSum int
var aa [][]int
var ok bool

func readData() {
 	in = bufio.NewReader(os.Stdin)
	// reading n
    n,_,_,_,_ = readFive()
    aa = make([][]int, n)
    for i:=0; i<n; i++ {
        aa[i] = make([]int, n)
        aaa := readLineNumbs(n)
        for j:=0; j<n; j++ {
            aa[i][j] = aaa[j]
            if aa[i][j]==0 {
                ii = i
                jj = j
            }
        }
    }
}

func checkSum(sm int, alt bool) {
    if !ok {
        return
    }
    if alt {
        if altSum==-1 {
            altSum = sm
        } else {
            if altSum != sm {
                ok = false
            }
        }
    } else {
        if sum==-1 {
            sum = sm
        } else {
            if sum != sm {
                ok = false
            }
        }
    }
}

func solve() {
    sum = -1
    altSum = -1
    ok = true
    // rows
    for i:=0; i<n; i++ {
        sm := 0
        for j:=0; j<n; j++ {
            sm += aa[i][j]
        }
        alt := (i==ii)
        checkSum(sm, alt)
    }
    // columns
    for j:=0; j<n; j++ {
        sm := 0
        for i:=0; i<n; i++ {
            sm += aa[i][j]
        }
        alt := (j==jj)
        checkSum(sm, alt)
    }
    // main diagonal
    sm := 0
    for i:=0; i<n; i++ {
        sm += aa[i][i]
    }
    alt := (ii==jj)
    checkSum(sm, alt)
    // other diagonal
    sm = 0
    for i:=0; i<n; i++ {
        sm += aa[i][n-i-1]
    }
    alt = (ii==(n-jj-1))
    checkSum(sm, alt)
}

func printRes() {
    if n==1 {
        val = 1
    } else if ok {
        val = sum - altSum
    } else {
        val = -1
    }
    fmt.Println(val)
}

func main() {
 	readData()

    solve()
    printRes()
}

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int, N)
	zero := 0
	sm := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = in.NextIntArray()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zero = i
			}
			sm[i] += A[i][j]
		}
	}
	if N == 1 {
		fmt.Println(1)
		return
	}
	B := 1
	if zero == 1 {
		B = 0
	}

	if N == 2 {
		if sm[B] - sm[zero] > 0 {
			fmt.Println(sm[B] - sm[zero])
			return
		} else {
			fmt.Println(-1)
			return
		}
	}
	mp := map[int]struct{}{}
	for i := 0; i < N; i++ {
		mp[sm[i]] = struct{}{}
	}
	if len(mp) != 1 {
		fmt.Println(-1)
		return
	}

	fmt.Println(sm[B] - sm[zero])
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int64, N)
	zeroColumn := 0
	zeroRow := 0
	sm := make([]int64, 2 * N + 2)
	for i := 0; i < N; i++ {
		A[i] = in.NextInt64Array()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zeroColumn = i
				zeroRow = j
			}
			sm[i] += A[i][j]
		}
	}
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			sm[i + N] += A[j][i]
		}
	}

	cross := int64(0)
	for i := 0; i < N; i++ {
		cross += A[i][i]
	}
	sm = append(sm, cross)
	for i := 0; i < N; i++ {
		cross += A[i][N - i - 1]
	}
	sm = append(sm, cross)
	for i := 0; i < N; i++ {
		cross += A[N - i - 1][i]
	}
	sm = append(sm, cross)

	s1 := int64(0)
	for i := 0; i < N; i++ {
		s1 += A[i][zeroRow]
	}

	s2 := int64(0)
	for i := 0; i < N; i++ {
		s2 += A[zeroColumn][i]
	}

	if s1 != s2 || cross != 0 && (s1 != cross || s2 != cross) {
		fmt.Println(-1)
		return
	}

	if N == 1 {
		fmt.Println(1)
		return
	}

	B := -1
	mn := sm[0]
	mx := int64(0)
	mp := map[int64]struct{}{}
	for i := 0; i < N; i++ {
		mp[sm[i]] = struct{}{}
		mn = min(mn, sm[i])
		if mx < sm[i] {
			B = i
		}
		mx = max(mx, sm[i])
	}
	if len(mp) != 2 || mn != sm[zeroColumn] {
		fmt.Println(-1)
		return
	}
	if sm[B] - sm[zeroColumn] > 0 {
		fmt.Println(sm[B] - sm[zeroColumn])
		return
	}
	fmt.Println(-1)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	if N == 1 {
		fmt.Println(1)
		return
	}

	A := make([][]int64, N)
	sm := make([]int64, 2 * N + 2)
	zCheck := make([]bool, 2 * N + 2)
	for i := 0; i < N; i++ {
		A[i] = in.NextInt64Array()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zCheck[i] = true
			}
			sm[i] += A[i][j]
		}
	}
	for j := 0; j < N; j++ {
		for i := 0; i < N; i++ {
			if A[j][i] == 0 {
				zCheck[j + N] = true
			}
			sm[j + N] += A[j][i]
		}
	}

	for i := 0; i < N; i++ {
		if A[i][i] == 0 {
			zCheck[2 * N ] = true
		}
		sm[2 * N] += A[i][i]
	}

	for i := 0; i < N; i++ {
		if A[i][N - i - 1] == 0 {
			zCheck[2 * N + 1 ] = true
		}

		sm[2 * N + 1] += A[i][N - i - 1]
	}

	mp := map[int64]struct{}{}
	for i := 0; i < len(sm); i++ {
		mp[sm[i]] = struct{}{}
	}

	if len(mp) != 2 {
		fmt.Println(-1)
		return
	}

	amn := int64(-1)
	amx := int64(0)
	for i := 0; i < len(sm); i++ {
		if !zCheck[i] {
			if amn == -1 || amn > sm[i] {
				amn = sm[i]
			}
			if amx < sm[i] {
				amx = sm[i]
			}
		}
	}
	if amn != amx {
		fmt.Println(-1)
		return
	}

	mn := int64(-1)
	mx := int64(0)
	for i := 0; i < len(sm); i++ {
		if zCheck[i] {
			if mn == -1 || mn > sm[i] {
				mn = sm[i]
			}
			if mx < sm[i] {
				mx = sm[i]
			}
		}
	}
	if mn != mx || amn <= mn {
		fmt.Println(-1)
		return
	}
	fmt.Println(amn - mn)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int64, N)
	zeroColumn := 0
	zeroRow := 0
	sm := make([]int64, 2 * N)
	for i := 0; i < N; i++ {
		A[i] = in.NextInt64Array()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zeroColumn = i
				zeroRow = j
			}
			sm[i] += A[i][j]
		}
	}
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			sm[i + N] += A[j][i]
		}
	}

	cross := int64(0)
	if zeroColumn == zeroRow {
		for i := 0; i < N; i++ {
			cross += A[i][i]
		}
		sm = append(sm, cross)
	} else if zeroColumn + zeroRow == N - 1 {
		if zeroColumn < zeroColumn {
			for i := 0; i < N; i++ {
				cross += A[i][N - i - 1]
			}
			sm = append(sm, cross)
		} else {
			for i := 0; i < N; i++ {
				cross += A[N - i - 1][i]
			}
			sm = append(sm, cross)
		}
	}

	s1 := int64(0)
	for i := 0; i < N; i++ {
		s1 += A[i][zeroRow]
	}

	s2 := int64(0)
	for i := 0; i < N; i++ {
		s2 += A[zeroColumn][i]
	}

	if s1 != s2 || cross != 0 && (s1 != cross || s2 != cross) {
		fmt.Println(-1)
		return
	}

	if N == 1 {
		fmt.Println(1)
		return
	}

	B := -1
	mn := sm[0]
	mx := int64(0)
	mp := map[int64]struct{}{}
	for i := 0; i < N; i++ {
		mp[sm[i]] = struct{}{}
		mn = min(mn, sm[i])
		if mx < sm[i] {
			B = i
		}
		mx = max(mx, sm[i])
	}
	if len(mp) != 2 || mn != sm[zeroColumn] {
		fmt.Println(-1)
		return
	}

	if sm[B] - sm[zeroColumn] > 0 {
		fmt.Println(sm[B] - sm[zeroColumn])
		return
	}
	fmt.Println(-1)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int, N)
	zero := 0
	sm := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = in.NextIntArray()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zero = i
			}
			sm[i] += A[i][j]
		}
	}
	if N == 1 {
		fmt.Println(1)
		return
	}

	if N == 2 {
		B := 1
		if zero == 1 {
			B = 0
		}

		if sm[B] - sm[zero] > 0 {
			fmt.Println(sm[B] - sm[zero])
			return
		} else {
			fmt.Println(-1)
			return
		}
	}
	B := 0
	mn := sm[0]
	mx := 0
	mp := map[int]struct{}{}
	for i := 0; i < N; i++ {
		mp[sm[i]] = struct{}{}
		mn = min(mn, sm[i])
		if mx < sm[i] {
			B = i
		}
		mx = max(mx, sm[i])
	}
	if len(mp) != 2 || mn != sm[zero] {
		fmt.Println(-1)
		return
	}

	if sm[B] - sm[zero] > 0 {
		fmt.Println(sm[B] - sm[zero])
		return
	}
	fmt.Println(-1)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

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

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int, N)
	zero := 0
	sm := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = in.NextIntArray()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zero = i
			}
			sm[i] += A[i][j]
		}
	}
	if N == 1 {
		fmt.Println(1)
		return
	}
	B := 1
	if zero == 1 {
		B = 0
	}

	if N == 2 {
		if sm[B] - sm[zero] > 0 {
			fmt.Println(sm[B] - sm[zero])
			return
		} else {
			fmt.Println(-1)
			return
		}
	}
	if sm[0] != sm[1] && sm[1] != sm[2] && sm[0] != sm[2] {
		fmt.Println(-1)
		return
	}

	fmt.Println(sm[B] - sm[zero])
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int, N)
	zero := 0
	sm := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = in.NextIntArray()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zero = i
			}
			sm[i] += A[i][j]
		}
	}
	if N == 1 {
		fmt.Println(1)
		return
	}
	B := 1
	if zero == 1 {
		B = 0
	}

	if N == 2 {
		if sm[B] - sm[zero] > 0 {
			fmt.Println(sm[B] - sm[zero])
			return
		} else {
			fmt.Println(-1)
			return
		}
	}
	mp := map[int]struct{}{}
	for i := 0; i < N; i++ {
		mp[sm[i]] = struct{}{}
	}
	if len(mp) != 2 {
		fmt.Println(-1)
		return
	}

	if sm[B] - sm[zero] > 0 {
		fmt.Println(sm[B] - sm[zero])
		return
	}
	fmt.Println(-1)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int, N)
	zero := 0
	sm := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = in.NextIntArray()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zero = i
			}
			sm[i] += A[i][j]
		}
	}
	if N == 1 {
		fmt.Println(1)
		return
	}

	if N == 2 {
		B := 1
		if zero == 1 {
			B = 0
		}

		if sm[B] - sm[zero] > 0 {
			fmt.Println(sm[B] - sm[zero])
			return
		} else {
			fmt.Println(-1)
			return
		}
	}
	B := -1
	mn := sm[0]
	mx := 0
	mp := map[int]struct{}{}
	for i := 0; i < N; i++ {
		mn = min(mn, sm[i])
		mx = max(mx, sm[i])
	}
	if len(mp) != 2 || mn != sm[zero] {
		fmt.Println(-1)
		return
	}

	if sm[B] - sm[zero] > 0 {
		fmt.Println(sm[B] - sm[zero])
		return
	}
	fmt.Println(-1)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func min(a int, b int) int {
	if a < b {
		return a
	}
	return b
}

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

package main

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

type TupleInt struct {
	first  int
	second int
	third  int
}

func main() {
	in := NewScanner()
	N := in.NextInt()
	A := make([][]int64, N)
	zeroColumn := 0
	zeroRow := 0
	sm := make([]int64, 2 * N)
	for i := 0; i < N; i++ {
		A[i] = in.NextInt64Array()
		for j := 0; j < N; j++ {
			if A[i][j] == 0 {
				zeroColumn = i
				zeroRow = j
			}
			sm[i] += A[i][j]
		}
	}
	for i := 0; i < N; i++ {
		for j := 0; j < N; j++ {
			sm[i + N] += A[j][i]
		}
	}

	cross1 := int64(0)
	for i := 0; i < N; i++ {
		cross1 += A[i][i]
	}
	sm = append(sm, cross1)

	cross2 := int64(0)
	for i := 0; i < N; i++ {
		cross2 += A[i][N - i - 1]
	}
	sm = append(sm, cross2)

	s1 := int64(0)
	for i := 0; i < N; i++ {
		s1 += A[i][zeroRow]
	}

	s2 := int64(0)
	for i := 0; i < N; i++ {
		s2 += A[zeroColumn][i]
	}
	h := []int64{s1, s2, cross1, cross2}
	amn := h[0]
	amx := h[0]
	for i := 0; i < 4; i++ {
		if amn > h[i] {
			amn = h[i]
		}
		if amx < h[i] {
			amx = h[i]
		}
	}
	//if amn != amx {
	//	fmt.Println(-1)
	//	return
	//}
	if N == 1 {
		fmt.Println(1)
		return
	}

	B := -1
	mn := sm[0]
	mx := int64(0)
	mp := map[int64]struct{}{}
	for i := 0; i < len(sm); i++ {
		mp[sm[i]] = struct{}{}
		mn = min(mn, sm[i])
		if mx < sm[i] {
			B = i
		}
		mx = max(mx, sm[i])
	}
	if len(mp) != 2 || mn != sm[zeroColumn] {
		fmt.Println(-1)
		return
	}
	if sm[B] - sm[zeroColumn] > 0 {
		fmt.Println(sm[B] - sm[zeroColumn])
		return
	}
	fmt.Println(-1)
	return

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
	result = append(result, v)

	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func min(a int64, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

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


Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balanced — their sum is equal to $$$0$$$.Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two.There are two conditions though:   For each participant $$$i$$$, their modified rating change $$$b_i$$$ must be integer, and as close to $$$\frac{a_i}{2}$$$ as possible. It means that either $$$b_i = \lfloor \frac{a_i}{2} \rfloor$$$ or $$$b_i = \lceil \frac{a_i}{2} \rceil$$$. In particular, if $$$a_i$$$ is even, $$$b_i = \frac{a_i}{2}$$$. Here $$$\lfloor x \rfloor$$$ denotes rounding down to the largest integer not greater than $$$x$$$, and $$$\lceil x \rceil$$$ denotes rounding up to the smallest integer not smaller than $$$x$$$.  The modified rating changes must be perfectly balanced — their sum must be equal to $$$0$$$. Can you help with that?

The first line contains a single integer $$$n$$$ ($$$2 \le n \le 13\,845$$$), denoting the number of participants. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$-336 \le a_i \le 1164$$$), denoting the rating change of the $$$i$$$-th participant. The sum of all $$$a_i$$$ is equal to $$$0$$$.

Output $$$n$$$ integers $$$b_i$$$, each denoting the modified rating change of the $$$i$$$-th participant in order of input. For any $$$i$$$, it must be true that either $$$b_i = \lfloor \frac{a_i}{2} \rfloor$$$ or $$$b_i = \lceil \frac{a_i}{2} \rceil$$$. The sum of all $$$b_i$$$ must be equal to $$$0$$$. If there are multiple solutions, print any. We can show that a solution exists for any valid input.

NoteIn the first example, $$$b_1 = 5$$$, $$$b_2 = -3$$$ and $$$b_3 = -2$$$ is another correct solution.In the second example there are $$$6$$$ possible solutions, one of them is shown in the example output.

package main

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

/* solution */

func solve() {
	n := readInt()
	f := 1
	for i := 0; i < n; i++ {
		v := readInt()
		if v%2 != 0 {
			v += f
			f = -f
		}
		printInt(v / 2)
	}
}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func printInt64(i int64) {
	print(strconv.FormatInt(i, 10))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}

	cnt := 0
	b := make([]int, n)
	for i, ai := range a {
		if ai&1 == 1 {
			cnt++
			if cnt&1 == 1 {
				b[i] = (ai + 1) / 2
			} else {
				b[i] = (ai - 1) / 2
			}
		} else {
			b[i] = ai / 2
		}
	}
	for _, bi := range b {
		Fprintln(out, bi)
	}
}

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

package main

import (
	"fmt"
)

func main() {
	var n, a int
	fmt.Scanf("%d\n", &n)

	b := make([]int, n)
	cnt := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d\n", &a)
		if a%2 == 0 {
			b[i] = a / 2
		} else {
			cnt++
			if cnt%2 == 0 {
				b[i] = (a + 1) / 2
			} else {
				b[i] = (a - 1) / 2
			}
		}
	}

	// output
	for i := 0; i < n; i++ {
		fmt.Println(b[i])
	}
}

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int, n)
	odd := 0
	for i := range arr {
		Fscan(in, &arr[i])
		if arr[i]&1 == 1 {
			odd++
		}
	}

	b := make([]int, n)
	sum := 0
	flag1, flag2 := 0, 0
	for i, ai := range arr {
		b[i] = ai / 2
		if ai&1 == 1 {
			if b[i] > 0 {
				if flag1&1 == 1 {
					b[i]++
				}
				flag1++
			} else {
				if flag2&1 == 1 {
					b[i]--
				}
				flag2++
			}
		}
		sum += b[i]
	}

	//Fprintln(out, sum)

	for _, bi := range b {
		Fprintln(out, bi)
	}
}

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

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int, n)
	odd := 0
	for i := range arr {
		Fscan(in, &arr[i])
		if arr[i]&1 == 1 {
			odd++
		}
	}

	b := make([]int, n)
	sum := 0

	for i, ai := range arr {
		b[i] = ai / 2
		sum += b[i]
	}

	for i,ai := range arr {
		if ai&1 == 1 {
			if sum < 0 && b[i] > 0 {
				b[i]++
				sum++
			} else if sum > 0 && b[i] < 0 {
				b[i]--
				sum--
			}
			if sum == 0 {
				break
			}
		}
	}

	//Fprintln(out, sum)

	for _, bi := range b {
		Fprintln(out, bi)
	}
}

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

package main

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

func solve(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int, n)
	odd := 0
	for i := range arr {
		Fscan(in, &arr[i])
		if arr[i]&1 == 1 {
			odd++
		}
	}

	b := make([]int, n)
	sum := 0
	flag :=0
	for i, ai := range arr {
		b[i] = ai / 2
		if ai&1 == 1 {
			if flag & 1 == 1 {
				if b[i]>0 {
					b[i]++

				} else {
						b[i]--
				}
			}
		}
		flag++
		sum += b[i]
	}

	//Fprintln(out, sum)

	for _, bi := range b {
		Fprintln(out, bi)
	}
}

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

package main

import (
	"fmt"
)

func main() {
	var n, a int
	fmt.Scanf("%d", &n)

	b := make([]int, n)
	cnt := 0

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a)
		if a%2 == 0 {
			b[i] = a / 2
		} else {
			cnt++
			if cnt%2 == 0 {
				b[i] = (a + 1) / 2
			} else {
				b[i] = (a - 1) / 2
			}
		}
	}

	// output
	for i := 0; i < n; i++ {
		fmt.Println(b[i])
	}
}


You and your $$$n - 1$$$ friends have found an array of integers $$$a_1, a_2, \dots, a_n$$$. You have decided to share it in the following way: All $$$n$$$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.You are standing in the $$$m$$$-th position in the line. Before the process starts, you may choose up to $$$k$$$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.Suppose that you're doing your choices optimally. What is the greatest integer $$$x$$$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $$$x$$$?Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains three space-separated integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 \le m \le n \le 3500$$$, $$$0 \le k \le n - 1$$$)  — the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$)  — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3500$$$.

For each test case, print the largest integer $$$x$$$ such that you can guarantee to obtain at least $$$x$$$.

NoteIn the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element.  the first person will take the last element ($$$5$$$) because he or she was forced by you to take the last element. After this turn the remaining array will be $$$[2, 9, 2, 3, 8]$$$;  the second person will take the first element ($$$2$$$) because he or she was forced by you to take the first element. After this turn the remaining array will be $$$[9, 2, 3, 8]$$$;  if the third person will choose to take the first element ($$$9$$$), at your turn the remaining array will be $$$[2, 3, 8]$$$ and you will take $$$8$$$ (the last element);  if the third person will choose to take the last element ($$$8$$$), at your turn the remaining array will be $$$[9, 2, 3]$$$ and you will take $$$9$$$ (the first element). Thus, this strategy guarantees to end up with at least $$$8$$$. We can prove that there is no strategy that guarantees to end up with at least $$$9$$$. Hence, the answer is $$$8$$$.In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $$$4$$$.

package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanInt() int {
	sc.Scan(); a,_ := strconv.Atoi(sc.Text()); return a
}

func scanInt64() int64 {
	sc.Scan(); a,_ := strconv.ParseInt(sc.Text(),10,64); return a
}

func scanFloat64() float64 {
	sc.Scan(); a,_ := strconv.ParseFloat(sc.Text(),64); return a
}

func scanInts(n int) []int {
	res := make([]int, n); for i := 0; i < n; i++ { res[i] = scanInt() }
	return res
}

func scanString() string {
	sc.Scan(); return sc.Text()
}

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e); if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr); wr.Flush()
}

func abs(a int) int { if a<0 { return -a }; return a }
func min(a,b int) int { if a<b { return a }; return b }
func max(a,b int) int { if a>b { return a }; return b }

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1000000)

	in := scanInt()
	for t := 0; t < in; t++ {
		n := scanInt()
		m := scanInt()
		k := scanInt()
		a := scanInts(n)

		ans := 0

		l := 0
		ma := min(k,m-1)
		for l < ma+1 {
			r := ma-l
			// fmt.Println(l,n-r)
			arr := a[l:n-r]
			mi := 1 << 31 -1
			for i := 0; i < len(arr)-(n-m); i++ {
				mi = min(mi,max(arr[i],arr[i+n-m]))
			}

			ans = max(ans, mi)
			// fmt.Println(arr,mi)
			l++
		}

		fmt.Println(ans)
	}
}

package main

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

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

	var t int
	fmt.Fscanf(reader, "%d\n", &t)
	for i := 0; i < t; i++ {
		var n, m, k int
		fmt.Fscanf(reader, "%d %d %d\n", &n, &m, &k)
		vals := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d", &vals[j])
		}
		fmt.Fscanf(reader, "\n")
		fmt.Fprintf(writer, "%d\n", findBestValue(vals, m, k))
	}
	writer.Flush()
}

func findBestValue(vals []int, m int, k int) int {
	n := len(vals)

	dp := make([][]int, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int, n)
		for j := 0; j < n; j++ {
			dp[i][j] = -1
		}
	}

	for len := n - m + 1; len <= n; len++ {
		for i := 0; i+len <= n; i++ {
			j := i + len - 1
			if len == n-m+1 {
				dp[i][j] = max(vals[i], vals[j])
			} else {
				if k > n-len {
					dp[i][j] = max(dp[i+1][j], dp[i][j-1])
				} else {
					dp[i][j] = min(dp[i+1][j], dp[i][j-1])
				}
			}
		}
	}

	return dp[0][n-1]
}

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

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

package main

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

func run(_r io.Reader, _w io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func() (ans int) {
		var n, kth, fix int
		Fscan(in, &n, &kth, &fix)
		fix = min(fix, kth-1)
		kth-- // todo
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		dp := make([][]int, n)
		for i := range dp {
			dp[i] = make([]int, n)
			for j := range dp[i] {
				dp[i][j] = -1
			}
		}

		minLen := n - kth

		kth -= fix

		var f func(int, int) int
		f = func(l, r int) (_ans int) {
			if l + minLen-1 ==r {
				return max(a[l],a[r])
			}

			//if l > r {
			//	return 0
			//}
			if dp[l][r] != -1 {
				return dp[l][r]
			}
			defer func() { dp[l][r] = _ans }()

			f1 := f(l,r-1)
			f2 := f(l+1,r)

			return min(f1,f2)
		}

		for i := 0; i <= fix; i++ {
			fAns :=  f(i, i+n-fix-1)
			ans = max(ans,fAns)
		}

		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		_ans := solve()
		Fprintln(out, _ans)
	}
}

func main() {
	//debug.SetGCPercent(-1)
	run(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"io"
	"os"
	"runtime/debug"
)

func run(_r io.Reader, _w io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func() (ans int) {
		var n, kth, fix int
		Fscan(in, &n, &kth, &fix)
		fix = min(fix, kth-1)
		kth-- // todo
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		dp := make([][]int, n)
		for i := range dp {
			dp[i] = make([]int, n)
			for j := range dp[i] {
				dp[i][j] = -1
			}
		}

		minLen := n - kth

		kth -= fix

		var f func(int, int) int
		f = func(l, r int) (_ans int) {
			if l + minLen-1 ==r {
				return max(a[l],a[r])
			}

			//if l > r {
			//	return 0
			//}
			if dp[l][r] != -1 {
				return dp[l][r]
			}
			defer func() { dp[l][r] = _ans }()

			f1 := f(l,r-1)
			f2 := f(l+1,r)

			return min(f1,f2)
		}

		for i := 0; i <= fix; i++ {
			fAns :=  f(i, i+n-fix-1)
			ans = max(ans,fAns)
		}

		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		_ans := solve()
		Fprintln(out, _ans)
	}
}

func main() {
	debug.SetGCPercent(-1)
	run(os.Stdin, os.Stdout) }

package main

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

func run(_r io.Reader, _w io.Writer) {
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	solve := func() (ans int) {
		var n, kth, fix int
		Fscan(in, &n, &kth, &fix)
		fix = min(fix, kth-1)
		kth-- // todo
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
		}

		dp := make([][]int, n)
		for i := range dp {
			dp[i] = make([]int, n)
			for j := range dp[i] {
				dp[i][j] = -1
			}
		}

		minLen := n - kth

		kth -= fix

		var f func(int, int) int
		f = func(l, r int) (_ans int) {
			if l + minLen-1 ==r {
				return max(a[l],a[r])
			}

			//if l > r {
			//	return 0
			//}
			if dp[l][r] != -1 {
				return dp[l][r]
			}
			defer func() { dp[l][r] = _ans }()

			f1 := f(l,r-1)
			f2 := f(l+1,r)

			return min(f1,f2)
		}

		for i := 0; i <= fix; i++ {
			fAns :=  f(i, i+n-fix-1)
			ans = max(ans,fAns)
		}

		return
	}

	var t int
	Fscan(in, &t)
	for _case := 1; _case <= t; _case++ {
		_ans := solve()
		Fprintln(out, _ans)
	}
}

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

package main

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

//•*¨*•.¸¸♪I/O•*¨*•.¸¸♪

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

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

func scanInt64() int64 {
	sc.Scan()
	a,_ := strconv.ParseInt(sc.Text(),10,64)
	return a
}

func scanFloat64() float64 {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return float64(a)
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ { res[i] = scanInt() }
	return res
}

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

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	// sc.Buffer(make([]byte, 10000), 100000000)

	in := scanInt()

	for t := 0; t < in; t++ {
		n := scanInt()
		m := scanInt()
		k := scanInt()
		a := scanInts(n)

		l, r := 0, n-m
		arr := make([]int, 0)

		for r < n {
			arr = append(arr, max(a[l],a[r]))
			l++
			r++
		}

		l,r = 0, len(arr)-1
		for i := 0; i < min(k,m-1); i++ {
			if arr[l] < arr[r] {
				l++
			} else {
				r--
			}
		}
		for i := 0; i < max(0,m-1-k); i++ {
			if arr[l] > arr[r] {
				l++
			} else {
				r--
			}
		}
		// fmt.Println(arr)

		fmt.Println(max(arr[l],arr[r]))
	}

}

func min(a ...int) int {
	res := a[0]
	for i := 1; i < len(a); i++ {
		if res > a[i] { res = a[i] }
	}
	return res
}

func max(a ...int) int {
	res := a[0]
	for i := 1; i < len(a); i++ {
		if res < a[i] { res = a[i] }
	}
	return res
}

package main

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

//•*¨*•.¸¸♪I/O•*¨*•.¸¸♪

var (
	sc = bufio.NewScanner(os.Stdin)
	wr = bufio.NewWriter(os.Stdout)
)

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

func scanInt64() int64 {
	sc.Scan()
	a,_ := strconv.ParseInt(sc.Text(),10,64)
	return a
}

func scanFloat64() float64 {
	sc.Scan()
	a,_ := strconv.Atoi(sc.Text())
	return float64(a)
}

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ { res[i] = scanInt() }
	return res
}

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

func printInts(a ...int) {
	for i, e := range a {
		fmt.Fprint(wr, e)
		if i != len(a)-1 { fmt.Fprint(wr, " ") }
	}
	fmt.Fprintln(wr)
	wr.Flush()
}

//•*¨*•.¸¸♪Main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)

func main() {
	sc.Split(bufio.ScanWords)
	// sc.Buffer(make([]byte, 10000), 100000000)

	in := scanInt()

	for t := 0; t < in; t++ {
		n := scanInt()
		m := scanInt()
		k := scanInt()
		a := scanInts(n)

		l, r := 0, n-m
		arr := make([]int, 0)

		for r < n {
			arr = append(arr, max(a[l],a[r]))
			l++
			r++
		}

		sort.Ints(arr)
		fmt.Println(arr[min(k,m-1)])

	}

}

func min(a ...int) int {
	res := a[0]
	for i := 1; i < len(a); i++ {
		if res > a[i] { res = a[i] }
	}
	return res
}

func max(a ...int) int {
	res := a[0]
	for i := 1; i < len(a); i++ {
		if res < a[i] { res = a[i] }
	}
	return res
}


Vlad came home and found out that someone had reconfigured the old thermostat to the temperature of $$$a$$$.The thermostat can only be set to a temperature from $$$l$$$ to $$$r$$$ inclusive, the temperature cannot change by less than $$$x$$$. Formally, in one operation you can reconfigure the thermostat from temperature $$$a$$$ to temperature $$$b$$$ if $$$|a - b| \ge x$$$ and $$$l \le b \le r$$$.You are given $$$l$$$, $$$r$$$, $$$x$$$, $$$a$$$ and $$$b$$$. Find the minimum number of operations required to get temperature $$$b$$$ from temperature $$$a$$$, or say that it is impossible.

The first line of input data contains the single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The descriptions of the test cases follow. The first line of each case contains three integers $$$l$$$, $$$r$$$ and $$$x$$$ ($$$-10^9 \le l \le r \le 10^9$$$, $$$1 \le x \le 10^9$$$) — range of temperature and minimum temperature change. The second line of each case contains two integers $$$a$$$ and $$$b$$$ ($$$l \le a, b \le r$$$) — the initial and final temperatures.

Output $$$t$$$ numbers, each of which is the answer to the corresponding test case. If it is impossible to achieve the temperature $$$b$$$, output -1, otherwise output the minimum number of operations.

NoteIn the first example, the thermostat is already set up correctly.In the second example, you can achieve the desired temperature as follows: $$$4 \rightarrow 10 \rightarrow 5$$$.In the third example, you can achieve the desired temperature as follows: $$$3 \rightarrow 8 \rightarrow 2 \rightarrow 7$$$.In the fourth test, it is impossible to make any operation.

package main

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

func Abs(a int32) int32 {
	if a < 0 {
		return -a
	}
	return a
}

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

	var tt int32
	fmt.Fscan(reader, &tt)

TT:
	for ; tt > 0; tt-- {
		var l, r, x, a, b int32
		fmt.Fscan(reader, &l, &r, &x, &a, &b)
		var limits [2]int32 = [2]int32{l, r}

		if a == b {
			// a equals b : 0
			fmt.Fprintf(writer, "0\n")
			continue TT

		} else if Abs(a-b) >= x {
			// |a - b| >= x : 1
			fmt.Fprintf(writer, "1\n")
			continue TT

		} else {

			// a limit h such that |a - h| >= x and |b - h| >= x : 2
			for h := 0; h < 2; h++ {
				if Abs(limits[h]-a) >= x && Abs(limits[h]-b) >= x {
					fmt.Fprintf(writer, "2\n")
					continue TT
				}
			}

			// 2 limits h, k such that |a - h| >= x and |b - k| >= x : 3
			for h := 0; h < 2; h++ {
				if Abs(limits[h]-b) >= x && Abs(limits[(h+1)%2]-a) >= x {
					fmt.Fprintf(writer, "3\n")
					continue TT
				}
			}

			fmt.Fprintf(writer, "-1\n")
		}
	}

	writer.Flush()
}

package main

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

func solve(l, r, x, a, b int) int {
	if a == b {
		return 0
	}
	if b-a >= x {
		return 1
	}
	if a-b >= x {
		return 1
	}

	if r-a >= x && r-b >= x {
		return 2
	}
	if a-l >= x && b-l >= x {
		return 2
	}

	if r-a >= x && r-l >= x && b-l >= x {
		return 3
	}
	if a-l >= x && r-l >= x && r-b >= x {
		return 3
	}

	return -1
}

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

	var test int
	fmt.Fscanln(inp, &test)
	for test > 0 {
		test--
		var l, r, x, a, b int
		fmt.Fscanln(inp, &l, &r, &x)
		fmt.Fscanln(inp, &a, &b)
		fmt.Fprintln(out, solve(l, r, x, a, b))
	}

	out.Flush()
}

package main

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

func solve(reader *bufio.Reader, writer *bufio.Writer) {
	l, r, x := readThreeInt(reader)
	a, b := readTwoInt(reader)

	if a == b {
		fmt.Fprintln(writer, 0)
		return
	}

	if abs(l-b) < x && abs(r-b) < x || abs(a-l) < x && abs(a-r) < x {
		fmt.Fprintln(writer, -1)
		return
	}

	if abs(a-b) >= x {
		fmt.Fprintln(writer, 1)
		return
	}

	if abs(a-l) >= x && abs(l-b) >= x || abs(a-r) >= x && abs(r-b) >= x {
		fmt.Fprintln(writer, 2)
		return
	}

	fmt.Fprintln(writer, 3)
}

func main() {
	reader := bReader(os.Stdin)
	writer := bWriter(os.Stdout)

	t := readInt(reader)

	for t > 0 {
		solve(reader, writer)
		t--
	}

	writer.Flush()
}

func min[T int | int64](a, b T) T {
	if a < b {
		return a
	}

	return b
}

func max[T int | int64](a, b T) T {
	if a > b {
		return a
	}

	return b
}

func abs[T int | int64](a T) T {
	if a < 0 {
		return a * -1
	} else {
		return a
	}
}

func gcd[T int | int64](x, y T) T {
	for y > 0 {
		x, y = y, x%y
	}

	return x
}

func lcm[T int | int64](x, y T) T {
	return x * y / gcd(x, y)
}

func bReader(stdin *os.File) *bufio.Reader {
	return bufio.NewReaderSize(stdin, 16*1024*1024)
}

func bWriter(stdout *os.File) *bufio.Writer {
	return bufio.NewWriterSize(stdout, 16*1024*1024)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func readInt(reader *bufio.Reader) int {
	return int(readInt64(reader))
}

func readInt64(reader *bufio.Reader) int64 {
	value, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)

	return value
}

func readString(reader *bufio.Reader) string {
	return readLine(reader)
}

func readArrayString(reader *bufio.Reader) []string {
	return strings.Split(strings.TrimSpace(readLine(reader)), " ")
}

func readArrayInt(reader *bufio.Reader) []int {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := int(arItemTemp)
		ar = append(ar, arItem)
	}

	return ar
}

func readArrayInt64(reader *bufio.Reader) []int64 {
	arTemp := strings.Split(strings.TrimSpace(readLine(reader)), " ")
	n := len(arTemp)

	var ar []int64

	for i := 0; i < n; i++ {
		arItemTemp, err := strconv.ParseInt(arTemp[i], 10, 64)
		checkError(err)
		arItem := arItemTemp
		ar = append(ar, arItem)
	}

	return ar
}

func readTwoInt(reader *bufio.Reader) (int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1]
}

func readTwoInt64(reader *bufio.Reader) (int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1]
}

func readThreeInt(reader *bufio.Reader) (int, int, int) {
	ar := readArrayInt(reader)
	return ar[0], ar[1], ar[2]
}

func readThreeInt64(reader *bufio.Reader) (int64, int64, int64) {
	ar := readArrayInt64(reader)
	return ar[0], ar[1], ar[2]
}

package main

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

func abs(a int) int {
	if a < 0 { return -a }
	return a
}

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var L, R, X, A, B int
		fmt.Fscan(r, &L, &R, &X, &A, &B)

		res := 3
		if A == B {
			res = 0
		} else if abs(L-A) < X && abs(R-A) < X {
			res = -1
		} else if abs(L-B) < X && abs(R-B) < X {
			res = -1
		} else if abs(A-B) >= X {
			res = 1
		} else if B > A && B+X <= R {
			res = 2
		} else if B > A && A-X >= L {
			res = 2
		} else if B < A && B-X >= L {
			res = 2
		} else if B < A && A+X <= R {
			res = 2
		}

		fmt.Fprintln(w, res)
	}

}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var l, r, x, a, b int
		fmt.Fscan(in, &l, &r, &x)
		fmt.Fscan(in, &a, &b)
		if a == b {
			fmt.Fprintln(out, "0")
			continue
		}
		if a > b {
			a, b = b, a
		}
		if b-a >= x {
			fmt.Fprintln(out, "1")
			continue
		}
		if (a-l >= x && b-l >= x) || (r-a >= x && r-b >= x) {
			fmt.Fprintln(out, "2")
			continue
		}
		if (r-a >= x && b-l >= x) || (a-l >= x && r-b >= x) {
			fmt.Fprintln(out, "3")
			continue
		}
		fmt.Fprintln(out, "-1")
	}
	out.Flush()
}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"index/suffixarray"
	"log"
	"math"
	"net"
	"os"
	"path"
	"sort"
	"strconv"
	"strings"
	"time"
)

// For Codeforces
func main() {
	io := newStdIO()
	defer io.Flush()
	for T := io.ScanUInt16(); T > 0; T-- {
		solve(&io)
	}
}
func solve(io *IO) {
	l := io.ScanInt64()
	r := io.ScanInt64()
	x := io.ScanInt64()
	a := io.ScanInt64()
	b := io.ScanInt64()

	if a == b {
		io.PrintLn(0)
		return
	}
	if Abs(a-b) >= x {
		io.PrintLn(1)
		return
	}

	if a < b && Abs(a-l) >= x {
		io.PrintLn(2)
		return
	}
	if a < b && Abs(a-r) >= x && Abs(b-r) >= x {
		io.PrintLn(2)
		return
	}
	if a > b && Abs(a-r) >= x {
		io.PrintLn(2)
		return
	}
	if a > b && Abs(a-l) >= x && Abs(b-l) >= x {
		io.PrintLn(2)
		return
	}

	if a > b && Abs(a-l) >= x && Abs(r-b) >= x {
		io.PrintLn(3)
		return
	}
	if a < b && Abs(a-r) >= x && Abs(b-l) >= x {
		io.PrintLn(3)
		return
	}
	io.PrintLn(-1)
}

// #region INTERFACES

// From https://pkg.go.dev/golang.org/x/exp/constraints
type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}
type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
type Integer interface {
	Signed | Unsigned
}
type Float interface {
	~float32 | ~float64
}
type Complex interface {
	~complex64 | ~complex128
}
type Ordered interface {
	Integer | Float | ~string
}

type Prioritizable[T any] interface {
	PriorTo(x T) bool // If you take this as a relation: if (a,b) in R => (b,a) not in R
}

// #endregion
// #region TYPES WITH METHODS
// #region SingleLinkedList
type singleLinkedListNode[T comparable] struct {
	Value T
	Next  *singleLinkedListNode[T]
}

type SingleLinkedList[T comparable, IndexType Unsigned] struct {
	first  *singleLinkedListNode[T]
	last   *singleLinkedListNode[T]
	length IndexType
}

func NewSingleLinkedList[T comparable, IndexType Unsigned]() *SingleLinkedList[T, IndexType] {
	return &SingleLinkedList[T, IndexType]{
		first:  nil,
		last:   nil,
		length: 0,
	}
}
func (l *SingleLinkedList[T, I]) Len() I { return l.length }

// length > 0
func (l *SingleLinkedList[T, I]) First() T { return l.first.Value }

// length > 0
func (l *SingleLinkedList[T, I]) Last() T { return l.last.Value }

func (l *SingleLinkedList[T, I]) InsertFirst(value T) {
	if l.length == 0 {
		nodeToInsert := &singleLinkedListNode[T]{
			Value: value,
			Next:  l.first,
		}
		l.first = nodeToInsert
		l.last = nodeToInsert
	} else {
		l.first = &singleLinkedListNode[T]{
			Value: value,
			Next:  l.first,
		}
	}
	l.length++
}

func (l *SingleLinkedList[T, I]) InsertLast(value T) {
	if l.length == 0 {
		nodeToInsert := &singleLinkedListNode[T]{
			Value: value,
			Next:  l.first,
		}
		l.first = nodeToInsert
		l.last = nodeToInsert
	} else {
		l.last.Next = &singleLinkedListNode[T]{
			Value: value,
			Next:  nil,
		}
		l.last = l.last.Next
	}
	l.length++
}

// index <= length
func (l *SingleLinkedList[T, I]) InsertAt(index I, value T) {
	if index == 0 {
		l.InsertFirst(value)
		return
	}
	if index == l.length {
		l.InsertLast(value)
		return
	}

	n := l.first
	for index > 1 {
		n = n.Next
		index--
	}
	n.Next = &singleLinkedListNode[T]{
		Value: value,
		Next:  n.Next,
	}
	l.length++
}

func (l *SingleLinkedList[T, I]) Contains(value T) bool {
	tmp := l.first
	for i := I(0); i < l.length; i++ {
		if tmp.Value == value {
			return true
		}
		tmp = tmp.Next
	}
	return false
}
func (l *SingleLinkedList[T, I]) Clear() {
	l.first = nil
	l.last = nil
	l.length = 0
}

// index < length
func (l *SingleLinkedList[T, I]) GetElementAt(index I) T {
	n := l.first
	for index > 0 {
		n = n.Next
		index--
	}
	return n.Value
}

// index < length
func (l *SingleLinkedList[T, I]) SetElementAt(index I, value T) {
	n := l.first
	for index > 0 {
		n = n.Next
		index--
	}
	n.Value = value
}

func (l *SingleLinkedList[T, I]) RemoveFirst() T {
	tmp := l.first
	l.first = l.first.Next
	l.length--
	if l.length == 0 {
		l.last = nil
	}
	return tmp.Value
}
func (l *SingleLinkedList[T, I]) RemoveLast() (value T) {
	value = l.last.Value
	if l.length == 1 {
		l.first = nil
		l.last = nil
		l.length = 0
		return
	}

	tmp := l.first
	for i := I(2); i < l.length; i++ {
		tmp = tmp.Next
	}
	l.last = tmp
	return
}

func (l *SingleLinkedList[T, I]) RemoveAt(index I) T {
	if index == 0 {
		return l.RemoveFirst()
	}
	if index == l.length-1 {
		return l.RemoveLast()
	}

	tmp := l.first
	for i := I(1); i < index; i++ {
		tmp = tmp.Next
	}
	res := tmp.Next.Value
	tmp.Next = tmp.Next.Next
	return res
}

func (l *SingleLinkedList[T, I]) ToSlice() (res []T) {
	res = make([]T, 0, l.length)
	tmp := l.first
	for i := I(0); i < l.length; i++ {
		res = append(res, tmp.Value)
		tmp = tmp.Next
	}
	return
}
func (it *SingleLinkedList[T, I]) String() string {
	return "Qua:: " + fmt.Sprint(it.ToSlice())
}

// #endregion
// #region Queue

type Queue[T comparable, I Unsigned] struct {
	l SingleLinkedList[T, I]
}

func NewQueue[T comparable, I Unsigned]() *Queue[T, I] {
	return &Queue[T, I]{
		l: SingleLinkedList[T, I]{
			first:  nil,
			last:   nil,
			length: 0,
		},
	}
}
func (q *Queue[T, I]) Len() I          { return q.l.length }
func (q *Queue[T, I]) Enqueue(value T) { q.l.InsertLast(value) }
func (q *Queue[T, I]) Dequeue(T) T     { return q.l.RemoveFirst() }
func (q *Queue[T, I]) Preview() T      { return q.l.First() }
func (q *Queue[T, I]) String() string  { return q.l.String() }

// #endregion
// #region Stack

type Stack[T comparable, I Unsigned] struct {
	l SingleLinkedList[T, I]
}

func NewStack[T comparable, I Unsigned]() *Stack[T, I] {
	return &Stack[T, I]{
		l: SingleLinkedList[T, I]{
			first:  nil,
			last:   nil,
			length: 0,
		},
	}
}
func (s *Stack[T, I]) Len() I         { return s.l.length }
func (s *Stack[T, I]) Push(value T)   { s.l.InsertFirst(value) }
func (s *Stack[T, I]) Pop(T) T        { return s.l.RemoveFirst() }
func (s *Stack[T, I]) Preview() T     { return s.l.First() }
func (s *Stack[T, I]) String() string { return s.l.String() }

// #endregion
// #region Heap
type BinaryHeap[T Prioritizable[T], I Signed] struct {
	s []T
}

func NewBinaryHeap[T Prioritizable[T], I Signed](s []T) (h *BinaryHeap[T, I]) {
	h = &BinaryHeap[T, I]{
		s: s,
	}

	for i := (h.Len() - 2) / 2; i >= 0; i-- {
		h.heapifyDown(i)
	}
	return
}
func (h *BinaryHeap[T, I]) Len() I {
	return I(len(h.s))
}
func (h *BinaryHeap[T, I]) Push(value T) {
	h.s = append(h.s, value)
	h.heapifyUp(h.Len() - 1)
}
func (h *BinaryHeap[T, I]) Pop() (res T) {
	res = h.s[0]
	h.s[0] = h.s[h.Len()-1]
	h.s = h.s[:h.Len()-1]
	h.heapifyDown(0)
	return
}

func (h *BinaryHeap[T, I]) heapifyDown(index I) bool {
	origin := index
	for {
		j := index*2 + 2
		if j < h.Len() {
			if h.s[j-1].PriorTo(h.s[j]) {
				j--
			}
		} else {
			if j <= h.Len() && !h.s[j].PriorTo(h.s[index]) {
				h.s[j], h.s[index] = h.s[index], h.s[j]
			}
			break
		}
		if h.s[j].PriorTo(h.s[index]) {
			h.s[j], h.s[index] = h.s[index], h.s[j]
			index = j
		} else {
			break
		}
	}
	return origin != index
}
func (h *BinaryHeap[T, I]) heapifyUp(index I) {
	for {
		parent := (index - 1) / 2
		if parent == index || h.s[parent].PriorTo(h.s[index]) {
			break
		}
		h.s[index], h.s[parent] = h.s[parent], h.s[index]
		index = parent
	}
}

// #endregion

// #endregion

// #region FUNCTIONS
func Max[T Ordered](a, b T) T {
	if a >= b {
		return a
	}
	return b
}
func Min[T Ordered](a, b T) T {
	if a <= b {
		return a
	}
	return b
}
func Abs[T Integer | Float](a T) T {
	if a < 0 {
		return -a
	}
	return a
}
func Swap[T any](a, b *T) {
	*a, *b = *b, *a
}

// At least one != 0
func GCD[T Unsigned](a, b T) T {
	if b < a {
		Swap(&a, &b)
	}
	for {
		if a == 0 {
			return b
		}
		r := (b % a)
		b = a
		a = r
	}
}

// At least one != 0
func LCM[T Unsigned](a, b T) T {
	return (a * b) / GCD(a, b)
}

func First[T any](a T, _ any) T { return a }

// #endregion

// #region IO STUFF
type IO struct {
	r *bufio.Reader
	w *bufio.Writer
}

func newStdIO() IO {
	return IO{
		r: bufio.NewReader(os.Stdin),
		w: bufio.NewWriter(os.Stdout),
	}
}

// Is assumed that input.txt file exists
func newFileIO() IO {
	return IO{
		r: bufio.NewReader(First(os.Open("input.txt"))),
		w: bufio.NewWriter(First(os.OpenFile("output.txt", os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm))),
	}
}

func (io *IO) ScanInt8() (x int8)   { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanInt16() (x int16) { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanInt32() (x int32) { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanInt64() (x int64) { fmt.Fscan(io.r, &x); return }

func (io *IO) ScanUInt8() (x uint8)   { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanUInt16() (x uint16) { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanUInt32() (x uint32) { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanUInt64() (x uint64) { fmt.Fscan(io.r, &x); return }

func (io *IO) ScanFloat32() (x float32) { fmt.Fscan(io.r, &x); return }
func (io *IO) ScanFloat64() (x float64) { fmt.Fscan(io.r, &x); return }

func (io *IO) ScanString() (x string) { fmt.Fscan(io.r, &x); return }

func (io *IO) Print(x ...any)   { fmt.Fprint(io.w, x...) }
func (io *IO) PrintLn(x ...any) { fmt.Fprintln(io.w, x...) }

func (io *IO) Flush() { io.w.Flush() }

// #endregion

// #region KEEP IMPORTS
func D() {
	_ = bufio.Reader{}
	_ = bytes.Buffer{}
	_ = suffixarray.Index{}
	_ = log.Default()
	_ = math.Abs(0)
	_ = net.Dialer{}
	_ = path.ErrBadPattern
	_ = sort.Float64sAreSorted(nil)
	_ = strconv.ErrRange
	_ = strings.Builder{}
	_ = time.ANSIC
	_ = newStdIO()
	_ = newFileIO()
}

// #endregion

package main

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

func Solve(reader *bufio.Reader, writer *bufio.Writer) {
	var l, r, x int
	fmt.Fscan(reader, &l, &r, &x)
	var a, b int
	fmt.Fscan(reader, &a, &b)
	if a == b {
		fmt.Fprintf(writer, "0")
	} else if max(abc(b-l), abc(b-r)) < x || max(abc(a-l), abc(a-r)) < x {
		fmt.Fprintf(writer, "-1")
	} else if abc(a-b) >= x {
		fmt.Fprintf(writer, "1")
	} else {
		ans := 3
		if abc(b-l) >= x {
			if abc(a-l) >= x {
				ans = 2
			}
		}
		if abc(b-r) >= x {
			if abc(a-r) >= x {
				ans = 2
			}
		}
		fmt.Fprintf(writer, "%v", ans)
	}

	fmt.Fprintf(writer, "\n")
}

func abc(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

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

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

func main() {
	var reader *bufio.Reader
	var writer *bufio.Writer

	filename_in := "data.in"
	filename_out := "data.out"

	stdinout := true
	readtests := true
	tests := 1

	if (!stdinout || inArgs("local")) && !inArgs("std") {
		infile, inerr := os.Open(filename_in)
		if inerr != nil {
			if os.IsNotExist(inerr) {
				os.Create(filename_in)
			}
			fmt.Println(inerr)
			return
		}
		defer infile.Close()

		outfile, outerr := os.OpenFile(filename_out, os.O_WRONLY, os.ModeAppend)
		if outerr != nil {
			if os.IsNotExist(outerr) {
				outfile, outerr = os.Create(filename_out)
				if outerr != nil {
					fmt.Println(outerr)
					return
				}
			}
		}
		defer outfile.Close()

		reader = bufio.NewReader(infile)
		writer = bufio.NewWriter(outfile)
	} else if stdinout || inArgs("std") {
		reader = bufio.NewReader(os.Stdin)
		writer = bufio.NewWriter(os.Stdout)
	}
	defer func() {
		err := writer.Flush()
		if err != nil {
			fmt.Println(err)
		}
	}()

	if readtests || inArgs("local") {
		fmt.Fscanf(reader, "%d\n", &tests)
	}

	for i := 0; i < tests; i++ {
		Solve(reader, writer)
		fmt.Fscanf(reader, "\n")
	}

}

func inArgs(arg string) bool {
	for _, a := range os.Args {
		if a == arg {
			return true
		}
	}
	return false
}

package main

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

func solve(l, r, x, a, b int) int {
	if a == b {
		return 0
	}
	if b-a >= x {
		return 1
	}

	if r-a >= x && r-b >= x {
		return 2
	}
	if a-l >= x && b-l >= x {
		return 2
	}

	if r-a >= x && r-l >= x && b-l >= x {
		return 3
	}
	if a-l >= x && r-l >= x && r-b >= x {
		return 3
	}

	return -1
}

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

	var test int
	fmt.Fscanln(inp, &test)
	for test > 0 {
		test--
		var l, r, x, a, b int
		fmt.Fscanln(inp, &l, &r, &x)
		fmt.Fscanln(inp, &a, &b)
		fmt.Fprintln(out, solve(l, r, x, a, b))
	}

	out.Flush()
}