package main

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

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

	var n, m int
	fmt.Fscan(in, &n)
	fmt.Fscanln(in, &m)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscanln(in, &a[n-1])

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &b[i])
	}
	fmt.Fscanln(in, &b[n-1])

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

	db := deltas(b, m)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	da := deltas(a, m)

	offset := m
	for i := 0; i < len(a); i++ {
		if check2(da, db, i) {
			off := 0
			if a[i] < b[0] {
				off = b[0] - a[i]
			}
			if a[0] > b[0] {
				off = b[0] + m - a[i]
			}
			off = off % m
			if off < offset {
				offset = off
			}
		}
	}

	fmt.Fprint(out, offset)
	out.Flush()
}

func deltas(a []int, m int) []int {
	b := make([]int, len(a))
	for i := 1; i < len(a); i++ {
		b[i-1] = a[i] - a[i-1]
	}
	b[len(b)-1] = -(a[len(a)-1] + m - a[0])
	return b
}

func check2(a, b []int, start int) bool {
	i := start
	for j := 0; j < len(a); j++ {
		if a[i] != b[j] {
			return false
		}

		i = (i + 1) % len(a)
	}
	return true
}

package main

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

var n, m int
var a, b []int

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([]int, n)
	b = make([]int, n)
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
		mp[a[i]] = 1
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}
	fmt.Fscanf(reader, "\n")

	var c []int
	for k := range mp {
		c = append(c, k)
	}

	sort.Ints(c)
	sort.Ints(b)
	sort.Ints(a)

	if reflect.DeepEqual(a, b) {
		fmt.Println(0)
		return
	}

	for i := len(c) - 1; i >= 0; i-- {
		t := make([]int, n)
		copy(t, a)

		x := (m + b[0] - c[i]) % m
		for j := 0; j < n; j++ {
			a[j] = (a[j] + x) % m
		}
		sort.Ints(a)
		if reflect.DeepEqual(a, b) {
			fmt.Println(x)
			return
		}
		copy(a, t)
	}
}

package main

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

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

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

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

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

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

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

var n, m int
var A, B []int

func main() {
	n, m = ReadInt2()
	A = ReadIntSlice(n)
	B = ReadIntSlice(n)

	amemo, bmemo := make(map[int]int), make(map[int]int)
	for i := 0; i < n; i++ {
		amemo[A[i]]++
		bmemo[B[i]]++
	}

	ans := -1
	aval, anum := A[0], amemo[A[0]]
	for bval, bnum := range bmemo {
		if anum != bnum {
			continue
		}

		var x int
		if bval > aval {
			x = bval - aval
		} else {
			x = bval + m - aval
		}

		isOK := true
		for av, an := range amemo {
			bv := (av + x) % m
			bn := bmemo[bv]
			if an == bn {
				continue
			} else {
				isOK = false
				break
			}
		}

		if isOK {
			if ans == -1 {
				ans = x
			} else {
				ChMin(&ans, x)
			}
		}
	}

	if ans == m {
		ans = 0
	}

	fmt.Println(ans)
}

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

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

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

package main

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

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Simple math functions

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

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

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

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

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcdll(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcdll(b, a%b)
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	n := readi()
	m := readi()
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		a[i] = readi()
	}
	for i := range b {
		b[i] = readi()
	}
	sort.Ints(a)
	sort.Ints(b)

	var av, ac, bv, bc []int
	av = append(av, a[0])
	ac = append(ac, 1)
	for i := 1; i < n; i++ {
		if a[i] != av[len(av)-1] {
			av = append(av, a[i])
			ac = append(ac, 0)
		}
		ac[len(ac)-1]++
	}
	bv = append(bv, b[0])
	bc = append(bc, 1)
	for i := 1; i < n; i++ {
		if b[i] != bv[len(bv)-1] {
			bv = append(bv, b[i])
			bc = append(bc, 0)
		}
		bc[len(bc)-1]++
	}

	minx := m + 1
outer:
	for i := range ac {
		p := av[i]
		q := bv[0]
		if q < p {
			q += m
		}
		x := q - p
		for j := range bc {
			k := (i + j) % len(ac)
			if (av[k]+x)%m != bv[j] || ac[k] != bc[j] {
				continue outer
			}
		}
		minx = min(x, minx)
	}
	println(minx)
}

package main

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

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

	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")

	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &b[i])
	}
	fmt.Fscanf(in, "\n")

	fmt.Fprintln(out, Subhodeep(a, b, m))

}

func Subhodeep(a, b []int, m int) int {
	x := make([]int, len(a))
	sort.Ints(a)
	sort.Ints(b)

	for i := 0; i < len(a); i++ {
		xVal := (b[0] - a[i] + m) % m
		x[i] = xVal
	}
	sort.Ints(x)

	//fmt.Println("a", a)
	//fmt.Println("b", b)
	//fmt.Println("x", x)

	for i := 0; i < len(a); i++ {
		k := make([]int, len(a))

		for j := 0; j < len(a); j++ {
			k[j] = (a[j] + x[i]) % m
		}
		sort.Ints(k)

		isValid := true
		for j := 0; j < len(a); j++ {
			if k[j] != b[j] {
				isValid = false
				break
			}
		}
		if isValid {
			return x[i]
		}
	}
	return 0

}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, m int
	fmt.Fscan(reader, &n, &m)
	a := make([]int, n)
	b := make([]int, n)
	c := make([]int, n)
	d := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
	}
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &b[i])
	}
	sort.Ints(b)
	for i := 0; i < n; i++ {
		c[i] = (b[i] - a[0] + m) % m
	}

	sort.Ints(c)
	for i := 0; i < n; i++ {
		if i == 0 || c[i] != c[i - 1] {
			var flag bool = false
			for j := 0; j < n; j++ {
				d[j] = (a[j] + c[i]) % m
			}
			sort.Ints(d)
			for j := 0; j < n; j++ {
				if b[j] != d[j] {
					flag = true
					break
				}
			}
			if flag == false {
				fmt.Fprintln(writer, c[i])
				break
			}
		}
	}
}//2020-08-07 15:08:41.063

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	var m int
	_, _ = fmt.Scan(&n, &m)
	x := make([]int, n)
	y := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&x[i])
	}
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&y[i])
	}
	sort.Ints(x)
	sort.Ints(y)
	var j int
	var i int
	for i = 0; i < n; i++ {
		for j = 0; j < n - 1; j++ {
			k := (i + j) % n
			r := (j + 1) % n
			t := (k + 1) % n
			if !((x[j] == x[r] && y[k] == y[t]) || (x[j] != x[r] && y[k] != y[t])) {
				break
			}
		}
		// fmt.Println(j)
		// j = n - 1
		if j == n-1 {
			var a int = x[0]
			var b int = y[i]
			// (a + x) % p = b
			var h int
			if a <= b {
				h = b - a
			} else {
				h = b - a + m
			}
			for j = 0; j < n; j++ {
				k := (i + j) % n
				if (x[j] + h) % m != y[k] {
					break
				}
			}
			if j == n {
				fmt.Println(h)
				return
			}
		}
	}
}

package main

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

var n, m int
var a, b []int

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([]int, n)
	b = make([]int, n)
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
		mp[a[i]] = 1
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}
	fmt.Fscanf(reader, "\n")

	var c []int
	for k := range mp {
		c = append(c, k)
	}

	sort.Ints(c)
	sort.Ints(b)
	sort.Ints(a)

	if reflect.DeepEqual(a, b) {
		fmt.Println(0)
		return
	}

	for i := len(c) - 1; i >= 0; i-- {
		x := m - c[i]
		for j := 0; j < n; j++ {
			a[j] = (a[j] + x) % m
		}
		sort.Ints(a)
		if reflect.DeepEqual(a, b) {
			fmt.Println(x)
			return
		}
		for j := 0; j < n; j++ {
			a[j] = (a[j] + c[i]) % m
		}
	}
}

package main

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

var n, m int
var a, b []int

func main() {
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a = make([]int, n)
	b = make([]int, n)
	mp := make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
		mp[a[i]] = 1
	}
	fmt.Fscanf(reader, "\n")
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &b[i])
	}
	fmt.Fscanf(reader, "\n")

	var c []int
	for k := range mp {
		c = append(c, k)
	}

	sort.Ints(c)
	sort.Ints(b)
	sort.Ints(a)

	if reflect.DeepEqual(a, b) {
		fmt.Println(0)
		return
	}

	for i := len(c) - 1; i >= 0; i-- {
		x := (m + b[0] - c[i]) % m
		for j := 0; j < n; j++ {
			a[j] = (a[j] + x) % m
		}
		sort.Ints(a)
		if reflect.DeepEqual(a, b) {
			fmt.Println(x)
			return
		}
		for j := 0; j < n; j++ {
			a[j] = (a[j] + m - b[0] + c[i]) % m
		}
	}
}

package main

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

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Simple math functions

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

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

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

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

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

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func gcdll(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcdll(b, a%b)
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()

	n := readi()
	m := readi()
	a := make([]int, n)
	b := make([]int, n)
	for i := range a {
		a[i] = readi()
	}
	for i := range b {
		b[i] = readi()
	}
	sort.Ints(a)
	sort.Ints(b)

	var av, ac, bv, bc []int
	av = append(av, a[0])
	ac = append(ac, 1)
	for i := 1; i < n; i++ {
		if a[i] != av[len(av)-1] {
			av = append(av, a[i])
			ac = append(ac, 0)
		}
		ac[len(ac)-1]++
	}
	bv = append(bv, b[0])
	bc = append(bc, 1)
	for i := 1; i < n; i++ {
		if b[i] != bv[len(bv)-1] {
			bv = append(bv, b[i])
			bc = append(bc, 0)
		}
		bc[len(bc)-1]++
	}

outer:
	for i := range ac {
		for j := range bc {
			k := (i + j) % len(ac)
			if ac[k] != bc[j] {
				continue outer
			}
		}
		p := av[i]
		q := bv[0]
		if p <= q {
			println(q - p)
		} else {
			println(q + m - p)
		}
	}
}

package main

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

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

	n, m := 0, 0
	fmt.Fscanf(in, "%d %d\n", &n, &m)
	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
	}
	fmt.Fscanf(in, "\n")

	for i := 0; i < n; i++ {
		fmt.Fscanf(in, "%d", &b[i])
	}
	fmt.Fscanf(in, "\n")

	fmt.Fprintln(out, Subhodeep(a, b, m))

}

func getXArr(a, b []int, m int) []int {
	sort.Ints(a)
	sort.Ints(b)
	xs := make([]int, len(a))

	for i := 0; i < len(a); i++ {
		x := (b[0] - a[i] + m) % m
		xs = append(xs, x)
	}

	return xs
}

func isEqualArr(k, b []int) bool {
	for i := 0; i < len(k); i++ {
		if k[i] != b[i] {
			return false
		}
	}
	return true
}

func Subhodeep(a, b []int, m int) int {
	x := make([]int, len(a))

	sort.Ints(a)
	sort.Ints(b)

	for i := 0; i < len(a); i++ {
		xVal := (b[0] - a[i] + m) % m
		x[i] = xVal
	}
	sort.Ints(x)

	fmt.Println("a", a)
	fmt.Println("b", b)
	fmt.Println("x", x)

	for i := 0; i < len(a); i++ {
		k := make([]int, len(a))

		for j := 0; j < len(a); j++ {
			k[j] = (a[j] + x[i]) % m
		}
		sort.Ints(k)

		isValid := true
		for j := 0; j < len(a); j++ {
			if k[j] != b[j] {
				isValid = false
				break
			}
		}
		if isValid {
			return x[i]
		}
	}
	return 0

}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, m int
	_, _ = fmt.Scan(&n, &m)
	x := make([]int, n)
	y := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&x[i])
	}
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&y[i])
	}
	sort.Ints(x)
	sort.Ints(y)
	var j int
	var i int
	for i = 0; i < n; i++ {
		for j = 0; j < n - 1; j++ {
			k := (i + j) % n
			r := (j + 1) % n
			t := (k + 1) % n
			if !((x[j] == x[r] && y[k] == y[t]) || (x[j] != x[r] && y[k] != y[t])) {
				break
			}
		}
		// j = n - 1
		if j == n-1 {
			break
		}
	}
	a := x[0]
	b := y[i]
	// (a + x) % p = b
	if a <= b {
		fmt.Println(b - a)
	} else {
		fmt.Println(b + m - a)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	var m int64
	_, _ = fmt.Scan(&n, &m)
	x := make([]int, n)
	y := make([]int, n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&x[i])
	}
	for i := 0; i < n; i++ {
		_, _ = fmt.Scan(&y[i])
	}
	sort.Ints(x)
	sort.Ints(y)
	var j int
	var i int
	for i = 0; i < n; i++ {
		for j = 0; j < n - 1; j++ {
			k := (i + j) % n
			r := (j + 1) % n
			t := (k + 1) % n
			if !((x[j] == x[r] && y[k] == y[t]) || (x[j] != x[r] && y[k] != y[t])) {
				break
			}
		}
		// j = n - 1
		if j == n-1 {
			break
		}
	}
	var a int64 = int64(x[0])
	var b int64 = int64(y[i])
	// (a + x) % p = b
	if a <= b {
		fmt.Println(b - a)
	} else {
		fmt.Println(b + m - a)
	}
}

package main

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

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

	var n, m int
	fmt.Fscan(in, &n)
	fmt.Fscanln(in, &m)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscanln(in, &a[n-1])

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &b[i])
	}
	fmt.Fscanln(in, &b[n-1])

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

	db := deltas(b, m)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	da := deltas(a, m)

	for i := 0; i < len(a); i++ {
		if check2(da, db, i) {
			offset := 0
			if a[i] < b[0] {
				offset = b[0] - a[i]
			}
			if a[0] > b[0] {
				offset = b[0] + m - a[i]
			}
			fmt.Fprint(out, offset%m)
			out.Flush()
		}
	}
}

func deltas(a []int, m int) []int {
	b := make([]int, len(a))
	for i := 1; i < len(a); i++ {
		b[i-1] = a[i] - a[i-1]
	}
	b[len(b)-1] = -(a[len(a)-1] + m - a[0])
	return b
}

func check2(a, b []int, start int) bool {
	i := start
	for j := 0; j < len(a); j++ {
		if a[i] != b[j] {
			return false
		}

		i = (i + 1) % len(a)
	}
	return true
}

package main

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

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

	var n, m int
	fmt.Fscan(in, &n)
	fmt.Fscanln(in, &m)

	a := make([]int, n)
	b := make([]int, n)

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &a[i])
	}
	fmt.Fscanln(in, &a[n-1])

	for i := 0; i < n-1; i++ {
		fmt.Fscan(in, &b[i])
	}
	fmt.Fscanln(in, &b[n-1])

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

	db := deltas(b, m)

	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	da := deltas(a, m)

	for i := 0; i < len(a); i++ {
		if check2(da, db, i) {
			offset := 0
			if a[i] < b[0] {
				offset = b[0] - a[i]
			}
			if a[0] > b[0] {
				offset = b[0] + m - a[i]
			}
			fmt.Fprint(out, offset)
			out.Flush()
		}
	}
}

func deltas(a []int, m int) []int {
	b := make([]int, len(a))
	for i := 1; i < len(a); i++ {
		b[i-1] = a[i] - a[i-1]
	}
	b[len(b)-1] = -(a[len(a)-1] + m - a[0])
	return b
}

func check2(a, b []int, start int) bool {
	i := start
	for j := 0; j < len(a); j++ {
		if a[i] != b[j] {
			return false
		}

		i = (i + 1) % len(a)
	}
	return true
}


package main

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

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

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

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

func main() {
	defer writer.Flush()
	var T, n int
	Fscan(reader, &T)
	for cas := 1; cas <= T; cas++ {
		Fscan(reader, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			Fscan(reader, &a[i])
		}
		ok := false
		for o := 0; o < 2; o++ {
			g := a[o]
			for i := o; i < n; i += 2 {
				g = gcd(g, a[i])
			}
			ok = true
			for i := o ^ 1; i < n; i += 2 {
				if a[i]%g == 0 {
					ok = false
					break
				}
			}
			if ok {
				Fprintln(writer, g)
				break
			}
		}
		if !ok {
			Fprintln(writer, 0)
		}
	}
}

package main

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

func gcd(a, b uint64) uint64 {
	if a == 0 {
		return b
	}
	return gcd(b%a, a)
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for t > 0 {
		t--
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		numbers := make([]uint64, m)
		for i, s := range strings.Fields(scanner.Text()) {
			n, _ := strconv.ParseUint(s, 10, 64)
			numbers[i] = n
		}

		var ans uint64 = 0
		for i := 0; i < 2; i++ {
			d := numbers[i]
			for j := i + 2; j < m; j += 2 {
				d = gcd(d, numbers[j])
			}
			if d == 1 {
				continue
			}
			ans = d
			for j := 1 - i; j < m; j += 2 {
				if numbers[j]%d == 0 {
					ans = 0
					break
				}
			}
			if ans != 0 {
				break
			}
		}

		fmt.Println(ans)
	}
}

package main

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

// GCD of two numbers
func GCD(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return GCD(b, a%b)
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N := 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		n, o, p := 0, []int64{}, int64(0)
		fmt.Fscan(f, &n)
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &p)
			o = append(o, p)
		}
		a, b, ok1, ok2 := o[0], o[1], true, true
		for i := 0; i < n; i += 2 {
			a = GCD(a, o[i])
		}
		for i := 1; i < n; i += 2 {
			b = GCD(b, o[i])
		}
		for i := 0; i < n; i += 2 {
			if o[i]%b == 0 {
				ok1 = false
			}
		}
		for i := 1; i < n; i += 2 {
			if o[i]%a == 0 {
				ok2 = false
			}
		}
		if !ok1 && !ok2 {
			fmt.Fprintln(w, 0)
		} else {
			if ok1 {
				fmt.Fprintln(w, b)
			} else {
				fmt.Fprintln(w, a)
			}
		}
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		nums := make([]int64, n)
		var d1, d2 int64

		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)
			nums[i] = v

			if i%2 == 0 {
				d1 = gcd(d1, v)
			} else {
				d2 = gcd(d2, v)
			}
		}

		d := d1
		for i := 1; i < n; i += 2 {
			if nums[i]%d1 == 0 {
				d = 0
				break
			}
		}

		if d == 0 {
			d = d2
			for i := 0; i < n; i += 2 {
				if nums[i]%d2 == 0 {
					d = 0
					break
				}
			}
		}

		fmt.Fprintln(out, d)
	}
}

package main

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

/*
2
2
8 7
2
4 4
---
8
0
*/

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	bld := strings.Builder{}

	var t = 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		var n int64
		scanf("%d\n", &n)

		a := make([]int64, n)

		for i := int64(0); i < n; i++ {
			scanf("%d", &a[i])
		}
		scanf("\n")

		gcdEven := a[0]
		gcdOdd := a[1]

		for i := int64(0); i < n; i++ {
			if i%2 == 0 {
				gcdEven = gcd(gcdEven, a[i])
			} else {
				gcdOdd = gcd(gcdOdd, a[i])
			}
		}

		d := gcdEven

		for i := int64(0); i < n; i++ {
			if i%2 == 1 && a[i]%d == 0 {
				d = 0
				break
			}
		}

		if d == 0 {
			d = gcdOdd
			for i := int64(0); i < n; i++ {
				if i%2 == 0 && a[i]%d == 0 {
					d = 0
					break
				}
			}
		}

		fmt.Fprintln(&bld, d)
	}

	printf("%s", bld.String())
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

//goland:noinspection GoUnusedType
type pair struct {
	x int64
	y int64
}

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

//goland:noinspection GoUnusedFunction
func printf(f string, a ...interface{}) {
	if _, err := fmt.Fprintf(writer, f, a...); err != nil {
		panic(err)
	}
}

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func max(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

//goland:noinspection GoUnusedFunction
func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

//goland:noinspection GoUnusedFunction
func isPrime(n int) bool {
	if n == 2 || n == 3 {
		return true
	}

	if n <= 1 || n%2 == 0 || n%3 == 0 {
		return false
	}

	for i := 5; i*i <= n; i += 6 {
		if n%i == 0 || n%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
//func lcm(a, b int) int {
//	return abs(a) / gcd(a, b) * abs(b)
//}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

// myapp project main.go
package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t int
	fmt.Fscan(r, &t)
	for i := 0; i < t; i++ {
		var n int
		fmt.Fscan(r, &n)
		a := make([]int64, n/2+(n%2))
		b := make([]int64, n/2)
		for j := 0; j < n; j++ {
			if j%2 == 0 {
				fmt.Fscan(r, &a[j/2])
			} else {
				fmt.Fscan(r, &b[j/2])
			}
		}
		ga := a[0]
		for _, x := range a {
			ga = gcd(ga, x)
		}
		gb := b[0]
		for _, x := range b {
			gb = gcd(gb, x)
		}
		fa := true
		fb := true
		for _, x := range a {
			if x%gb == 0 {
				fa = false
				break
			}
		}
		for _, x := range b {
			if x%ga == 0 {
				fb = false
				break
			}
		}
		var ans int64
		if fa {
			ans = gb
		} else if fb {
			ans = ga
		}

		fmt.Fprintln(w, ans)
	}
}
func gcd(a int64, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a

}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func gcd(x, y int64) int64 {
	if y > x {
		return gcd(y, x)
	}
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

func solve(n int, a []int64) int64 {
	g := [2]int64{a[0], a[1]}
	for i := 2; i < n; i++ {
		g[i%2] = gcd(g[i%2], a[i])
	}
	good := [2]bool{true, true}
	for i := 0; i < n; i++ {
		good[1-i%2] = good[1-i%2] && a[i]%g[1-i%2] > 0
	}
	for i := range good {
		if good[i] {
			return g[i]
		}
	}
	return 0
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int64, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1618C(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int64, n)
		g := [2]int64{}
		for i := range a {
			Fscan(in, &a[i])
			g[i&1] = gcd(g[i&1], a[i])
		}
		ok := [2]bool{true, true}
		for i, v := range a {
			ok[i&1] = ok[i&1] && v%g[i&1^1] > 0
		}
		if ok[0] {
			Fprintln(out, g[1])
		} else if ok[1] {
			Fprintln(out, g[0])
		} else {
			Fprintln(out, 0)
		}
	}
}

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

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

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for t > 0 {
		t--
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		numbers := make([]uint64, m)
		candidate := [2]uint64{math.MaxUint64, math.MaxUint64}
		for i, s := range strings.Fields(scanner.Text()) {
			n, _ := strconv.ParseUint(s, 10, 64)
			numbers[i] = n
			if i%2 == 0 {
				if candidate[0] > n {
					candidate[0] = n
				}
			} else {
				if candidate[1] > n {
					candidate[1] = n
				}
			}
		}

		var ans uint64 = 0
		for x, d := range candidate {
			qualified := true
			for i := x; i < m; i += 2 {
				if numbers[i]%d != 0 {
					qualified = false
					break
				}
			}
			if !qualified {
				continue
			}

			ans = d
			divided := numbers[0]%d == 0
			for i := 1; i < m; i++ {
				res := numbers[i]%d == 0
				if divided == (numbers[i]%d == 0) {
					ans = 0
					break
				}
				divided = res
			}
			if ans != 0 {
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for t > 0 {
		t--
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		numbers := make([]uint64, m)
		candidate := [2]uint64{math.MaxUint64, math.MaxUint64}
		for i, s := range strings.Fields(scanner.Text()) {
			n, _ := strconv.ParseUint(s, 10, 64)
			numbers[i] = n
			if i%2 == 0 {
				if candidate[0] > n {
					candidate[0] = n
				}
			} else {
				if candidate[1] > n {
					candidate[1] = n
				}
			}
		}

		var ans uint64 = 0
		for x, d := range candidate {
			qualified := true
			for i := x; i < m; i += 2 {
				if numbers[i]%d != 0 {
					qualified = false
					break
				}
			}
			if !qualified {
				break
			}

			ans = d
			divided := numbers[0]%d == 0
			for i := 1; i < m; i++ {
				res := numbers[i]%d == 0
				if divided == (numbers[i]%d == 0) {
					ans = 0
					break
				}
				divided = res
			}
			if ans != 0 {
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for t > 0 {
		t--
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		numbers := make([]uint64, m)
		candidate := [2]uint64{math.MaxUint64, math.MaxUint64}
		for i, s := range strings.Fields(scanner.Text()) {
			n, _ := strconv.ParseUint(s, 10, 64)
			numbers[i] = n
			if i%2 == 0 {
				if candidate[0] > n {
					candidate[0] = n
				}
			} else {
				if candidate[1] > n {
					candidate[1] = n
				}
			}
		}

		var ans uint64 = 0
		for _, d := range candidate {
			ans = d
			divided := numbers[0]%d == 0
			for i := 1; i < m; i++ {
				res := numbers[i]%d == 0
				if divided == (numbers[i]%d == 0) {
					ans = 0
					break
				}
				divided = res
			}
			if ans != 0 {
				break
			}
		}
		fmt.Println(ans)
	}
}

package main

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

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

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

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		nums := make([]int64, n)
		var d1, d2 int64

		for i := 0; i < n; i++ {
			var v int64
			fmt.Fscan(in, &v)
			nums[i] = v

			if i%2 == 0 {
				d1 = gcd(d1, v)
			} else if i%2 == 1 {
				d2 = gcd(d2, v)
			}
		}

		var d int64
		if d1 >= d2 {
			d = d1
			for i := 1; i < n; i += 2 {
				if nums[i]%d1 == 0 {
					d = 0
					break
				}
			}
		} else {
			d = d2
			for i := 0; i < n; i += 2 {
				if nums[i]%d2 == 0 {
					d = 0
					break
				}
			}
		}

		fmt.Fprintln(out, d)
	}
}


package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	solve := func(Case int) {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		x, y := 0, 0
		for i := range a {
			if a[i] == 1 {
				x = i + 1
			}
			if a[i] == n {
				y = i + 1
			}
		}
		if y < x {
			x, y = y, x
		}
		fmt.Fprintln(out, min([]int{x + n - y + 1, y, n - x + 1}))
	}

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

func max(x, y int64) int64 {
	if x < y {
		return y
	}
	return x
}

func min(a []int) int {
	mn := int(1e9)
	for i := range a {
		if a[i] < mn {
			mn = a[i]
		}
	}
	return mn
}
func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func reverse(s []byte) []byte {
	a := []byte(s)
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
	return a
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		list:= make([]int,n)
		var small int = 999999999999
		var big,small_key,big_key int
		for j:=0;j<n;j++{
			fmt.Scan(&list[j])
			if list[j] > big {
				big=list[j]
				big_key=j+1
			}
			if list[j] < small {
				small=list[j]
				small_key=j+1
			}
		}
		a:= min(small_key,big_key)+min(n-(small_key-1),n-(big_key-1))
		//b:=
		c:= max(small_key,big_key)
		d:= max(n-(small_key-1),n-(big_key-1))

		fmt.Println(smallest_of_three(a,c,d))
	}
}

func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}
func max (x,y int) int {
	if x>y{
		return x
	} else {
		return y
	}
}
func min (x,y int) int {
	if x>y{
		return y
	} else {
		return x
	}
}
func smallest_of_three(x,y,z int) int {
	if x <= y {
		if x <= z {
			return x
		}
	} else {
		if y <= z {
			return y
		}
	}
	return z
}

package main

import (
  "fmt"
  "errors"
)

func findElement(arr []int, comparator func(int, int) bool) (int, error) {
  n := len(arr)

  if (n == 0) {
    return -1, errors.New("empty slice")
  }

  key, value := 0, arr[0]
  for i := 1; i < n; i++ {
    if (comparator(arr[i], value)) {
      key, value = i, arr[i]
    }
  }

  return key, nil
}

func minElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a < b
  })
}

func maxElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a > b
  })
}

func min(num int, nums ...int) int {
  nums = append(nums, num)
  i, _ := minElement(nums)
  return nums[i]
}

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

  for ; t > 0; t-- {
    var n int
    fmt.Scan(&n)

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

    left, _ := minElement(a)
    right, _ := maxElement(a)

    if (left > right) {
      left, right = right, left
    }

    ans := min(right + 1, n - left, (left + 1) + (n - right))
    fmt.Println(ans)
  }
}

package main

import (
  "fmt"
  "errors"
)

func findElement(arr []int, comparator func(int, int) bool) (int, error) {
  n := len(arr)

  if (n == 0) {
    return -1, errors.New("empty slice")
  }

  index, min := 0, arr[0]
  for i := 1; i < n; i++ {
    if (comparator(arr[i], min)) {
      index, min = i, arr[i]
    }
  }

  return index, nil
}

func minElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a < b
  })
}

func maxElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a > b
  })
}

func min(num int, nums ...int) int {
  nums = append(nums, num)
  i, _ := minElement(nums)
  return nums[i]
}

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

  for ; t > 0; t-- {
    var n int
    fmt.Scan(&n)

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

    left, _ := minElement(a)
    right, _ := maxElement(a)

    if (left > right) {
      left, right = right, left
    }

    ans := min(right + 1, n - left, (left + 1) + (n - right))
    fmt.Println(ans)
  }
}

package main

import (
  "fmt"
  "errors"
)

func findElement(arr []int, comparator func(int, int) bool) (int, error) {
  n := len(arr)

  if (n == 0) {
    return -1, errors.New("empty slice")
  }

  index, min := 0, arr[0]
  for i := 1; i < n; i++ {
    if (comparator(arr[i], min)) {
      index, min = i, arr[i]
    }
  }

  return index, nil
}

func minElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a < b
  })
}

func maxElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a > b
  })
}

func min(num int, nums ...int) int {
  nums = append(nums, num)
  i, err := minElement(nums)
  if err != nil {
    return num
  }

  return nums[i]
}

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

  for ; t > 0; t-- {
    var n int
    fmt.Scan(&n)

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

    left, _ := minElement(a)
    right, _ := maxElement(a)

    if (left > right) {
      left, right = right, left
    }

    ans := min(right + 1, n - left, (left + 1) + (n - right))
    fmt.Println(ans)
  }
}

package main

import (
  "fmt"
  "errors"
)

func findElement(arr []int, comparator func(int, int) bool) (int, error) {
  n := len(arr)

  if (n == 0) {
    return -1, errors.New("empty slice")
  }

  i, min := 0, arr[0]
  for j := 1; j < n; j++ {
    if (comparator(arr[j], min)) {
      i, min = j, arr[j]
    }
  }

  return i, nil
}

func minElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a < b
  })
}

func maxElement(arr []int) (int, error) {
  return findElement(arr, func(a int, b int) bool {
    return a > b
  })
}

func min(num int, nums ...int) int {
  nums = append(nums, num)
  i, err := minElement(nums)
  if err != nil {
    return num
  }

  return nums[i]
}

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

  for ; t > 0; t-- {
    var n int
    fmt.Scan(&n)

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

    left, _ := minElement(a)
    right, _ := maxElement(a)

    if (left > right) {
      left, right = right, left
    }

    ans := min(right + 1, n - left, (left + 1) + (n - right))
    fmt.Println(ans)
  }
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		pos := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			a[i]--
			pos[a[i]] = i
		}
		v,w := pos[0],pos[n-1]
		if v > w {
			v, w = w, v
		}
		res := v+1+n-w
		res = min(res, w+1)
		res = min(res, n-v)
		Fprintln(out, res)
	}

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

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

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	min := func(a, b int) int {
		if a < b {
			return a
		}
		return b
	}
	solve := func(Case int) {
		var n int
		Fscan(in, &n)
		a := make([]int, n)
		pos := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			a[i]--
			pos[a[i]] = i
		}
		v,w := pos[0],pos[n-1]
		if v > w {
			v, w = w, v
		}
		res := v+1+n-w
		res = min(res, w+1)
		res = min(res, n-v)
		Fprintln(out, res)
	}

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

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

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

package main

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

func init() {
	log.SetFlags(log.Lshortfile)
}

func parse(s *bufio.Scanner) []int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())

	s.Scan()
	ss := strings.Split(s.Text(), " ")

	arr := make([]int, n)
	for i, el := range ss {
		num, _ := strconv.Atoi(el)
		arr[i] = num
	}

	return arr
}

func playGame(arr []int) int {
	minIx, _ := find(arr, minCmp)
	maxIx, _ := find(arr, maxCmp)

	if minIx > maxIx {
		minIx, maxIx = maxIx, minIx
	}

	return Min(maxIx+1, len(arr)-minIx, minIx+1+len(arr)-maxIx)
}

func minCmp(x, y int) bool {
	return x < y
}

func maxCmp(x, y int) bool {
	return x > y
}

func find(arr []int, comp func(i, j int) bool) (int, int) {
	value, ix, start := arr[0], 0, 1
	for i, el := range arr[start:] {
		if comp(el, value) {
			value = el
			ix = i + start
		}
	}

	return ix, value
}

// Min returns the smallest argument
func Min(a ...int) int {
	curr := a[0]

	for _, el := range a {

		if el < curr {
			curr = el
		}
	}

	return curr
}

func main() {
	s := bufio.NewScanner(os.Stdin)

	s.Scan()
	T, _ := strconv.Atoi(s.Text())

	for test := 0; test < T; test++ {
		arr := parse(s)
		minMoves := playGame(arr)
		fmt.Println(minMoves)
	}
}

package main

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

func init() {
	log.SetFlags(log.Lshortfile)
}

func parse(s *bufio.Scanner) []int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())

	s.Scan()
	ss := strings.Split(s.Text(), " ")

	arr := make([]int, n)
	for i, el := range ss {
		num, _ := strconv.Atoi(el)
		arr[i] = num
	}

	return arr
}

func playGame(arr []int) int {
	min, max := 101, 0
	minIx, maxIx := 0, 0

	for i, el := range arr {
		if el < min {
			min = el
			minIx = i
		}

		if el > max {
			max = el
			maxIx = i
		}
	}

	if minIx > maxIx {
		minIx, maxIx = maxIx, minIx
	}

	return Min(maxIx+1, len(arr)-minIx, minIx+1+len(arr)-maxIx)
}

// Min returns the smallest argument
func Min(a ...int) int {
	curr := a[0]

	for _, el := range a {

		if el < curr {
			curr = el
		}
	}

	return curr
}

func main() {
	s := bufio.NewScanner(os.Stdin)

	s.Scan()
	T, _ := strconv.Atoi(s.Text())

	for test := 0; test < T; test++ {
		arr := parse(s)
		minMoves := playGame(arr)
		fmt.Println(minMoves)
	}
}

package main

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

type CF1538A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1538A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1538A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1538A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1538A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1538A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
 Run solve the problem CF1538A
 Date: 18/08/21
 User: pepradere
 URL: https://codeforces.com/problemset/problem/1538/A
 Problem: CF1538A
**/
func (in *CF1538A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt()
		min, max := n+1, 0
		posMin, posMax := -1, -1
		for i := 1; i <= n; i++ {
			num := in.NextInt()
			if num < min {
				min = num
				posMin = i
			}
			if num > max {
				max = num
				posMax = i
			}
		}

		ans := 0

		if posMax < posMin {
			posMax, posMin = posMin, posMax
		}

		n++
		if posMin < n-posMax {
			ans += posMin
			if posMax-posMin < n-posMax {
				ans += posMax - posMin
			} else {
				ans += n - posMax
			}
		} else {
			ans += n - posMax
			if posMax-posMin < posMin {
				ans += posMax - posMin
			} else {
				ans += posMin
			}
		}

		fmt.Println(ans)

	}
}

func closeTo(a, b, c int) int {
	if b-a > b-c {
		return b - c
	}
	return b - a
}

func NewCF1538A(r *bufio.Reader) *CF1538A {
	return &CF1538A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1538A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		pos := make([]int, n + 1)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			pos[arr[i]] = i + 1
		}
		if pos[1] > pos[n] {
			pos[1], pos[n] = pos[n], pos[1]
		}
		ans := pos[n]
		ans = min(ans, pos[1] + n + 1 - pos[n])
		ans = min(ans, n + 1 - pos[1])
		fmt.Fprintln(out, ans)
		t--
	}
	out.Flush()

}

package main

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

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
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)

	for t > 0 {
		t--
		var n int
		max_val := 0
		min_val := 101
		max_pos := 0
		min_pos := 0

		fmt.Fscan(in, &n)
		arr := make([]int, n)

		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] > max_val {
				max_val = arr[i]
				max_pos = i
			}
			if arr[i] < min_val {
				min_val = arr[i]
				min_pos = i
			}

		}

		// all from left side
		ans := max(max_pos, min_pos) + 1

		// all from right side
		ans = min(ans, n-min(max_pos, min_pos))

		// from left and right with alt vals
		ans = min(ans, max_pos+1+(n-min_pos))
		ans = min(ans, min_pos+1+(n-max_pos))

		fmt.Fprintln(out, ans)
	}

	out.Flush()

}

package main

import (
	"fmt"
	"os"
)

var ins = os.Stdin
var out = os.Stdout

func scanf(s string, a ...interface{}) (int, error)  { return fmt.Fscanf(ins, s, a...) }
func scan(a ...interface{}) (int, error)             { return fmt.Fscan(ins, a...) }
func printf(s string, a ...interface{}) (int, error) { return fmt.Fscanf(out, s, a...) }

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

func main() {
	var l int
	var n int
	scan(&n)
	for j := 0; j < n; j++ {
		l=0
		scan(&l)
		m := make([]int, l)
		for i := 0; i < l; i++ {
			scan(&m[i])
		}
		fmt.Printf("%d\n", fn(m))
	}
}
func fn(a []int) (res int){
	var imax, imin,max int
	min:=101
	for i:=0; i<len(a); i++{
		if a[i] < min{
			min = a[i]
			imin = i
		}
		if a[i] > max{
			max = a[i]
			imax = i
		}

	}
	imax++
	imin++

	left := maximum(imax, imin)
	right := maximum(len(a) - imax + 1, len(a) - imin + 1)
	bouth := minimum(imin+(len(a)-imax+1), imax+len(a)-imin+1)

	return min3(left, right, bouth)
}

func maximum(a int , b int)int{
	if a>b{
		return a
	}
	return b
}
func minimum(a int , b int)int{
	if a<b{
		return a
	}
	return b
}

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

func min3(a int , b int , c int) (res int){
	if a < b{res = a}else {res = b}
	if c < res {res = c}
	return res
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scan(&t)
	for t > 0 {
		solve()
		t--
	}
}
func solve() {
	var n int
	fmt.Scan(&n)
	minIndex, maxIndex := -1, -1
	for i := 0; i < n; i++ {
		var t int
		fmt.Scan(&t)
		if t == 1 {
			minIndex = i
		} else if t == n {
			maxIndex = i
		}
	}
	minIndex++
	maxIndex++
	i, j := min(minIndex, maxIndex), max(minIndex, maxIndex)
	ans := min(j, min(n-i+1, i+n-j+1))
	fmt.Println(ans)
}
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
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

package main

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

func Min(arg ...int) int {
	m := arg[0]
	for _, v := range arg {
		if v < m {
			m = v
		}
	}
	return m
}

func Max(arg ...int) int {
	m := arg[0]
	for _, v := range arg {
		if v > m {
			m = v
		}
	}
	return m
}

func solve() {
	f := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	N, n := 0, 0
	fmt.Fscan(f, &N)
	for i := 0; i < N; i++ {
		mn, mx, lmn, lmx, rmn, rmx, ele := 1000, -1000, 0, 0, 0, 0, 0
		fmt.Fscan(f, &n)
		for j := 0; j < n; j++ {
			fmt.Fscan(f, &ele)
			if ele > mx {
				mx, lmx = ele, j+1
			}
			if ele < mn {
				mn, lmn = ele, j+1
			}
		}
		rmx, rmn = n-lmx+1, n-lmn+1
		fmt.Fprintln(w, Min(Max(lmx, lmn), lmx+rmn, rmx+lmn, Max(rmx, rmn)))
	}
	w.Flush()
}

func main() {
	solve()
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int, a []int) int {
	var pmax, pmin, x1, x2, x3 int
	for i, x := range a {
		if x > a[pmax] {
			pmax = i
		}
		if x < a[pmin] {
			pmin = i
		}
	}
	if pmin > pmax {
		pmin, pmax = pmax, pmin
	}
	x1 = pmax + 1
	x2 = n - pmin
	x3 = pmin + 1 + n - pmax
	if x2 < x1 {
		x1 = x2
	}
	if x3 < x1 {
		x1 = x3
	}
	return x1
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &a[i])
		}
		fmt.Println(solve(n, a))
	}
}

package main

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

func run(r io.Reader, w io.Writer) {
	in := bufio.NewReader(r)
	out := bufio.NewWriter(w)
	defer out.Flush()

	type pair struct {
		x, y int
	}
	var t int
	for Fscan(in, &t); t > 0; t-- {
		var n int
		Fscan(in, &n)
		stones := make([]int, n)

		greatest, a, smallest, b := 0, -1, int(1e9), -1
		for i := range stones {
			Fscan(in, &stones[i])
			if greatest < stones[i] {
				greatest, a = stones[i], i
			}
			if smallest > stones[i] {
				smallest, b = stones[i], i
			}
		}

		ans := min(a+1, n-a) + min(b+1, n-b)
		ans = min(ans, max(a, b)+1)
		ans = min(ans, n - min(a, b))
		Fprintln(out, ans)
	}
}

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

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

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

package main

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

func minimum(a int32, b int32) int32 {
	if a > b {
		return b
	} else {
		return a
	}
}

func maximum(a int32, b int32) int32 {
	if a < b {
		return b
	} else {
		return a
	}
}

func solve(n int32, k []int32) int32 {
	leastPosition := int32(0)
	greatestPosition := int32(0)
	least := k[0]
	greatest := k[0]
	for i := int32(1); i < n; i++ {
		if k[i] < least {
			least = k[i]
			leastPosition = i
		}
		if k[i] > greatest {
			greatest = k[i]
			greatestPosition = i
		}
	}
	leftmost := minimum(leastPosition, greatestPosition)
	rightmost := maximum(leastPosition, greatestPosition)
	left := leftmost + 1
	right := n - rightmost
	middle := rightmost - leftmost
	output1 := left + middle
	output2 := left + right
	output3 := middle + right
	return minimum(minimum(output1, output2), output3)
}

func arrayScanf(in *bufio.Reader, n int32, arr *[]int32) {
	m := int32(0)
	for i := int32(0); i < n; i++ {
		if i > 0 {
			fmt.Fscanf(in, " ")
		}
		fmt.Fscanf(in, "%d", &m)
		*arr = append(*arr, m)
	}
	fmt.Fscanf(in, "\n")
}

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

	tt := int32(0)
	fmt.Fscanf(in, "%d\n", &tt)
	for tt > 0 {
		n := int32(0)
		arr := []int32{}
		fmt.Fscanf(in, "%d\n", &n)
		arrayScanf(in, n, &arr)

		fmt.Fprintf(out, "%d\n", solve(n, arr))
		out.Flush()
		tt--
	}
}

package main

import "fmt"

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func solve() {
	n, a := 0, [101]int{0}
	fmt.Scan(&n)
	for i := 0; i < n; i ++ {
		fmt.Scan(&a[i])
	}
	max, p1 := 0, -1
	for i := 0; i < n; i ++ {
		if a[i] > max {
			max, p1 = a[i], i
		}
	}
	min, p2 := 101, -1
	for i := 0; i < n; i ++ {
		if a[i] < min {
			min, p2 = a[i], i
		}
	}
	if p1 > p2 {
		tmp := p1
		p1 = p2
		p2 = tmp
	}
	t1, t2, t3 := p1 + 1, p2 - p1, n - p2
	if t1 > t2 {
		tmp := t1
		t1 = t2
		t2 = tmp
	}
	if t2 > t3 {
		tmp := t2
		t2 = t3
		t3 = tmp
	}
	fmt.Println(t1 + t2)
}
func main() {
	t := 0
	fmt.Scan(&t)
	for i := 0; i < t; i ++ {
		solve()
	}
}

package main

import "fmt"

const Mod = 1000000007

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

	for tc := 1; tc <= t; tc++ {
		solveTest(tc)
	}
}

func solveTest(tc int) {
	var n int
	fmt.Scan(&n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	x, y := 0, 0
	for i := 1; i < n; i++ {
		if a[i] < a[x] {
			x = i
		}
		if a[i] > a[y] {
			y = i
		}
	}

	fmt.Println(min(max(x + 1, y + 1), n - min(x, y), min(x, y) + 1 + n - max(x, y)))
}

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

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

package main

import (
	"fmt"
)

func main() {
	var ts int
	fmt.Scan(&ts)
	for i := 0; i < ts; i++ {
		var n int
		fmt.Scan(&n)
		arr := make([]int, n)
		maxPos := 0
		minPos := 0
		max := -1
		min := 200
		for idx := 0; idx < n; idx++ {
			fmt.Scan(&arr[idx])
			if max < arr[idx] {
				maxPos = idx + 1
				max = arr[idx]
			}
			if min > arr[idx] {
				minPos = idx + 1
				min = arr[idx]
			}
		}
		// fmt.Println(Min(maxPos, minPos))
		minCanddt := Min(minPos, n-minPos+1)
		maxCanddt := Min(maxPos, n-maxPos+1)
		if Max(maxPos, minPos) != maxPos {
			maxPos, minPos = minPos, maxPos
		}
		fmt.Println(Min(minCanddt+maxCanddt, Min(maxPos, n-minPos+1)))
	}
}

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

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

package main

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

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

	var t int
	fmt.Fscan(reader, &t)

	var n, num int
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		min, max := 1<<31-1, -1<<31
		iMin, iMax := -1, -1
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &num)
			if num < min {
				if iMin != -1 && iMax == -1 {
					max, iMax = min, iMin
				}
				min, iMin = num, i
			} else if num > max {
				if iMin == -1 && iMax != -1 {
					min, iMin = max, iMax
				}
				max, iMax = num, i
			}
		}
		l, r := Min(iMin, iMax), Max(iMin, iMax)
		out := Min(Min(r+1, n-l), l+1+n-r)
		fmt.Fprintln(writer, out)
	}
}

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
}

package main

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

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

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

func solve(arr []int, n int) int {
	min, max := 100, 0
	minIdx, maxIdx := 0, 0
	var ans int

	for i, n := range arr {
		if min > n {
			min = n
			minIdx = i
		}
		if max < n {
			max = n
			maxIdx = i
		}
	}
	ans = maxValue(minIdx, maxIdx) + 1
	ans = minValue(ans, n-minValue(minIdx, maxIdx))
	ans = minValue(ans, minValue(minIdx, maxIdx)+1+n-maxValue(minIdx, maxIdx))

	return ans
}

func main() {
	var t int
	var n int
	in := bufio.NewReader(os.Stdin)

	fmt.Fscan(in, &t)

	for ; t > 0; t-- {
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
		}
		fmt.Println(solve(arr, n))
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })

func MIN(x, y int64) int64 {
	if x > y {
		return y
	} else {
		return x
	}
}

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

func MAX(x, y int64) int64 {
	if x > y {
		return x
	} else {
		return y
	}
}

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m
}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}
}

func PrimeFactorization(n int) (pfs map[int]int) {
	pfs = make(map[int]int)

	// Get the number of 2s that divide n
	for n%2 == 0 {
		if _, ok := pfs[2]; ok {
			pfs[2] += 1
		} else {
			pfs[2] = 1
		}
		n = n / 2
	}

	// n must be odd at this point. so we can skip one element
	// (note i = i + 2)
	for i := 3; i*i <= n; i = i + 2 {
		// while i divides n, append i and divide n
		for n%i == 0 {
			if _, ok := pfs[i]; ok {
				pfs[i] += 1
			} else {
				pfs[i] = 1
			}
			n = n / i
		}
	}

	// This condition is to handle the case when n is a prime number
	// greater than 2
	if n > 2 {
		pfs[n] = 1
	}

	return
}

func NumberOfDivisors(n int) int {
	pfs := PrimeFactorization(n)

	num := 1
	for _, exponents := range pfs {
		num *= (exponents + 1)
	}

	return num
}

func KthPrime(x int) (y int) {
	if x == 1 {
		y = 2
	}
	if x == 2 {
		y = 3
	}
	if x > 2 {
		var sum int = 2
		begin := 5
		for sum < x+1 {
			if IsItPrime(begin) == true {
				sum++
			}
			if sum == x {
				y = begin
				break
			} else {
				if begin%6 == 1 {
					begin = begin + 4
				} else {
					begin = begin + 2
				}
			}
		}
	}
	return
}

var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for o := 0; o < t; o++ {
		var n int
		fmt.Fscan(input, &n)
		var biggest, smallest int
		var bigind, smallind int
		smallest = 101
		biggest = 0
		for i := 1; i < n+1; i++ {
			var m int
			fmt.Fscan(input, &m)
			if m < smallest {
				smallest = m
				smallind = i
			}
			if m > biggest {
				biggest = m
				bigind = i
			}

		}
		Max := MAX(int64(bigind), int64(smallind))
		Min := MIN(int64(bigind), int64(smallind))

		var fir, sec, thr int64
		fir = Max
		sec = int64(n) - Min + 1
		thr = int64(n) - Max + Min + 1
		fmt.Println(MIN(fir, MIN(sec, thr)))
	}

}

package main

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

const (
	MOD int = int(1e9 + 7)
)

type Int int64

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

	var t, n int
	fmt.Fscan(_r, &t)

	for tc := 0; tc < t; tc++ {
		fmt.Fscan(_r, &n)
		a := make([]Int, n)
		var l, r Int
		for i := 0; i < n; i++ {
			fmt.Fscan(_r, &a[i])
			if a[i] == Int(1) {
				l = Int(i)
			}
			if a[i] == Int(n) {
				r = Int(i)
			}
		}
		if l > r {
			l, r = r, l
		}
		var ans Int = l - r + Int(n+1)
		if ans > r+Int(1) {
			ans = r + Int(1)
		}
		if ans > Int(n)-l {
			ans = Int(n) - l
		}
		fmt.Fprintf(_w, "%d\n", ans)
	}
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

func init() {
	log.SetFlags(0)
	log.SetOutput(ioutil.Discard)

}

func corners(arr []int) (int, int) {
	min, max := arr[0], arr[0]
	minIndex, maxIndex := 0, 0
	for i, v := range arr {
		if v < min {
			minIndex = i
			min = v
		}
		if v > max {
			maxIndex = i
			max = v
		}
	}

	left, right := minIndex, maxIndex
	if left > right {
		left, right = right, left
	}
	return left, right
}

func min(arr ...int) int {
	min := arr[0]
	for _, v := range arr {
		if v < min {
			min = v
		}
	}
	return min
}

func solve(arr []int) int {
	log.Printf("arr = %+v\n", arr)
	left, right := corners(arr)
	log.Printf("arr[:left] = %+v\n", arr[:left])
	log.Printf("arr[:right] = %+v\n", arr[:right])

	log.Printf("right+1 = %+v\n", right+1)
	log.Printf("len(arr)-left = %+v\n", len(arr)-left)
	log.Printf("len(arr)-(right-left) = %+v\n", len(arr)-(right-left))

	// log.Printf("minmax(arr) = %+v\n", minmax(arr))
	return min(
		right+1,
		len(arr)-left,
		len(arr)-(right-left)+1,
	)
}

func main() {
	log.Println("start")

	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	num := 0
	fmt.Fscan(in, &num)
	for i := 0; i < num; i++ {
		size := 0
		fmt.Fscan(in, &size)
		arr := []int{}
		for i := 0; i < size; i++ {
			val := 0
			fmt.Fscan(in, &val)
			arr = append(arr, val)
		}
		fmt.Fprintf(out, "%d\n", solve(arr))

	}
	log.Println("__end__")
}

package main

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

func main() {

	r := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(r, &t)

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

		var n int
		fmt.Fscan(r, &n)

		a := make([]int, n)
		for j := 0; j < n; j++ {
			fmt.Fscan(r, &a[j])
		}

		fmt.Println(solve(a, n))

	}
}

func solve(a[]int, n int) int {
	min, max := 101, 0
	var minIdx int
	var maxIdx int

	for i := 0; i < n; i++ {
		if a[i] < min {
			min = a[i]
			minIdx = i
		}
		if a[i] > max {
			max = a[i]
			maxIdx = i
		}
	}

	var left int
	var right int

	if minIdx < maxIdx {
		left = minIdx
		right = maxIdx
	} else {
		left = maxIdx
		right = minIdx
	}

	return minAB((left + 1 + n - right), minAB(right + 1, n - left))
}

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

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		list:= make([]int,n)
		var small int = 999999999999
		var big,small_key,big_key int
		for j:=0;j<n;j++{
			fmt.Scan(&list[j])
			if list[j] > big {
				big=list[j]
				big_key=j+1
			}
			if list[j] < small {
				small=list[j]
				small_key=j+1
			}
		}
		if n%2 == 0 {
			if abs(big_key-small_key) >= n/2  {
				if big_key <= n/2 {
					fmt.Println(big_key+(n-(small_key-1)))
				} else if big_key > n/2 {
					fmt.Println(small_key+(n-(big_key-1)))
				}
			} else {
				if min(big_key,small_key) <= n/2 {
					fmt.Println(max(big_key,small_key))
				} else {
					fmt.Println(n-(min(big_key,small_key)-1))
				}
			}
		} else {
			if abs(big_key-small_key) > n/2  {
				if big_key <= n/2 {
					fmt.Println(big_key+(n-(small_key-1)))
				} else if big_key > n/2 {
					fmt.Println(small_key+(n-(big_key-1)))
				}
			} else {
				if min(big_key,small_key) <= n/2 {
					fmt.Println(max(big_key,small_key))
				} else {
					fmt.Println(n-(min(big_key,small_key)-1))
				}
			}
		}
	}
}

func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}
func max (x,y int) int {
	if x>y{
		return x
	} else {
		return y
	}
}
func min (x,y int) int {
	if x>y{
		return y
	} else {
		return x
	}
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		list:= make([]int,n)
		var small int = 999999999999
		var big,small_key,big_key int
		for j:=0;j<n;j++{
			fmt.Scan(&list[j])
			if list[j] > big {
				big=list[j]
				big_key=j+1
			}
			if list[j] < small {
				small=list[j]
				small_key=j+1
			}
		}
		if abs(big_key-small_key) >= n/2  {
			if big_key <= n/2 {
				fmt.Println(big_key+(n-(small_key-1)))
			} else if big_key > n/2 {
				fmt.Println(small_key+(n-(big_key-1)))
			}
		} else {
			if min(big_key,small_key) <= n/2 {
				fmt.Println(max(big_key,small_key))
			} else {
				fmt.Println(n-(min(big_key,small_key)-1))
			}
		}
	}
}

func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}
func max (x,y int) int {
	if x>y{
		return x
	} else {
		return y
	}
}
func min (x,y int) int {
	if x>y{
		return y
	} else {
		return x
	}
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		largest:= 0
		smallest:= 101
		var largest_key,smallest_key int
		for j:=0;j<n;j++{
			var x int
			fmt.Scan(&x)
			if x > largest {
				largest= x
				largest_key= j
			}
			if x < smallest {
				smallest= x
				smallest_key= j
			}
		}
		if abs(smallest_key-largest_key) > n/2 {
			if smallest_key>largest_key {
				fmt.Println(largest_key+1+(n-smallest_key))
			} else {
				fmt.Println(smallest_key+1+(n-largest_key))
			}
		} else {
			if smallest_key>largest_key{
				if smallest_key+1<n-largest_key{
					fmt.Println(smallest_key+1)
				} else {
					fmt.Println(n-largest_key)
				}
			} else {
				if largest_key+1<n-smallest_key {
					fmt.Println(largest_key+1)
				} else {
					fmt.Println(n-smallest_key)
				}
			}
		}
	}
}

func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		largest:= 0
		smallest:= 101
		var largest_key,smallest_key int
		for j:=0;j<n;j++{
			var x int
			fmt.Scan(&x)
			if x > largest {
				largest= x
				largest_key= j
			}
			if x < smallest {
				smallest= x
				smallest_key= j
			}
		}
		if abs(smallest_key-largest_key) >= n/2 {
			if smallest_key>largest_key {
				fmt.Println(largest_key+1+(n-smallest_key))
			} else {
				fmt.Println(smallest_key+1+(n-largest_key))
			}
		} else {
			if smallest_key>largest_key{
				if smallest_key+1<n-largest_key{
					fmt.Println(smallest_key+1)
				} else {
					fmt.Println(n-largest_key)
				}
			} else {
				if largest_key+1<n-smallest_key {
					fmt.Println(largest_key+1)
				} else {
					fmt.Println(n-smallest_key)
				}
			}
		}
	}
}

func abs(x int) int {
	var result int
	if x > 0 {
		result = x
	} else {
		if x < 0 {
			result = x*-1
		}
	}
	return result
}

package main
import(
    "fmt"
)
func main() {
	var t int
	fmt.Scan(&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Scan(&n)
		largest:= 0
		smallest:= 101
		var largest_key,smallest_key int
		for j:=0;j<n;j++{
			var x int
			fmt.Scan(&x)
			if x > largest {
				largest= x
				largest_key= j
			}
			if x < smallest {
				smallest= x
				smallest_key= j
			}
		}
		if largest_key <= n/2 && smallest_key <= n/2 {
			if largest_key > smallest_key {
				fmt.Println(largest_key+1)
			} else {
				fmt.Println(smallest_key+1)
			}
		} else if largest_key >= n/2 && smallest_key >= n/2{
			if n-largest_key > n-smallest_key {
				fmt.Println(n-largest_key)
			} else {
				fmt.Println(n-smallest_key)
			}
		} else {
			if largest_key > smallest_key{
				fmt.Println((n-largest_key)+smallest_key+1)
			} else {
				fmt.Println((n-smallest_key)+largest_key+1)
			}
		}
	}
}

package main

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

func init() {
	log.SetFlags(log.Lshortfile)
}

func parse(s *bufio.Scanner) []int {
	s.Scan()
	n, _ := strconv.Atoi(s.Text())

	s.Scan()
	ss := strings.Split(s.Text(), " ")

	arr := make([]int, n)
	for i, el := range ss {
		num, _ := strconv.Atoi(el)
		arr[i] = num
	}

	return arr
}

func playGame(arr []int) int {
	min, max := 101, 0
	minIx, maxIx := 0, 0

	for i, el := range arr {
		if el < min {
			min = el
			minIx = i
		} else if el > max {
			max = el
			maxIx = i
		}
	}

	if minIx > maxIx {
		minIx, maxIx = maxIx, minIx
	}
	return Min(maxIx+1, len(arr)-minIx, minIx+1+len(arr)-maxIx)
}

// Min returns the smallest argument
func Min(a ...int) int {
	curr := a[0]
	for _, el := range a {
		if el < curr {
			curr = el
		}
	}

	return curr
}

func main() {
	s := bufio.NewScanner(os.Stdin)

	s.Scan()
	T, _ := strconv.Atoi(s.Text())

	for test := 0; test < T; test++ {
		arr := parse(s)
		minMoves := playGame(arr)
		fmt.Println(minMoves)
	}
}

package main

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

type CF1538A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1538A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1538A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1538A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1538A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1538A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
 Run solve the problem CF1538A
 Date: 18/08/21
 User: pepradere
 URL: https://codeforces.com/problemset/problem/1538/A
 Problem: CF1538A
**/
func (in *CF1538A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt()
		min, max := n+1, 0
		posMin, posMax := -1, -1
		for i := 1; i <= n; i++ {
			num := in.NextInt()
			if num < min {
				min = num
				posMin = i
			}
			if num > max {
				max = num
				posMax = i
			}
		}

		ans := 0

		if posMax < posMin {
			posMax, posMin = posMin, posMax
		}

		if posMin < n-posMax {
			ans += posMin
			if posMax-posMin < n-posMax {
				ans += posMax - posMin
			} else {
				ans += n - posMax
			}
		} else {
			ans += n - posMax + 1
			if posMax-posMin < posMin {
				ans += posMax - posMin
			} else {
				ans += posMin
			}
		}

		fmt.Println(ans)

	}
}

func closeTo(a, b, c int) int {
	if b-a > b-c {
		return b - c
	}
	return b - a
}

func NewCF1538A(r *bufio.Reader) *CF1538A {
	return &CF1538A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1538A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)
	for t > 0 {
		var n int
		fmt.Fscan(in, &n)
		arr := make([]int, n)
		pos := make([]int, n + 1)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			pos[arr[i]] = i + 1
		}
		if pos[1] > pos[n] {
			pos[1], pos[n] = pos[n], pos[1]
		}
		ans := pos[n]
		ans = min(ans, pos[1] + n + 1 - pos[n])
		ans = min(ans, n + 1 - pos[1])
		fmt.Fprintln(out, ans)
		t--
	}
}

package main

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

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
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var t int
	fmt.Fscan(in, &t)

	for t > 0 {
		t--
		var n int
		max_val := 101
		min_val := 0
		max_pos := 0
		min_pos := 0

		fmt.Fscan(in, &n)
		arr := make([]int, n)

		for i := 0; i < n; i++ {
			fmt.Fscan(in, &arr[i])
			if arr[i] > max_val {
				max_val = arr[i]
				max_pos = i
			}
			if arr[i] < min_val {
				min_val = arr[i]
				min_pos = i
			}

		}

		ans := max(max_pos, min_pos) + 1
		ans = min(ans, n-min(max_pos, min_pos))
		ans = min(ans, max_pos+1+(n-min_pos))
		ans = min(ans, min_pos+1+(n-max_pos))

		fmt.Fprintln(out, ans)
	}

	out.Flush()

}

package main

import (
	"fmt"
	"os"
)

var ins = os.Stdin
var out = os.Stdout

func scanf(s string, a ...interface{}) (int, error)  { return fmt.Fscanf(ins, s, a...) }
func scan(a ...interface{}) (int, error)             { return fmt.Fscan(ins, a...) }
func printf(s string, a ...interface{}) (int, error) { return fmt.Fscanf(out, s, a...) }

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

func main() {
	var l int
	var n int
	scan(&n)
	for j := 0; j < n; j++ {
		l=0
		scan(&l)
		m := make([]int, l)
		for i := 0; i < l; i++ {
			scan(&m[i])
		}
		fmt.Printf("%d\n", fn(m))
	}
}
func fn(a []int) (res int){
	var imax, imin,max int
	min:=101
	for i:=0; i<len(a); i++{
		if a[i] < min{
			min = a[i]
			imin = i
		}
		if a[i] > max{
			max = a[i]
			imax = i
		}

	}
	if (imax+1)<=(len(a)/2) && (imin+1) <= (len(a)/2) {
		return maximum(imin+1, imax+1)
	}
	if (imax+1)>=(len(a)/2) && (imin+1) >= (len(a)/2) {
		return maximum(len(a) - imax, len(a) - imin)
	}
	if (imax+1)<=(len(a)/2) && (imin+1) >= (len(a)/2){
		return imax+1+len(a)-imin
	}

	return imin+1+len(a)-imax
}

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


package main

import "fmt"

func calc(t int) string {
	var a int
	var b int
	fmt.Scanln(&a,&b)
	var prev int = a
	var sum1 int = a
	var sum2 int = b
	var flag bool = false

	if a!=b{
		return "rated"
	}
	for i:=1;i<t;i++{
		var c int
		var d int
		fmt.Scanln(&c,&d)
		if c!=d{
			return "rated"
		}
		sum1 += c
		sum2 += d
		if prev<c{
			flag = true
		}
		prev  = c
	}

	if sum1!=sum2{
		return "rated"
	}else{
		if flag==true{
			return "unrated"
		}
	}
	return "maybe"
}

func main(){
	var t int
	fmt.Scanln(&t)
	ans := calc(t)
	fmt.Print(ans)
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	var a [2000]int
	var tmp int

	fmt.Scan(&n)
	//	fmt.Println(n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&tmp)
		if a[i] != tmp {
			fmt.Println("rated")
			os.Exit(0)
		}
	}

	for i := 1; i < n; i++ {
		if a[i] > a[i-1] {
			fmt.Println("unrated")
			os.Exit(0)
		}
	}

	fmt.Println("maybe")

}

package main

import "fmt"

func main() {
	var n int;
	fmt.Scanf("%d\n",&n);
	var ratingBefore, ratingAfter []int = make([]int, n), make([]int, n)
	changed := false;
	sorted := true;
	for i := 0; i < n; i++ {
		fmt.Scanf("%d%d\n",&ratingBefore[i], &ratingAfter[i]);
		changed = changed || ratingBefore[i] != ratingAfter[i];
		sorted = sorted && (i == 0 || ratingBefore[i] <= ratingBefore[i - 1])
	}
	switch {
	case changed:
		fmt.Println("rated")
	case sorted:
		fmt.Println("maybe")
	default:
		fmt.Println("unrated")
	}
}

package main

import (
	"fmt"
)

func main() {
	var num, oldR, newR int
	prev := 4127
	isUnrated := false

	fmt.Scanf("%d\n", &num)

	for i := 0; i < num; i++ {
		fmt.Scanf("%d %d\n", &oldR, &newR)

		if oldR != newR {
			fmt.Print("rated")
			return
		}

		if prev < oldR {
			isUnrated = true
		}
		prev = oldR
	}

	if isUnrated {
		fmt.Print("unrated")
	} else {
		fmt.Print("maybe")
	}
}

package main

import "fmt"

type row struct {
	fst int
	scd int
}

func is_rating(input []row) string {
	prev := input[0]
	if prev.fst != prev.scd {
		return "rated"
	}
	for _,i := range input[1:] {
		if i.fst != i.scd {
			return "rated"
		}
	}
	for _,i := range input[1:]{
		if i.fst > prev.fst{
			return "unrated"
		}
		prev = i
	}
	return "maybe"
}

func main() {
	var num int
	fmt.Scanf("%d\n", &num)
	table := make([]row,num)
	for i:=0 ; i < num; i++{
		fmt.Scanf("%d %d\n", &table[i].fst, &table[i].scd)
	}
	fmt.Printf("%s\n", is_rating(table))
}

package main

import "fmt"

type row struct {
	fst int
	scd int
}

func is_rating(input []row) string {
	change := false
	prev := input[0]
	if prev.fst != prev.scd {
		return "rated"
	}
	for _,i := range input[1:]{
		if i.fst != i.scd {
			return "rated"
		}
		if i.fst > prev.fst{
			change = true
		}
		prev = i
	}
	if change {
		return "unrated"
	}
	return "maybe"
}

func main() {
	var num int
	fmt.Scanf("%d\n", &num)
	table := make([]row,num)
	for i:=0 ; i < num; i++{
		fmt.Scanf("%d %d\n", &table[i].fst, &table[i].scd)
	}
	fmt.Printf("%s\n", is_rating(table))
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	var a [2000]int
	var tmp int

	fmt.Scan(&n)
	//	fmt.Println(n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&tmp)
		if a[i] != tmp {
			fmt.Println("rated")
			os.Exit(0)
		}
		if i != 0 {
			if a[i] > a[i-1] {
				fmt.Println("unrated")
				os.Exit(0)
			}
		}
	}

	fmt.Println("maybe")

}

package main

import "fmt"

func calc(t int) string {
	var a int
	var b int
	fmt.Scanln(&a,&b)
	var prev int = a
	var sum1 int = a
	var sum2 int = b
	var flag bool = false
	for i:=1;i<t;i++{
		var c int
		var d int
		fmt.Scanln(&c,&d)
		sum1 += c
		sum2 += d
		if prev<c{
			flag = true
		}
		prev  = c
	}

	if sum1!=sum2{
		return "rated"
	}else{
		if flag==true{
			return "unrated"
		}
	}
	return "maybe"
}

func main(){
	var t int
	fmt.Scanln(&t)
	ans := calc(t)
	fmt.Print(ans)
}

package main

import "fmt"

func calc(t int) string {
	var a int
	var b int
	fmt.Scanln(&a,&b)
	var prev int = a

	if a!=b{
		return "rated"
	}

	for i:=1;i<t;i++{
		var c int
		var d int
		fmt.Scanln(&c,&d)
		if c!=d{
			return "rated"
		}else{
			if c>prev{
				return "unrated"
			}
		}
		prev = c
	}
	return "maybe"
}

func main(){
	var t int
	fmt.Scanln(&t)
	ans := calc(t)
	fmt.Print(ans)
}

package main

import "fmt"

func main() {
	var n int;
	fmt.Scanf("%d",&n);
	var ratingBefore, ratingAfter []int = make([]int, n), make([]int, n)
	changed := false;
	sorted := true;
	for i := 0; i < n; i++ {
		fmt.Scanf("%d%d",&ratingBefore[i], &ratingAfter[i]);
		changed = changed || ratingBefore[i] != ratingAfter[i];
		sorted = sorted && (i == 0 || ratingBefore[i] <= ratingBefore[i - 1])
	}
	switch {
	case changed:
		fmt.Println("rated")
	case sorted:
		fmt.Println("maybe")
	default:
		fmt.Println("unrated")
	}
}

package main

import "fmt"

func main() {
	var n int;
	fmt.Scanf("%d",&n);
	var ratingBefore, ratingAfter []int = make([]int, n), make([]int, n)
	changed := false;
	sorted := true;
	for i := 0; i < n; i++ {
		fmt.Scanf("%d%d",&ratingBefore[i], &ratingAfter[i]);
		changed = changed && ratingBefore[i] != ratingAfter[i];
		sorted = sorted && i == 0 || ratingBefore[i] <= ratingBefore[i - 1]
	}
	switch {
	case changed:
		fmt.Println("rated")
	case sorted:
		fmt.Println("maybe")
	default:
		fmt.Println("unrated")
	}
}

package main

import "fmt"

func main() {
	var n int;
	fmt.Scanf("%d",&n);
	var ratingBefore, ratingAfter []int = make([]int, n), make([]int, n)
	changed := false;
	sorted := true;
	for i := 0; i < n; i++ {
		fmt.Scanf("%d%d",&ratingBefore[i], &ratingAfter[i]);
		changed = changed || ratingBefore[i] != ratingAfter[i];
		sorted = sorted && i == 0 || ratingBefore[i] <= ratingBefore[i - 1]
	}
	switch {
	case changed:
		fmt.Println("rated")
	case sorted:
		fmt.Println("maybe")
	default:
		fmt.Println("unrated")
	}
}

package main

import (
	"fmt"
)

func main() {
	var num, oldR, newR int
	prev := 4127
	isUnrated := false

	fmt.Scanf("%d\n", &num)

	for i := 0; i < num; i++ {
		fmt.Scanf("%d %d\n", &oldR, &newR)

		if oldR != newR {
			fmt.Print("rated")
			return
		}

		if prev < oldR {
			isUnrated = false
		}
		prev = oldR
	}

	if isUnrated {
		fmt.Print("unrated")
	} else {
		fmt.Print("maybe")
	}
}

package main

import (
	"fmt"
)

func main() {
	var num, oldR, newR int
	prev := 4127
	isUnrated := false

	fmt.Scanf("%d\n", &num)

	for i := 0; i < num; i++ {
		fmt.Scanf("%d %d\n", &oldR, &newR)

		if oldR != newR {
			fmt.Print("rated")
			return
		}

		isUnrated = prev < oldR
		prev = oldR
	}

	if isUnrated {
		fmt.Print("unrated")
	} else {
		fmt.Print("maybe")
	}
}

package main

import "fmt"

type row struct {
	fst int
	scd int
}

func is_rating(input []row) string {
	prev := input[0]
	if prev.fst != prev.scd {
		return "rated"
	}
	for _,i := range input[1:]{
		if i.fst != i.scd {
			return "rated"
		}
		if i.fst > prev.fst{
			return "unrated"
		}
		prev = i
	}
	return "maybe"
}

func main() {
	var num int
	fmt.Scanf("%d\n", &num)
	table := make([]row,num)
	for i:=0 ; i < num; i++{
		fmt.Scanf("%d %d\n", &table[i].fst, &table[i].scd)
	}
	fmt.Printf("%s\n", is_rating(table))
}

package main

import "fmt"

type row struct {
	fst int64
	scd int64
}

func is_rating(input []row) string {
	change := false
	prev := input[0]
	if prev.fst != prev.scd {
		return "rated"
	}
	for _,i := range input[1:]{
		if i.fst != i.scd {
			return "rated"
		}
		if i.fst > prev.fst{
			change = true
		}
		prev = i
	}
	if change {
		return "unrated"
	}
	return "maybe"
}

func main() {
	var num int
	fmt.Scanf("%d", &num)
	table := make([]row,num)
	for i:=0 ; i < num; i++{
		fmt.Scanf("%d %d", &table[i].fst, &table[i].scd)
	}
	fmt.Printf("%s", is_rating(table))
}

package main

import "fmt"

type row struct {
	fst int
	scd int
}

func is_rating(input []row) string {
	change := false
	prev := input[0]
	if prev.fst != prev.scd {
		return "rated"
	}
	for _,i := range input[1:]{
		if i.fst != i.scd {
			return "rated"
		}
		if i.fst > prev.fst{
			change = true
		}
		prev = i
	}
	if change {
		return "unrated"
	}
	return "maybe"
}

func main() {
	var num int
	fmt.Scanf("%d", &num)
	table := make([]row,num)
	for i:=0 ; i < num; i++{
		fmt.Scanf("%d %d", &table[i].fst, &table[i].scd)
	}
	fmt.Printf("%s", is_rating(table))
}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	var a [2000]int
	var tmp int

	fmt.Scan(&n)
	//	fmt.Println(n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&tmp)
		if a[i] != tmp {
			fmt.Println("rated")
			os.Exit(0)
		}
		if i != 0 {
			if a[i] > a[i-1] {
				fmt.Println("unrated")
				os.Exit(0)
			}
		}
	}

	for i := 1; i < n; i++ {
		if a[i] > a[i-1] {
			fmt.Println("unrated")
			os.Exit(0)
		}
	}

	fmt.Println("maybe")

}

package main

import (
	"fmt"
	"os"
)

func main() {
	var n int
	var a [2000]int
	var tmp int

	fmt.Scan(&n)
	//	fmt.Println(n)

	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
		fmt.Scan(&tmp)
		if a[i] != tmp {
			fmt.Println("rated")
			os.Exit(0)
		}
		if i != 0 {
			if a[i] > a[i-1] {
				fmt.Println("unrated")
			}
		}
	}

	fmt.Println("maybe")

}


package main

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

func Max(a, b int32) int32 {
	if b > a {
		return b
	}

	return a
}

func main() {
	var t, a, b, x, y int32
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	_, _ = fmt.Fscanf(in, "%d\n", &t)
	for i := int32(0); i < t; i++ {
		_, _ = fmt.Fscanf(in, "%v %v %v %v\n", &a, &b, &x, &y)
		_, _ = fmt.Fprintf(out, "%v\n", Max(b*Max(x, a-x-1), a*Max(y, b-y-1)))
	}
	_ = out.Flush()
}

// A. Dead Pixel
/*
Screen resolution of Polycarp's monitor is 𝑎×𝑏 pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates (𝑥,𝑦) (0≤𝑥<𝑎,0≤𝑦<𝑏). You can consider columns of pixels to be numbered from 0 to 𝑎−1, and rows — from 0 to 𝑏−1.

Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.

Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.

Input

In the first line you are given an integer 𝑡 (1≤𝑡≤104) — the number of test cases in the test. In the next lines you are given descriptions of 𝑡 test cases.

Each test case contains a single line which consists of 4 integers 𝑎,𝑏,𝑥 and 𝑦 (1≤𝑎,𝑏≤104; 0≤𝑥<𝑎; 0≤𝑦<𝑏) — the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that 𝑎+𝑏>2 (e.g. 𝑎=𝑏=1 is impossible).

Output
Print 𝑡 integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.

Example
Input
6
8 8 0 0
1 10 0 3
17 31 10 4
2 1 0 0
5 10 3 9
10 10 4 8

Output
56
6
442
1
45
80

Note
In the first test case, the screen resolution is 8×8, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(input1))

	for ; t > 0; t-- {
		input2, _ := reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(input2), " ")

		a, _ := strconv.Atoi(arr[0])
		b, _ := strconv.Atoi(arr[1])
		x, _ := strconv.Atoi(arr[2])
		y, _ := strconv.Atoi(arr[3])

		fmt.Println(max(max(x, a-1-x)*b, max(y, b-y-1)*a))
	}
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}

package main

import "fmt"

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var a, b, x, y int
		fmt.Scanf("%d %d %d %d\n", &a, &b, &x, &y)
		s1 := b * (x)
		s2 := a * (y)
		s3 := (a - x - 1) * b
		s4 := (b - y - 1) * a
		max := s1
		if max < s2 {
			max = s2
		}
		if max < s3 {
			max = s3
		}
		if max < s4 {
			max = s4
		}
		fmt.Println(max)
	}
}

package main

import "fmt"
import "os"
import "bufio"
import "strings"
import "strconv"

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    line := scanner.Text()
    t, _ := strconv.Atoi(line)
    for i := 0; i < t; i++ {
        scanner.Scan()
        line = scanner.Text()
        parts := strings.Split(line, " ")
        a, _ := strconv.Atoi(parts[0])
        b, _ := strconv.Atoi(parts[1])
        x, _ := strconv.Atoi(parts[2])
        y, _ := strconv.Atoi(parts[3])
        //~ fmt.Println(a, b, x, y)

        area_l := x * b
        area_r := (a - 1 - x) * b
        area_t := y * a
        area_b := (b - 1 - y) * a

        max := area_l
        if area_r > max {
            max = area_r
        }
        if area_t > max {
            max = area_t
        }
        if area_b > max {
            max = area_b
        }

        fmt.Println(max)
    }
}

package main

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

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

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

	for t := 0; t < tc; t++ {
		var a, b, x, y int
		fmt.Fscanf(r, "%d %d %d %d\n", &a, &b, &x, &y)
		fmt.Fprintf(w, "%d\n", findBestRect(a, b, x, y))
	}
}

func findBestRect(a, b, x, y int) int {
	r := 0
	if y-1 >= 0 {
		r = max(r, y*a)
	}
	if y+1 < b {
		r = max(r, (b-y-1)*a)
	}
	if x+1 < a {
		r = max(r, (a-x-1)*b)
	}
	if x-1 >= 0 {
		r = max(r, x*b)
	}

	return r
}

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

package main

import "fmt"

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

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var a, b, x, y int
		fmt.Scan(&a, &b, &x, &y)
		rls := 0
		rls = max(rls, a*y)
		rls = max(rls, a*(b-y-1))
		rls = max(rls, b*x)
		rls = max(rls, b*(a-x-1))
		fmt.Println(rls)
	}
}

package main

import (
	"fmt"
)

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

	return a
}

func main() {
	var t, a, b, x, y int
	_, _ = fmt.Scanf("%v\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanf("%v %v %v %v\n", &a, &b, &x, &y)
		_, _ = fmt.Printf("%v\n", Max(b*Max(x, a-x-1), a*Max(y, b-y-1)))
	}
}

package main

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

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

	return a
}

func main() {
	var t, a, b, x, y int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	_, _ = fmt.Fscanf(in, "%v\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Fscanf(in, "%v %v %v %v\n", &a, &b, &x, &y)
		_, _ = fmt.Fprintf(out, "%v\n", Max(b*Max(x, a-x-1), a*Max(y, b-y-1)))
	}
	_ = out.Flush()
}


package main

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

// github.com/EndlessCheng/codeforces-go
func CF1016D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m, s, s0 int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		s ^= a[i]
		if i > 0 {
			s0 ^= a[i]
		}
	}
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
		s ^= b[i]
	}

	if s > 0 {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	Fprint(out, s0^b[0])
	for j := 1; j < m; j++ {
		Fprint(out, " ", b[j])
	}
	Fprintln(out)
	for i := 1; i < n; i++ {
		Fprint(out, a[i])
		for j := 1; j < m; j++ {
			Fprint(out, " 0")
		}
		Fprintln(out)
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1016D(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var n, m, s, s0 int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		s ^= a[i]
		if i > 0 {
			s0 ^= a[i]
		}
	}
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
		s ^= b[i]
	}

	if s > 0 {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	Fprint(out, s0^b[0])
	for j := 1; j < m; j++ {
		Fprint(out, " ", b[j])
	}
	Fprintln(out)
	for i := 1; i < n; i++ {
		Fprintln(out, a[i], strings.Repeat(" 0", m-1))
	}
}

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