package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if ((s[0]-s[2] <= s[4]) || (s[3]-1 <= s[4])) && ((s[1]-s[3] <= s[4]) || (s[2]-1 <= s[4])) {
		return false
	}

	return true
}

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

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d, c, temp, temp1, ans int32
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c = 0
		temp = 0
		temp1 = 0
		ans = -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
			temp1++
		}
		if sy-d <= 1 {
			c++
			temp1++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && (temp == 2 || temp1 == 2) {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c >= 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) ||
			((sx+d >= n && sy-d <= 1) && (sx-d <= 1 && sy+d >= m)) ||
			(sx+d >= n && sx-d <= 1) || (sy+d >= m && sy-d <= 1) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if (sx-d <= 1 && sy-d <= 1) || (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sx+d >= n) || (sy-d <= 1 && sy+d >= m) {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) || (abs(n-x)+abs(m-y) <= d) || (x-d <= 1 && y-d <= 1) || (x+d >= n && y+d >= m) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var n, m, sx, sy, d int

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

	for ; t > 0; t-- {
		fmt.Fscan(in, &n, &m, &sx, &sy, &d)
		if (sx-d > 1 && sy+d < m) || (sy-d > 1 && sx+d < n) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

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

	for ; t > 0; t-- {
		var n, m, sx, sy, d int
		fmt.Fscan(in, &n, &m, &sx, &sy, &d)
		if (sx-d > 1 && sy+d < m) || (sy-d > 1 && sx+d < n) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if (sx-d > 1 && sy+d < m) || (sy-d > 1 && sx+d < n) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var n, m, x, y, d int
var dir [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		fmt.Fscanf(in, "%d %d %d %d %d\n", &n, &m, &x, &y, &d)
		solve()
		t--
	}
}

func solve() {
	a1, a2, a3, a4 := y-1, x-1, m-y, n-x
	if (a1 <= d && a2 <= d) ||
		(a3 <= d && a4 <= d) ||
		(a1 <= d && a3 <= d) ||
		(a2 <= d && a4 <= d) {
		fmt.Println(-1)
	} else {
		fmt.Println(n + m - 2)
	}
}

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

package main

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

func main() {
	r := readerStdin()

	for cases := readInt(r); cases != 0; cases-- {
		values := readSliceInt(r)
		n, m, x, y, d := values[0], values[1], values[2], values[3], values[4]

		var result = -1
		// go to the upside
		if y+d < m && x-d > 1 || y-d > 1 && x+d < n {
			result = n + m - 2
		}

		fmt.Println(result)
	}
}

func readerStdin() *bufio.Reader {
	return bufio.NewReader(os.Stdin)
}

func readInt(r *bufio.Reader) (i int) {
	str := readString(r)
	i, _ = strconv.Atoi(str)
	return
}

func readString(r *bufio.Reader) (s string) {
	s, _ = r.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return
}

func readSliceInt(r *bufio.Reader) []int {
	str := readString(r)
	slice := strings.Split(str, " ")

	var result = make([]int, len(slice))
	for i, v := range slice {
		x, _ := strconv.Atoi(v)
		result[i] = x
	}

	return result
}

package main

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

var P107 int = 1000000007

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

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

func Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		y, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		d, _ := strconv.Atoi(scanner.Text())

		if (x-d <= 1 && y-d <= 1) || (x+d >= n && y+d >= m) || (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) {
			wr.WriteString("-1\n")
		} else {
			wr.WriteString(strconv.Itoa(n+m-2) + "\n")
		}
	}
}

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

package main

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

var (
	reader           = bufio.NewReader(os.Stdin)
	t, n, m, x, y, d int
)

func solve(n, m, x, y, d int) int {
	if (n-x <= d || y-1 <= d) && (m-y <= d || x-1 <= d) {
		return -1
	}
	return n + m - 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m, &x, &y, &d)
		fmt.Println(solve(n, m, x, y, d))
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		n := tmp[0]
		m := tmp[1]
		sx := tmp[2]
		sy := tmp[3]
		d := tmp[4]
		fmt.Println(solve(n, m, sx, sy, d))
	}
}

func solve(n, m, sx, sy, d int) int {
	if sx+d >= n && sx-d <= 1 {
		return -1
	} else if sy+d >= m && sy-d <= 1 {
		return -1
	} else if n-sx+m-sy <= d {
		return -1
	} else if sx-d <= 1 && sy-d <= 1 {
		return -1
	} else if sx+d >= n && sy+d >= m {
		return -1
	}
	return n + m - 2
}

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 readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return int64(n)
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

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, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if (x-d > 1 && y+d < m) || (y-d > 1 && x+d < n) {
			fmt.Fprintln(out, n+m-2)
		} else {
			fmt.Fprintln(out, -1)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) ||
			((sx+d >= n && sy-d <= 1) && (sx-d <= 1 && sy+d >= m)) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) ||
			(sx+d >= n && sy-d <= 1) || (sx-d <= 1 && sy+d >= m) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if (sx+d >= n && sy+d >= m) || (sx-d <= 1 && sy-d <= 1) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) || (n-1) <= d || (m-1) <= d {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d\n", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i < numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanln(&numberOfCases)
	if err != nil {
		fmt.Printf("Error DDD%s", err.Error())
		return
	}
	var i int
	for i <= numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error sdadasd %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
		i++
	}
	return
}

package main

import (
	"fmt"
)

func main() {
	var numberOfCases int
	_, err := fmt.Scanf("%d\n", &numberOfCases)
	if err != nil {
		fmt.Printf("Error %s", err.Error())
		return
	}
	var i int
	for i <= numberOfCases {
		var n, m, sx, sy, d int
		numberOfRead, err := fmt.Scanf("%d %d %d %d %d", &n, &m, &sx, &sy, &d)
		if err != nil || numberOfRead != 5 {
			fmt.Printf("Error %x", err.Error())
			return
		}
		if ((sx-1)+(sy-1) <= d) || ((n-sx)+(m-sy) <= d) {
			fmt.Println(-1)
		} else {
			fmt.Println(n + m - 2)
		}
	}
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		/*
			sx - d > 1
			sy - d > 1
			sx + d < n -1
			sy + d < m - 1

		*/
		if sx-d >= 1 && sy-d >= 1 && sx+d < n || sy+d < m {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)

	var collision = func(x, y, rx, ry, d int) bool {
		_rx := rx + d
		_ry := ry - d
		if x >= _rx-2*d && x <= _rx && y >= _ry-2*d && y <= _ry {
			return true
		}
		return false
	}

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if !collision(1, 1, sx, sy, d) && !collision(n, m, sx, sy, d) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)

	var collision = func(x, y, rx, ry, d int) bool {
		_rx := rx + d
		_ry := ry - d
		if x >= _rx && x <= _rx+d && y >= _ry && y <= _ry+d {
			return true
		}
		return false
	}

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if !collision(1, 1, sx, sy, d) && !collision(n, m, sx, sy, d) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if (sx+d < n || sx-d > 1) && (sy+d < m || sy-d > 1) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	t := readInt(in)

	for ; t > 0; t-- {
		inputs := readArrInt(in)
		n, m, sx, sy, d := inputs[0], inputs[1], inputs[2], inputs[3], inputs[4]
		if (sx+d < n || sx-d >= 1) && (sy+d < m || sy-d >= 1) {
			fmt.Println(n + m - 2)
		} else {
			fmt.Println(-1)
		}
	}
}

///////////////////////////////////
// DO NOT EDIT
///////////////////////////////////

func readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readLine(in *bufio.Reader) string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	return line
}

func readLineNumbs(in *bufio.Reader) []string {
	numbs := strings.Split(readLine(in), " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if ((s[0]-s[2] <= s[4]) || (s[1]-s[3] <= s[4])) && ((s[2]-1 <= s[4]) || (s[3]-1 <= s[4])) {
		return false
	}

	return true
}

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

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if ((s[0]-s[2] <= s[4]) || (s[1]-s[3] <= s[4])) && ((s[2]-1 <= s[4]) || (s[3]-1 <= s[4])) {
		return false
	}

	if s[0]-s[2]+s[1]-s[3] <= s[4] {
		return false
	}

	return true
}

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

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if (s[0]-s[2] <= s[4]) && (s[1]-s[3] <= s[4]) {
		return false
	}
	if (s[2]-1 <= s[4]) && (s[3]-1 <= s[4]) {
		return false
	}

	if s[0]-s[2]+s[1]-s[3] <= s[4] {
		return false
	}

	return true
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var t int
	fmt.Scanln(&t)
	var reader = bufio.NewReader(os.Stdin)
	var writer = bufio.NewWriter(os.Stdout)
	for i := 0; i < t; i++ {
		var r = getIntln(reader)
		if canCross(r) {
			writer.WriteString(strconv.Itoa(r[0]+r[1]-2) + "\n")
		} else {
			writer.WriteString("-1\n")
		}
	}
	writer.Flush()
}

func canCross(s []int) bool {
	if (s[0]-s[2] <= s[4]) && (s[1]-s[3] <= s[4]) {
		return false
	}
	if s[0]-s[2]+s[1]-s[3] <= s[4] {
		return false
	}

	return true
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

func main() {
	var N, D int
	fmt.Scanln(&N, &D)
	var reader = bufio.NewReader(os.Stdin)
	var p = getIntln(reader)
	sort.Slice(p, func(i, j int) bool {
		return p[i] < p[j]
	})
	var x = sort.Search(N, func(i int) bool {
		return p[i] > D
	})
	var result = N - x
	var first, last = 0, x - 1
	for first < last {
		var x = p[last]
		for x <= D && first < last {
			x += p[last]
			first++
		}
		if x > D {
			result++
		}
		last--
	}
	fmt.Println(result)
}

func getIntln(in *bufio.Reader) []int {
	var x = strings.Split(getString(in), " ")
	var res = make([]int, len(x))
	for k, v := range x {
		res[k], _ = strconv.Atoi(v)
	}
	return res
}

func getString(in *bufio.Reader) string {
	var s, _ = in.ReadString('\n')
	s = strings.TrimSpace(s)
	return s
}

package main

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

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, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if (x+d >= n && y+d >= m) || (x-d <= 1 && y-d <= 1) {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n+m-2)
		}
	}
}

package main

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

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, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if x+d >= n && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n+m-2)
		}
	}
}

package main

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

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, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if x+d >= n && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, (n-1)+(m-1))
		}
	}
}

package main

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

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, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if x+d >= n && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n-1+m-1)
		}
	}
}

package main

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

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

func abs(n int) int {
	if n < 0 {
		return n * -1
	}
	return n
}

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, m, x, y, d int
		fmt.Fscan(in, &n, &m, &x, &y, &d)

		if x-d <= 1 && x+d >= n || y-d <= 1 && y+d >= m {
			fmt.Fprintln(out, -1)
		} else {
			fmt.Fprintln(out, n-1+m-1)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d, c, temp, ans int32
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c = 0
		temp = 0
		ans = -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && temp == 2 {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c >= 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d, c, temp, ans int32
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c = 0
		temp = 0
		ans = -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && temp == 2 {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c > 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d int
		fmt.Scan(&n, &m, &sx, &sy, &d)
		c := 0
		temp := 0
		ans := -1
		if sx-d <= 1 {
			c++
			temp++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
			temp++
		}
		// fmt.Printf(" c: %v, temp : %v \n", c, temp)
		if c == 2 && temp == 2 {
			ans = n + m - 2
			fmt.Println(ans)
		} else if c > 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var n, m, sx, sy, d int
		fmt.Scan(&n, &m, &sx, &sy, &d)
		if d == 0 {
			ans := n + m - 2
			fmt.Println(ans)
			continue
		}
		c := 0
		ans := -1
		if sx-d <= 1 {
			c++
		}
		if sx+d >= n {
			c++
		}
		if sy-d <= 1 {
			c++
		}
		if sy+d >= m {
			c++
		}
		if c >= 2 {
			fmt.Println(ans)
		} else {
			ans = n + m - 2
			fmt.Println(ans)
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if (sx-d <= 1 && sy-d <= 1) || (sx+d >= n && sy+d >= m) {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if (sx-d <= 0 && sy-d <= 0) || (sx+d >= n && sy+d >= m) {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

// 00
package main

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

func write(f *bufio.Writer, a ...interface{}) {
	f.Write([]byte(fmt.Sprint(a...)))
}

type Number interface {
	int64 | float64 | int
}

func min[K Number](a K, b K) K {
	if a < b {
		return a
	}
	return b
}
func max[K Number](a K, b K) K {
	if a > b {
		return a
	}
	return b
}
func main() {
	var T int
	reader := bufio.NewReader(os.Stdin)
	f := bufio.NewWriter(os.Stdout)
	defer f.Flush()
	fmt.Fscan(reader, &T)
	for t := 1; t <= T; t++ {
		var n, m, sx, sy, d int
		fmt.Fscan(reader, &n, &m, &sx, &sy, &d)
		if sx-d <= 0 && sy-d <= 0 || sx+d >= n && sy+d >= m {
			write(f, -1, "\n")
		} else {
			write(f, n+m-2, "\n")
		}
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) || (abs(n-x)+abs(m-y) <= d) || (abs(1-x)+abs(1-y) <= d) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

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

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) || (abs(n-x)+abs(m-y) <= d) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var in = bufio.NewReader(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func qwq() {
	var n, m, x, y, d int
	fmt.Fscan(in, &n, &m, &x, &y, &d)
	if (x-d <= 1 && x+d >= n) || (y-d <= 1 && y+d >= m) {
		fmt.Fprintln(out, -1)
		return
	}
	fmt.Fprintln(out, n+m-2)

}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var n, m, x, y, d int
var dir [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		fmt.Fscanf(in, "%d %d %d %d %d\n", &n, &m, &x, &y, &d)
		solve()
		t--
	}
}

func solve() {
	a1, a2, a3, a4 := y-1, x-1, m-y, n-x
	if (a1 <= d && a2 <= d) ||
		(a3 <= 3 && a4 <= d) ||
		(a1 <= d && a3 <= d) ||
		(a2 <= 3 && a4 <= d) {
		fmt.Println(-1)
	} else {
		fmt.Println(n + m - 2)
	}
}

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

package main

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

var in *bufio.Reader = bufio.NewReader(os.Stdin)
var n, m, x, y, d int
var dir [][]int = [][]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}

func main() {
	var t int
	fmt.Fscanf(in, "%d\n", &t)
	for t > 0 {
		fmt.Fscanf(in, "%d %d %d %d %d\n", &n, &m, &x, &y, &d)
		solve()
		t--
	}
}

func solve() {
	if (x-1 <= d && y-1 <= d) || (n-x <= d && m-y <= d) {
		fmt.Println(-1)
	} else {
		fmt.Println(n + m - 2)
	}
}

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

package main

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

func main() {
	r := readerStdin()

	for cases := readInt(r); cases != 0; cases-- {
		values := readSliceInt(r)
		n, m, x, y, d := values[0], values[1], values[2], values[3], values[4]

		var result = -1
		// go to the upside
		if y+d < m && x-d > 0 || y-d > 0 && x+d < n {
			result = n + m - 2
		}

		fmt.Println(result)
	}
}

func readerStdin() *bufio.Reader {
	return bufio.NewReader(os.Stdin)
}

func readInt(r *bufio.Reader) (i int) {
	str := readString(r)
	i, _ = strconv.Atoi(str)
	return
}

func readString(r *bufio.Reader) (s string) {
	s, _ = r.ReadString('\n')
	s = strings.ReplaceAll(s, "\n", "")
	s = strings.ReplaceAll(s, "\r", "")
	return
}

func readSliceInt(r *bufio.Reader) []int {
	str := readString(r)
	slice := strings.Split(str, " ")

	var result = make([]int, len(slice))
	for i, v := range slice {
		x, _ := strconv.Atoi(v)
		result[i] = x
	}

	return result
}

package main

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

var P107 int = 1000000007

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

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

func Abs(a int) int {
	return Max(a, -a)
}

func Gcd(a, b int) int {
	min := Min(a, b)
	max := Max(a, b)
	if min == 0 {
		return max
	}

	for max%min != 0 {
		min, max = max%min, min
	}

	return min
}

func Lcm(a, b int) int {
	return a / Gcd(a, b) * b
}

// var facs []int = make([]int, X + 1)
func FacP(n, p int) int {
	// if facs[n] != 0 {
	//   return facs[n]
	// }
	result := 1
	// facs[0] = 1
	for i := 1; i <= n; i++ {
		result = (result * i) % p
		// facs[i] = result
	}

	return result
}

func ExpP(a, x, p int) int {
	if x == 0 {
		return 1
	}

	if x%2 == 0 {
		half := ExpP(a, x/2, p)
		return (half * half) % p
	} else {
		return ((a % p) * ExpP(a, x-1, p)) % p
	}
}

func CombP(n, c, p int) int {
	// If CombP be called multiple times, you should optimize FacP using memoization.
	return (FacP(n, p) * ((ExpP(FacP(n-c, p), p-2, p) * ExpP(FacP(c, p), p-2, p)) % p)) % p
}

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())

	for _i := 0; _i < t; _i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		x, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		y, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		d, _ := strconv.Atoi(scanner.Text())

		if (x-d <= 1 && y-d <= 1) || (x+d >= n && y+d >= m) {
			wr.WriteString("-1\n")
		} else {
			wr.WriteString(strconv.Itoa(n+m-2) + "\n")
		}
	}
}

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

package main

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

var (
	reader           = bufio.NewReader(os.Stdin)
	t, n, m, x, y, d int
)

func solve(n, m, x, y, d int) int {
	if n-x <= d && m-y <= d {
		return -1
	}
	return n + m - 2
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n, &m, &x, &y, &d)
		fmt.Println(solve(n, m, x, y, d))
	}
}

package main

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

func main() {
	input := bufio.NewReader(os.Stdin)
	cases := readInt(input)
	for asdf := 0; asdf < cases; asdf++ {
		tmp := readArrInt(input)
		n := tmp[0]
		m := tmp[1]
		sx := tmp[2]
		sy := tmp[3]
		d := tmp[4]
		fmt.Println(solve(n, m, sx, sy, d))
	}
}

func solve(n, m, sx, sy, d int) int {
	if sx+d >= n && sx-d <= 1 {
		return -1
	} else if sy+d >= m && sy-d <= 1 {
		return -1
	} else if n-sx+m-sy <= d {
		return -1
	}
	return n + m - 2
}

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 readInt(in *bufio.Reader) int {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return n
}

func readInt64(in *bufio.Reader) int64 {
	nStr, _ := in.ReadString('\n')
	nStr = strings.ReplaceAll(nStr, "\r", "")
	nStr = strings.ReplaceAll(nStr, "\n", "")
	n, _ := strconv.Atoi(nStr)
	return int64(n)
}

func readLineNumbs(in *bufio.Reader) []string {
	line, _ := in.ReadString('\n')
	line = strings.ReplaceAll(line, "\r", "")
	line = strings.ReplaceAll(line, "\n", "")
	numbs := strings.Split(line, " ")
	return numbs
}

func readArrInt(in *bufio.Reader) []int {
	numbs := readLineNumbs(in)
	arr := make([]int, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.Atoi(n)
		arr[i] = val
	}
	return arr
}

func readArrInt64(in *bufio.Reader) []int64 {
	numbs := readLineNumbs(in)
	arr := make([]int64, len(numbs))
	for i, n := range numbs {
		val, _ := strconv.ParseInt(n, 10, 64)
		arr[i] = val
	}
	return arr
}


package main

import (
	"fmt"
)

type node struct {
	s []int
	c int
	i int
	a int
}

func main() {
	var n, p, c int
	fmt.Scan(&n)
	t := make([]node, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&p, &c)
		t[i].i = i
		t[i].c = c
		t[p].s = append(t[p].s, i)
	}
	// fmt.Println(t)
	a, ok := dfs(t, t[0].s[0])
	if !ok {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for a, i := range a {
		t[i].a = a+1
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", t[i].a)
	}
	fmt.Println()
}

func dfs(t []node, x int) ([]int, bool) {
	r := &t[x]
	a := []int{r.i}
	if len(r.s) == 0 {
		if r.c != 0 {
			return nil, false
		}
	}
	for _, j := range r.s {
		b, ok := dfs(t, j)
		if !ok {
			return nil ,false
		}
		if r.c == 0 {
			a = append(a, b...)
		} else if r.c >= len(b) {
			a = append(a[:len(a)-1], b...)
			a = append(a, r.i)
			r.c -= len(b)
		} else {//r.c < len(b)
			a = append(a[:len(a)-1], b[:r.c]...)
			a = append(a, r.i)
			a = append(a, b[r.c:]...)
			r.c = 0
		}
	}
	if r.c > 0 {
		return nil, false
	}
	return a, true
}

package main

import (
	"fmt"
	"os"
)

const N = 2010
const M = N << 1
var tot = 0
var st [N]int
var nex, b [M]int
func addEdge(x, y int) {
	tot++
	b[tot] = y
	nex[tot] = st[x]
	st[x] = tot
}

var n int
var c, v, sz [N]int
var used [N]bool
func fill(x int) {
	rest := c[x] + 1
	for i := 1; i <= n; i++ {
		if used[i] {
			continue
		}
		rest--
		if rest == 0 {
			v[x] = i
			used[i] = true
			break
		}
	}
	if rest > 0 {
		fmt.Println("NO")
		os.Exit(0)
	}
	sz[x] = 1
	for j := st[x]; j != 0; j = nex[j] {
		fill(b[j])
		sz[x] += sz[b[j]]
	}
	if sz[x] < c[x] + 1 {
		fmt.Println("NO")
		os.Exit(0)
	}
}

func main() {
	var root int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		var tmp int
		fmt.Scan(&tmp, &c[i])
		if tmp == 0 {
			root = i
			continue
		}
		addEdge(tmp, i)
	}

	fill(root)
	fmt.Println("YES")
	for i := 1; i <= n; i++ {
		fmt.Printf("%d", v[i])
		if i != n {
			fmt.Print(" ")
		}
	}
	fmt.Print("\n")
}

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

var p []int
var c []int
var children [][]int
var invalid bool

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	//fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		children = make([][]int, n)
		for i := 0; i < n; i++ {
			children[i] = make([]int, 0)
		}
		p, c = make([]int, n), make([]int, n)
		root := -1
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d %d\n", &p[i], &c[i])
			p[i]--
			if p[i] == -1 {
				root = i
			} else {
				children[p[i]] = append(children[p[i]], i)
			}
		}
		invalid = false
		ans := dfs(root)
		a := make([]int, n)
		for i := 0; i < len(ans); i++ {
			a[ans[i]] = i + 1
		}
		if invalid {
			fmt.Fprintf(out, "NO\n")
		} else {
			fmt.Fprintf(out, "YES\n")
			for i := 0; i < n-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			fmt.Fprintf(out, "%d\n", a[n-1])
		}
	}
	return
}

func dfs(i int) []int {
	if len(children[i]) == 0 {
		if c[i] > 0 {
			invalid = true
			return nil
		}
		return []int{i}
	}
	tmp := []int{}
	for j := 0; j < len(children[i]); j++ {
		tmp = append(tmp, dfs(children[i][j])...)
		if invalid {
			return nil
		}
	}
	if c[i] > len(tmp) {
		invalid = true
		return nil
	}
	ans := []int{}
	for j := 0; j < c[i]; j++ {
		ans = append(ans, tmp[j])
	}
	ans = append(ans, i)
	ans = append(ans, tmp[c[i]:]...)
	return ans
}

package main

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

type node struct {
	diff int
	time int
}

type ByTime []node

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

var p []int
var c []int
var children [][]int
var invalid bool

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(1)
	//fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		n := 0
		fmt.Fscanf(in, "%d\n", &n)
		children = make([][]int, n)
		for i := 0; i < n; i++ {
			children[i] = make([]int, 0)
		}
		p, c = make([]int, n), make([]int, n)
		root := -1
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d %d\n", &p[i], &c[i])
			p[i]--
			if p[i] == -1 {
				root = i
			} else {
				children[p[i]] = append(children[p[i]], i)
			}
		}
		invalid = false
		ans := dfs(root)
		a := make([]int, n)
		for i := 0; i < len(ans); i++ {
			a[ans[i]] = i + 1
		}
		if invalid {
			fmt.Fprintf(out, "NO\n")
		} else {
			fmt.Fprintf(out, "YES\n")
			for i := 0; i < n-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			fmt.Fprintf(out, "%d\n", a[n-1])
		}
	}
	return
}

func dfs(i int) []int {
	if len(children[i]) == 0 {
		return []int{i}
	}
	tmp := []int{}
	for j := 0; j < len(children[i]); j++ {
		tmp = append(tmp, dfs(children[i][j])...)
		if invalid {
			return nil
		}
	}
	if c[i] > len(tmp) {
		invalid = true
		return nil
	}
	ans := []int{}
	for j := 0; j < c[i]; j++ {
		ans = append(ans, tmp[j])
	}
	ans = append(ans, i)
	ans = append(ans, tmp[c[i]:]...)
	return ans
}

package main

import (
	"fmt"
)

type node struct {
	s []int
	c int
	i int
	a int
}

func main() {
	var n, p, c int
	fmt.Scan(&n)
	t := make([]node, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&p, &c)
		t[i].i = i
		t[i].c = c
		t[p].s = append(t[p].s, i)
	}
	// fmt.Println(t)
	a, ok := dfs(t, t[0].s[0])
	if !ok {
		fmt.Println("NO")
		return
	}
	fmt.Println("YES")
	for a, i := range a {
		t[i].a = a+1
	}
	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", t[i].a)
	}
	fmt.Println()
}

func dfs(t []node, x int) ([]int, bool) {
	r := &t[x]
	a := []int{r.i}
	if len(r.s) == 0 {
		if r.c != 0 {
			return nil, false
		}
	}
	for _, j := range r.s {
		b, ok := dfs(t, j)
		if !ok {
			return nil ,false
		}
		if r.c == 0 {
			a = append(a, b...)
		} else if r.c >= len(b) {
			a = append(a[:len(a)-1], b...)
			a = append(a, r.i)
			r.c -= len(b)
		} else {//r.c < len(b)
			a = append(a[:len(a)-1], b[:r.c]...)
			a = append(a, r.i)
			a = append(a, b[r.c:]...)
			r.c = 0
		}
	}
	return a, true
}

package main

import (
	"fmt"
)

func main() {
	var n, p, c int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p, &c)
	}
	fmt.Println("NO")
}

package main

import (
	"fmt"
	"os"
)

const N = 2010
const M = N << 1
var tot = 0
var st [N]int
var nex, b [M]int
func addEdge(x, y int) {
	tot++
	b[tot] = y
	nex[tot] = st[x]
	st[x] = tot
}

var n int
var c, v [N]int
var used [N]bool
func fill(x int) {
	rest := c[x] + 1
	for i := 1; i <= n; i++ {
		if used[i] {
			continue
		}
		rest--
		if rest == 0 {
			v[x] = i
			used[i] = true
			break
		}
	}
	if rest > 0 {
		fmt.Println("NO")
		os.Exit(0)
	}
	for j := st[x]; j != 0; j = nex[j] {
		fill(b[j])
	}
}

func main() {
	var root int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		var tmp int
		fmt.Scan(&tmp, &c[i])
		if tmp == 0 {
			root = i
			continue
		}
		addEdge(tmp, i)
	}

	fill(root)
	fmt.Println("YES")
	for i := 1; i <= n; i++ {
		fmt.Printf("%d", v[i])
		if i != n {
			fmt.Print(" ")
		}
	}
	fmt.Print("\n")
}


package main
import (
	"fmt"
	"bufio"
	"os"
	)

var n int
var a [100005][]int
var b [100005]int
var A [100005]float64

func pre_compute(x int) {
    b[x] = 1
    for _,ch := range(a[x]) {
        pre_compute(ch)
        b[x] += b[ch]
    }
}

func dfs(x int, pp float64) {
    mn := pp+1
    mx := mn
    for _,ch := range(a[x]) {
        mx += float64(b[ch])
    }
    for _,ch := range(a[x]) {
        A[ch] = (mn+mx-float64(b[ch]))/2.0
        dfs(ch,A[ch])
    }
}

func main() {
	in := bufio.NewReader(os.Stdin)
    fmt.Fscan(in,&n);
    for i:=2;i<=n;i++ {
        x := 0
        fmt.Fscan(in,&x)
        a[x] = append(a[x],i)
    }
    pre_compute(1)
    A[1] = 1.0
    dfs(1,1.0)
    for i:=1;i<=n;i++ {
        fmt.Printf("%.1f ",A[i])
    }
}

package main

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

func main() {
	n := getInt()
	C := make([][]int, n+1)
	for i := 2; i <= n; i++ {
		p := getInt()
		C[p] = append(C[p], i)
	}
	S := make([]int, n+1)
	var dfsSize func(x int) int
	dfsSize = func(x int) int {
		S[x] = 1
		for _, v := range C[x] {
			S[x] += dfsSize(v)
		}
		return S[x]
	}
	dfsSize(1)
	T := make([]int64, n+1)
	var dfsTime func(x int, tp int64, sp int)
	dfsTime = func(x int, tp int64, sp int) {
		T[x] = tp + 2 + int64(sp-S[x]-1)
		for _, v := range C[x] {
			dfsTime(v, T[x], S[x])
		}
	}
	dfsTime(1, 0, S[1]+1)
	buf := &bytes.Buffer{}
	for i := 1; i <= n; i++ {
		if t := T[i]; t%2 == 0 {
			fmt.Fprintf(buf, "%v.0\n", t/2)
		} else {
			fmt.Fprintf(buf, "%v.5\n", t/2)
		}
	}
	io.Copy(os.Stdout, buf)
}

type Input struct {
	*bufio.Scanner
}

var globalInput = wrapStdin()

func wrapStdin() Input {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return Input{Scanner: sc}
}
func getInt() int {
	globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x
}
func getIntOk() (int, bool) {
	ok := globalInput.Scan()
	x, _ := strconv.Atoi(globalInput.Text())
	return x, ok
}
func getNint(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		v[i] = getInt()
	}
	return v
}
func getVint(v []int) bool {
	var ok bool
	for i := 0; i < len(v); i++ {
		v[i], ok = getIntOk()
	}
	return ok
}
func getWord() string {
	globalInput.Scan()
	return globalInput.Text()
}
func getWordOk() (string, bool) {
	ok := globalInput.Scan()
	return globalInput.Text(), ok
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min64(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func max64(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}
func minStr(a, b string) string {
	if a < b {
		return a
	}
	return b
}
func maxStr(a, b string) string {
	if a < b {
		return b
	}
	return a
}
func remin(a *int, b int) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax(a *int, b int) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func remin64(a *int64, b int64) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remax64(a *int64, b int64) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}
func reminStr(a *string, b string) bool {
	if b < *a {
		*a = b
		return true
	}
	return false
}
func remaxStr(a *string, b string) bool {
	if b > *a {
		*a = b
		return true
	}
	return false
}

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

type (
	FLess    func(i, j int) bool
	FSwap    func(i, j int)
	wrapSort struct {
		Length int
		FLess
		FSwap
	}
)

func (w *wrapSort) Len() int           { return w.Length }
func (w *wrapSort) Less(i, j int) bool { return w.FLess(i, j) }
func (w *wrapSort) Swap(i, j int)      { w.FSwap(i, j) }
func wsort(length int, fless func(i, j int) bool, fswap func(i, j int)) {
	sort.Sort(&wrapSort{length, fless, fswap})
}


package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	fire := make([]int, 0, n)
	ice := make([]int, 0, n)
	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	mn := int(1e10)
	var num int
	sum := 0
	for i := range a {
		fmt.Fscan(in, &num)
		if a[i] == 0 {
			fire = append(fire, num)
		} else {
			ice = append(ice, num)
		}
		sum += num
		mn = min(mn, num)
	}
	sort.Ints(fire)
	sort.Ints(ice)

	ans := sum

	if len(fire) > len(ice) {
		fire, ice = ice, fire
	}

	if len(fire) < len(ice) {
		for _, p := range fire {
			ans += p
		}
		start := len(ice) - 1
		for i := start; i >= start-len(fire)+1; i-- {
			ans += ice[i]
		}
	} else if len(fire) == len(ice) {
		ans += sum
		ans -= mn
	}

	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

func gena(x, n int) (int, int) {
	if x == 0 || x == n {
		return 0, 0
	}
	y := n - x
	if x > y {
		return y, y
	}
	if x == y {
		return x - 1, y
	}
	return x - 1, x
}

func headsum(a []int64, n int) int64 {
	if n == 0 {
		return 0
	}
	return a[n-1]
}

func calheadsum(a []int64, n int) {
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
}

func main() {
	Fire := make([]int64, 1e5+50)
	Frost := make([]int64, 1e5+50)
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		fireCount := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] == 0 {
				fireCount++
			}
		}
		var fireIndex, frostIndex int
		var sum int64
		for i := 0; i < n; i++ {
			var tmp int64
			fmt.Fscan(in, &tmp)
			if a[i] == 0 {
				Fire[fireIndex] = tmp
				fireIndex++
			} else {
				Frost[frostIndex] = tmp
				frostIndex++
			}
			sum += tmp
		}
		fire := Fire[0:fireIndex]
		frost := Frost[0:frostIndex]
		sort.Slice(fire, func(i, j int) bool {
			return fire[i] > fire[j]
		})
		sort.Slice(frost, func(i, j int) bool {
			return frost[i] > frost[j]
		})
		calheadsum(fire, fireIndex)
		calheadsum(frost, frostIndex)
		x, y := gena(fireCount, n)
		res1 := headsum(fire, x) + headsum(frost, y)
		x, y = gena(n-fireCount, n)
		res2 := headsum(frost, x) + headsum(fire, y)
		if res1 > res2 {
			fmt.Fprintln(out, sum+res1)
		} else {
			fmt.Fprintln(out, sum+res2)
		}
	}
	out.Flush()
}

package main

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

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

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)

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}
		bb := []int64{}
		cc := []int64{}

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

			if aa[i] == 0 {
				bb = append(bb, v)
			} else {
				cc = append(cc, v)
			}
		}

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

		d := min(len(bb), len(cc))
		for i := 0; i < d; i++ {
			bb[len(bb)-i-1] *= 2
			cc[len(cc)-i-1] *= 2
		}
		if d == len(bb) && d == len(cc) {
			if bb[0] <= cc[0] {
				bb[0] /= 2
			} else {
				cc[0] /= 2
			}
		}

		var tot int64
		for _, v := range bb {
			tot += v
		}
		for _, v := range cc {
			tot += v
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

func sum(a []int) int {
	res := 0
	for _, v := range a { res += v }
	return res
}

func solve(d1, d2 []int) int {
	if len(d1) == len(d2) {
		return 2 * (sum(d1) + sum(d2)) - min(d1[0], d2[0])
	} else {
		l := min(len(d1), len(d2))
		return sum(d1[:len(d1)-l]) + sum(d2[:len(d2)-l]) + 2 * (sum(d1[len(d1)-l:]) + sum(d2[len(d2)-l:]))
	}
}

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

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

		a := make([]int, n)
		for i := range a {
			fmt.Fscan(r, &a[i])
		}

		var dmg1, dmg2 [] int
		for _, i := range a {
			var dmg int
			fmt.Fscan(r, &dmg)
			if i == 1 {
				dmg2 = append(dmg2, dmg)
			} else {
				dmg1 = append(dmg1, dmg)
			}
		}

		sort.Ints(dmg1)
		sort.Ints(dmg2)

		fmt.Fprintln(w, solve(dmg1, dmg2))
	}

}

package main

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

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

func (in *CF1738A) 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 *CF1738A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

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

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

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

/**
Run solve the problem CF1738A
Date: 10/4/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1738/A
Problem: CF1738A
**/
func (in *CF1738A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n := in.NextInt()
		a := make([][]int, n)
		for i := 0; i < n; i++ {
			a[i] = make([]int, 2)
			a[i][0] = in.NextInt()
		}
		for i := 0; i < n; i++ {
			a[i][1] = in.NextInt()
		}

		sort.Slice(a, func(i, j int) bool {
			if a[i][1] < a[j][1] {
				return true
			}
			return false
		})
		zeros := make([][]int, 0)
		ones := make([][]int, 0)
		for i := 0; i < n; i++ {
			if a[i][0] == 0 {
				zeros = append(zeros, a[i])
			} else {
				ones = append(ones, a[i])
			}
		}

		sum := 0
		if len(zeros) == len(ones) {
			if zeros[0][1] < ones[0][1] {
				sum = zeros[0][1]
				for i := 1; i < len(zeros); i++ {
					sum += zeros[i][1] * 2
				}
				for i := 0; i < len(ones); i++ {
					sum += ones[i][1] * 2
				}
			} else {
				sum = ones[0][1]
				for i := 0; i < len(zeros); i++ {
					sum += zeros[i][1] * 2
				}
				for i := 1; i < len(ones); i++ {
					sum += ones[i][1] * 2
				}
			}
		} else if len(zeros) > len(ones) {
			for i := 0; i < len(zeros)-len(ones); i++ {
				sum += zeros[i][1]
			}
			for i := len(zeros) - len(ones); i < len(zeros); i++ {
				sum += zeros[i][1] * 2
			}
			for i := 0; i < len(ones); i++ {
				sum += ones[i][1] * 2
			}
		} else {
			for i := 0; i < len(ones)-len(zeros); i++ {
				sum += ones[i][1]
			}
			for i := len(ones) - len(zeros); i < len(ones); i++ {
				sum += ones[i][1] * 2
			}
			for i := 0; i < len(zeros); i++ {
				sum += zeros[i][1] * 2
			}
		}
		fmt.Println(sum)
	}
}

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

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

package main

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

var (
	in  = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func scan(a ...interface{}) {
	fmt.Fscan(in, a...)
}
func printf(f string, a ...interface{}) {
	fmt.Fprintf(out, f, a...)
}

func solve() {
	var n int
	scan(&n)
	fi := make([]int64, 0)
	fr := make([]int64, 0)
	a := make([]int, n)
	b := make([]int64, n)
	var s int64 = 0
	for i := 0; i < n; i++ {
		scan(&a[i])
	}
	for i := 0; i < n; i++ {
		scan(&b[i])
		s += b[i]
	}
	if n == 1 {
		printf("%d\n", b[0])
		return
	}
	for i := 0; i < n; i++ {
		if a[i] == 0 {
			fi = append(fi, b[i])
		} else {
			fr = append(fr, b[i])
		}
	}
	sort.Slice(fi, func(i, j int) bool {
		return fi[i] < fi[j]
	})
	sort.Slice(fr, func(i, j int) bool {
		return fr[i] < fr[j]
	})
	if len(fi) == 0 || len(fr) == 0 {
		printf("%d\n", s)
		return
	}
	m1 := len(fi)
	m2 := len(fr)
	var r int64 = 0
	i, j := len(fi)-1, len(fr)-1
	if (m1 > m2) || (m1 == m2 && fi[0] < fr[0]) {
		r += fi[0]
		// i++
		turn := 1
		for i > 0 || j >= 0 {
			// printf("%d %d %d\n", i, j, turn)
			if turn == 1 {
				if j >= 0 {
					r += 2 * fr[j]
					j--
					turn = 0
				} else {
					if i > 0 {
						r += fi[i]
						i--
					}
				}
			} else {
				if i > 0 {
					r += 2 * fi[i]
					turn = 1
					i--
				} else {
					if j >= 0 {
						r += fr[j]
						j--
					}
				}
			}
		}
	} else {
		r += fr[0]
		turn := 0
		for i >= 0 || j > 0 {
			// printf("%d %d %d\n", i, j, turn)
			if turn == 1 {
				if j > 0 {
					r += 2 * fr[j]
					j--
					turn = 0
				} else {
					if i >= 0 {
						r += fi[i]
						i--
					}
				}
			} else {
				if i >= 0 {
					r += 2 * fi[i]
					turn = 1
					i--
				} else {
					if j > 0 {
						r += fr[j]
						j--
					}
				}
			}
		}
	}
	printf("%d\n", r)
}
func main() {
	defer out.Flush()
	n := 0
	scan(&n)
	for i := 0; i < n; i++ {
		solve()
	}
}

package main

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

func gena(x, n int) (int, int) {
	if x == 0 || x == n {
		return 0, 0
	}
	y := n - x
	if x > y {
		return y, y
	}
	if x == y {
		return x - 1, y
	}
	return x - 1, x
}

func headsum(a []int64, n int) int64 {
	if n == 0 {
		return 0
	}
	return a[n-1]
}

func calheadsum(a []int64, n int) {
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
}

func main() {
	Fire := make([]int64, 1e5+50)
	Frost := make([]int64, 1e5+50)
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	fmt.Fscan(in, &t)
	for ; t != 0; t-- {
		var n int
		fmt.Fscan(in, &n)
		a := make([]int, n)
		fireCount := 0
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			if a[i] == 0 {
				fireCount++
			}
		}
		var fireIndex, frostIndex int
		var sum int64
		for i := 0; i < n; i++ {
			var tmp int64
			fmt.Fscan(in, &tmp)
			if a[i] == 0 {
				Fire[fireIndex] = tmp
				fireIndex++
			} else {
				Frost[frostIndex] = tmp
				frostIndex++
			}
			sum += tmp
		}
		fire := Fire[0:fireIndex]
		frost := Frost[0:frostIndex]
		sort.Slice(fire, func(i, j int) bool {
			return fire[i] > fire[j]
		})
		sort.Slice(frost, func(i, j int) bool {
			return frost[i] > frost[j]
		})
		calheadsum(fire, fireIndex)
		calheadsum(frost, frostIndex)
		x, y := gena(fireCount, n)
		res1 := headsum(fire, x) + headsum(frost, y)
		x, y = gena(n-fireCount, n)
		res2 := headsum(frost, x) + headsum(fire, y)
		if res1 > res2 {
			fmt.Fprintln(out, sum+res1)
		} else {
			fmt.Fprintln(out, sum+res2)
		}
	}
}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	fire := make([]int, 0, n)
	ice := make([]int, 0, n)
	a := make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	var num int
	for i := range a {
		fmt.Fscan(in, &num)
		if a[i] == 0 {
			fire = append(fire, num)
		} else {
			ice = append(ice, num)
		}
	}
	sort.Ints(fire)
	sort.Ints(ice)

	ans := 0

	check := func(curIsFire bool) {
		start := curIsFire

		prev := curIsFire
		curAns := 0
		if curIsFire {
			curAns += fire[0]
		} else {
			curAns += ice[0]
		}
		curIsFire = !curIsFire

		curFire, curIce := len(fire)-1, len(ice)-1
		for i := 0; i < n-1; i++ {
			if curIsFire {
				if (start == curIsFire && curFire == 0) || curFire < 0 {
					curIsFire = !curIsFire
					i--
					continue
				}
				if prev != curIsFire {
					curAns += fire[curFire]
				}
				curAns += fire[curFire]
				curFire--
				prev = curIsFire
			} else {
				if (start == curIsFire && curFire == 0) || curIce < 0 {
					curIsFire = !curIsFire
					i--
					continue
				}
				if prev != curIsFire {
					curAns += ice[curIce]
				}
				curAns += ice[curIce]
				curIce--
				prev = curIsFire
			}
			curIsFire = !curIsFire
		}

		ans = max(ans, curAns)
	}

	if len(fire) > 0 {
		check(true)
	}
	if len(ice) > 0 {
		check(false)
	}

	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(MaxUint >> 1)
const MinInt = -MaxInt - 1

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

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

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)

		aa := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &aa[i])
		}
		bb := []int64{}
		cc := []int64{}

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

			if aa[i] == 0 {
				bb = append(bb, v)
			} else {
				cc = append(cc, v)
			}
		}

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

		d := min(len(bb), len(cc))
		for i := 0; i < d; i++ {
			bb[len(bb)-i-1] *= 2
			cc[len(cc)-i-1] *= 2
		}
		if d == len(bb) && d == len(cc) {
			bb[0] /= 2
		}

		var tot int64
		for _, v := range bb {
			tot += v
		}
		for _, v := range cc {
			tot += v
		}
		fmt.Fprintln(out, tot)
	}
}


package main

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

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	djset := NewDJSet(n)
	for i := 0; i < n; i++ {
		cur := a[i]
		with := a[cur.index]
		if with.index == i {
			djset.Union(with.index, cur.index)
		} else {
			djset.Union(i, cur.index)
		}
	}
	byroot := make([][]string, n)
	for i := 0; i < n; i++ {
		byroot[i] = make([]string, 0)
	}
	for i := 0; i < n; i++ {
		root := djset.Root(i)
		byroot[root] = append(byroot[root], strconv.Itoa(i+1))
	}
	fmt.Println(djset.Count())
	for i := 0; i < n; i++ {
		if len(byroot[i]) > 0 {
			fmt.Printf("%d %s\n", len(byroot[i]), strings.Join(byroot[i], " "))
		}
	}
}

type ByValue []A

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

type DJSet struct {
	upper []int
}

func NewDJSet(n int) *DJSet {
	s := DJSet{upper: make([]int, n)}
	for i := 0; i < n; i++ {
		s.upper[i] = -1
	}
	return &s
}

func (s *DJSet) Root(x int) int {
	if s.upper[x] < 0 {
		return x
	} else {
		s.upper[x] = s.Root(s.upper[x])
	}
	return s.upper[x]
}

func (s *DJSet) Union(x, y int) bool {
	x = s.Root(x)
	y = s.Root(y)
	if x != y {
		if s.upper[y] < s.upper[x] {
			x, y = y, x
		}
		s.upper[x] += s.upper[y]
		s.upper[y] = x
	}
	return x == y
}

func (s *DJSet) Equiv(x, y int) bool {
	return s.Root(x) == s.Root(y)
}

func (s *DJSet) Upper(y int) int {
	return s.upper[y]
}

func (s *DJSet) Count() int {
	ct := 0
	for _, u := range s.upper {
		if u < 0 {
			ct++
		}
	}
	return ct
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	djset := NewDJSet(n)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", with.index+1, cur.index+1))
			djset.Union(with.index, cur.index)
		} else {
			djset.Union(i, cur.index)
		}
	}
	byroot := make([][]string, n)
	for i := 0; i < n; i++ {
		byroot[i] = make([]string, 0)
	}
	for i := 0; i < n; i++ {
		root := djset.Root(i)
		byroot[root] = append(byroot[root], strconv.Itoa(i+1))
	}
	fmt.Println(djset.Count())
	for i := 0; i < n; i++ {
		if len(byroot[i]) > 0 {
			fmt.Printf("%d %s\n", len(byroot[i]), strings.Join(byroot[i], " "))
		}
	}
}

type ByValue []A

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

type DJSet struct {
	upper []int
}

func NewDJSet(n int) *DJSet {
	s := DJSet{upper: make([]int, n)}
	for i := 0; i < n; i++ {
		s.upper[i] = -1
	}
	return &s
}

func (s *DJSet) Root(x int) int {
	if s.upper[x] < 0 {
		return x
	} else {
		s.upper[x] = s.Root(s.upper[x])
	}
	return s.upper[x]
}

func (s *DJSet) Union(x, y int) bool {
	x = s.Root(x)
	y = s.Root(y)
	if x != y {
		if s.upper[y] < s.upper[x] {
			x, y = y, x
		}
		s.upper[x] += s.upper[y]
		s.upper[y] = x
	}
	return x == y
}

func (s *DJSet) Equiv(x, y int) bool {
	return s.Root(x) == s.Root(y)
}

func (s *DJSet) Upper(y int) int {
	return s.upper[y]
}

func (s *DJSet) Count() int {
	ct := 0
	for _, u := range s.upper {
		if u < 0 {
			ct++
		}
	}
	return ct
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
			continue
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", cur.index+1, with.index+1))
		}
	}
	indexes := make([]string, 0)
	for i := 0; i < n; i++ {
		if !visited[i] {
			indexes = append(indexes, fmt.Sprintf("%d", i+1))
		} else if len(indexes) > 0 {
			res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
			indexes = make([]string, 0)
		}
	}
	if len(indexes) > 0 {
		res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
	}
	fmt.Println(len(res))
	for i := 0; i < len(res); i++ {
		fmt.Println(res[i])
	}
}

type ByValue []A

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
			continue
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", cur.index+1, with.index+1))
		}
	}
	indexes := make([]string, 0)
	for i := 0; i < n; i++ {
		if !visited[i] {
			indexes = append(indexes, fmt.Sprintf("%d", i+1))
		}
	}
	if len(indexes) > 0 {
		res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
	}
	fmt.Println(len(res))
	for i := 0; i < len(res); i++ {
		fmt.Println(res[i])
	}
}

type ByValue []A

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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

package main

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

type A struct {
	value int
	index int
}

func solve() {
	n := readInt()
	a := make([]A, n)
	for i := 0; i < n; i++ {
		a[i] = A{readInt(), i}
	}
	sort.Sort(ByValue(a))
	visited := make([]bool, n)

	res := make([]string, 0)
	for i := 0; i < n; i++ {
		cur := a[i]
		if cur.index == i {
			res = append(res, fmt.Sprintf("1 %d", cur.index+1))
			visited[i] = true
			continue
		}
		if visited[i] {
			continue
		}
		with := a[cur.index]
		if with.index == i {
			visited[i] = true
			visited[cur.index] = true
			res = append(res, fmt.Sprintf("2 %d %d", with.index+1, cur.index+1))
		}
	}
	indexes := make([]string, 0)
	for i := 0; i < n; i++ {
		if !visited[i] {
			indexes = append(indexes, fmt.Sprintf("%d", i+1))
		} else if len(indexes) > 0 {
			res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
			indexes = make([]string, 0)
		}
	}
	if len(indexes) > 0 {
		res = append(res, fmt.Sprintf("%d %s", len(indexes), strings.Join(indexes, " ")))
	}
	fmt.Println(len(res))
	for i := 0; i < len(res); i++ {
		fmt.Println(res[i])
	}
}

type ByValue []A

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

var scanner *bufio.Scanner

func main() {
	scanner = bufio.NewScanner(os.Stdin)
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

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

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

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


package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}

func getUniques(num int) []int {
  previous := getI()
  unique := []int{ previous }
  for i := 1; i < num; i++ {
    current := getI()
    if current != previous {
      unique = append(unique, current)
      previous = current
    }
  }
  return unique
}

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 main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, m := getI(), getI()
  cities := getUniques(n)
  towers := getUniques(m)
  n = len(cities)
  m = len(towers)

  result := 0
  if cities[0] < towers[0] {
    result = towers[0] - cities[0]
  }
  if towers[m - 1] < cities[n - 1] {
    result = max(result, cities[n - 1] - towers[m - 1])
  }
  if len(towers) == 1 {
    writer.WriteString(fmt.Sprintf("%d\n", result))
    return
  }

  right := 1
  for _, city := range cities {
    for towers[right] < city {
      right++
      if right == m {
        break
      }
    }
    if right == m {
      break
    }
    distance := min(city - towers[right - 1], towers[right] - city)
    result = max(result, distance)
  }

  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer
var n, m int
var a, b []int

func f(x int, y int) int {
	if x < y {
		return y - x
	}
	return x - y
}

func nearest(x int) int {
	left := 0
	right := m - 1

	for right-left >= 3 {
		i1 := left + (right-left)/3
		i2 := right - (right-left)/3
		if f(x, b[i1]) < f(x, b[i2]) {
			right = i2
		} else {
			left = i1
		}
	}

	best := f(x, b[left])
	for i := left + 1; i <= right; i++ {
		d := f(x, b[i])
		if d < best {
			best = d
		}
	}

	return best
}

func unique(a []int, n *int) {
	p := 1
	for i := 1; i < *n; i++ {
		if a[i] > a[i-1] {
			a[p] = a[i]
			p++
		}
	}
	*n = p
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	unique(a, &n)
	unique(b, &m)

	r := 0
	for i := 0; i < n; i++ {
		d := nearest(a[i])
		if d > r {
			r = d
		}
	}

	fmt.Fprintln(bufout, r)
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer
var n, m int
var a, b []int

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

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	r := 0
	j := 0
	for i := 0; i < n; i++ {
		for j+1 < m && b[j+1] < a[i] {
			j++
		}

		d := abs(a[i] - b[j])
		if j+1 < m && d > b[j+1]-a[i] {
			d = b[j+1] - a[i]
		}

		if r < d {
			r = d
		}
	}

	fmt.Fprintln(bufout, r)
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"time"
)

var bufin *bufio.Reader
var bufout *bufio.Writer
var n, m, n1, m1 int
var a, b, a1, b1 []int

func f(x int, y int) int {
	if x < y {
		return y - x
	}
	return x - y
}

func nearest(x int) int {
	left := 0
	right := m - 1

	for right-left >= 3 {
		i1 := left + (right-left)/3
		i2 := right - (right-left)/3
		if f(x, b[i1]) < f(x, b[i2]) {
			right = i2
		} else {
			left = i1
		}
	}

	best := f(x, b[left])
	for i := left + 1; i <= right; i++ {
		d := f(x, b[i])
		if d < best {
			best = d
		}
	}

	return best
}

func doit() int {
	n1 = 0
	a1 = make([]int, n)
	for i := 0; i < n; i++ {
		if (i == 0) || (a[i] != a[i-1]) {
			a1[n1] = a[i]
			n1++
		}
	}

	m1 = 0
	b1 = make([]int, m)
	for i := 0; i < m; i++ {
		if (i == 0) || (b[i] != b[i-1]) {
			b1[m1] = b[i]
			m1++
		}
	}

	n = n1
	m = m1
	a = a1
	b = b1

	r := 0
	for i := 0; i < n; i++ {
		d := nearest(a[i])
		if d > r {
			r = d
		}
	}
	return r
}

func covered(x int, r int) bool {
	for i := 0; i < m; i++ {
		if f(x, b[i]) <= r {
			return true
		}
	}
	return false
}

func good(r int) bool {
	for i := 0; i < n; i++ {
		if !covered(a[i], r) {
			return false
		}
	}
	return true
}

func brute() int {
	r := 0
	for !good(r) {
		r++
	}
	return r
}

func gen() {
	n = rand.Int()%20 + 1
	m = rand.Int()%20 + 1
	fmt.Fprintln(bufout, n, m)

	a = make([]int, n)
	a[0] = rand.Int()%10 - 20
	fmt.Fprint(bufout, a[0], " ")
	for i := 1; i < n; i++ {
		a[i] = a[i-1] + rand.Int()%10
		fmt.Fprint(bufout, a[i], " ")
	}

	fmt.Fprintln(bufout)

	b = make([]int, m)
	b[0] = rand.Int()%10 - 20
	fmt.Fprint(bufout, b[0], " ")
	for i := 1; i < m; i++ {
		b[i] = b[i-1] + rand.Int()%10
		fmt.Fprint(bufout, b[i], " ")
	}

	fmt.Fprintln(bufout)
}

func test() {
	rand.Seed(time.Now().UTC().UnixNano())

	for i := 0; i < 10000; i++ {
		fmt.Fprintln(bufout, "\n>>> Test", i+1)
		gen()
		r := doit()
		e := brute()
		g := r == e
		fmt.Fprintln(bufout, r, e, g)
		if !g {
			break
		}
	}
}

func solve() {
	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	r := doit()
	fmt.Fprintln(bufout, r)
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	// test()
	solve()
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer
var n, m int
var a, b []int

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

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	r := 0
	j := 0
	for i := 0; i < n; i++ {
		for j+1 < m && abs(a[i]-b[j]) >= abs(a[i]-b[j+1]) {
			j++
		}
		d := abs(a[i] - b[j])
		if r < d {
			r = d
		}
	}

	fmt.Fprintln(bufout, r)
}

package main

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

func ternary(a int, m int, b []int) int {
	f := func(j int) int {
		if a < b[j] {
			return b[j] - a
		}
		return a - b[j]
	}

	left := 0
	right := m - 1

	for right-left >= 3 {
		j1 := left + (right-left)/3
		j2 := right - (right-left)/3
		if f(j1) < f(j2) {
			right = j2
		} else {
			left = j1
		}
	}

	minimum := f(left)
	for j := left + 1; j <= right; j++ {
		d := f(j)
		if d < minimum {
			minimum = d
		}
	}

	return minimum
}

func solution(n int, m int, a []int, b []int) int {
	p := 1
	for i := 1; i < m; i++ {
		if b[i] > b[i-1] {
			b[p] = b[i]
			p++
		}
	}
	m = p

	result := 0
	for i := 0; i < n; i++ {
		d := ternary(a[i], m, b)
		if d > result {
			result = d
		}
	}

	return result
}

func main() {
	var bufin *bufio.Reader
	var bufout *bufio.Writer
	var n, m int
	var a, b []int

	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	fmt.Fprintln(bufout, solution(n, m, a, b))
}

package main

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

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

func f(x int, y int) int {
	if x < y {
		return y - x
	}
	return x - y
}

func nearest(x int) int {
	left := 0
	right := m - 1

	for right-left >= 3 {
		i1 := left + (right-left)/3
		i2 := right - (right-left)/3

		y1 := b[i1]
		y2 := b[i2]
		if f(x, y1) < f(x, y2) {
			right = i2
		} else {
			left = i1
		}
	}

	best := f(x, b[left])
	for i := left + 1; i <= right; i++ {
		d := f(x, b[i])
		if d < best {
			best = d
		}
	}

	return best
}

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

	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	r = 0
	for i := 0; i < n; i++ {
		d := nearest(a[i])
		if d > r {
			r = d
		}
	}

	fmt.Fprint(bufout, r)
}

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer
var n, m int
var a, b []int

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

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	r := 0
	j := 0
	for i := 0; i < n; i++ {
		for j+1 < m && abs(a[i]-b[j]) > abs(a[i]-b[j+1]) {
			j++
		}
		d := abs(a[i] - b[j])
		if r < d {
			r = d
		}
	}

	fmt.Fprintln(bufout, r)
}

package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"time"
)

var bufin *bufio.Reader
var bufout *bufio.Writer
var n, m, n1, m1 int
var a, b, a1, b1 []int

func f(x int, y int) int {
	if x < y {
		return y - x
	}
	return x - y
}

func nearest(x int) int {
	left := 0
	right := m - 1

	for right-left >= 3 {
		i1 := left + (right-left)/3
		i2 := right - (right-left)/3
		if f(x, b[i1]) < f(x, b[i2]) {
			right = i2
		} else {
			left = i1
		}
	}

	best := f(x, b[left])
	for i := left + 1; i <= right; i++ {
		d := f(x, b[i])
		if d < best {
			best = d
		}
	}

	return best
}

func doit() int {
	n1 = 0
	a1 = make([]int, n)
	for i := 0; i < n; i++ {
		if (i == 0) || (a[i] != a[i-1]) {
			a1[n1] = a[i]
			n1++
		}
	}

	m1 = 0
	b1 = make([]int, m)
	for i := 0; i < m; i++ {
		if (i == 0) || (b[i] != b[i-1]) {
			b[m1] = b[i]
			m1++
		}
	}

	n = n1
	m = m1
	a = a1
	b = b1

	r := 0
	for i := 0; i < n; i++ {
		d := nearest(a[i])
		if d > r {
			r = d
		}
	}
	return r
}

func covered(x int, r int) bool {
	for i := 0; i < m; i++ {
		if f(x, b[i]) <= r {
			return true
		}
	}
	return false
}

func good(r int) bool {
	for i := 0; i < n; i++ {
		if !covered(a[i], r) {
			return false
		}
	}
	return true
}

func brute() int {
	r := 0
	for !good(r) {
		r++
	}
	return r
}

func gen() {
	n = rand.Int()%20 + 1
	m = rand.Int()%20 + 1
	fmt.Fprintln(bufout, n, m)

	a = make([]int, n)
	a[0] = rand.Int()%10 - 20
	fmt.Fprint(bufout, a[0], " ")
	for i := 1; i < n; i++ {
		a[i] = a[i-1] + rand.Int()%10
		fmt.Fprint(bufout, a[i], " ")
	}

	fmt.Fprintln(bufout)

	b = make([]int, m)
	b[0] = rand.Int()%10 - 20
	fmt.Fprint(bufout, b[0], " ")
	for i := 1; i < m; i++ {
		b[i] = b[i-1] + rand.Int()%10
		fmt.Fprint(bufout, b[i], " ")
	}

	fmt.Fprintln(bufout)
}

func test() {
	rand.Seed(time.Now().UTC().UnixNano())

	for i := 0; i < 10000; i++ {
		fmt.Fprintln(bufout, "\n>>> Test", i+1)
		gen()
		r := doit()
		e := brute()
		g := r == e
		fmt.Fprintln(bufout, r, e, g)
		if !g {
			break
		}
	}
}

func solve() {
	fmt.Fscan(bufin, &n, &m)

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

	b = make([]int, m)
	for i := 0; i < m; i++ {
		fmt.Fscan(bufin, &b[i])
	}

	r := doit()
	fmt.Fprintln(bufout, r)
}

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

	// test()
	solve()
}