There are n pictures delivered for the new exhibition. The i-th painting has beauty ai. We know that a visitor becomes happy every time he passes from a painting to a more beautiful one.We are allowed to arranged pictures in any order. What is the maximum possible number of times the visitor may become happy while passing all pictures from first to last? In other words, we are allowed to rearrange elements of a in any order. What is the maximum possible number of indices i (1 ≤ i ≤ n - 1), such that ai + 1 > ai.

The first line of the input contains integer n (1 ≤ n ≤ 1000) — the number of painting. The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai means the beauty of the i-th painting.

Print one integer — the maximum possible number of neighbouring pairs, such that ai + 1 > ai, after the optimal rearrangement.

NoteIn the first sample, the optimal order is: 10, 20, 30, 40, 50.In the second sample, the optimal order is: 100, 200, 100, 200.

package main

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

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

	paintings := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; scanner.Scan() && i < n; i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		paintings[i] = a
	}

	merge_sort(paintings, 0, n-1)

	happiness := 0
	finished := false
	for !finished {
		finished = true

		last := -1
		for i := 0; i < n; i++ {
			if paintings[i] != -1 {
				finished = false

				if last < 0 {
					last = paintings[i]
					paintings[i] = -1
				} else if last > 0 && paintings[i] > last {
					happiness++
					last = paintings[i]
					paintings[i] = -1
				}
			}
		}
	}

	fmt.Println(happiness)
}

func merge(buffer []int, start, middle, end int) {
	sorted := make([]int, end-start+1)

	i := start
	j := middle + 1
	k := 0
	for i <= middle && j <= end {
		if buffer[i] <= buffer[j] {
			sorted[k] = buffer[i]
			i++
		} else {
			sorted[k] = buffer[j]
			j++
		}
		k++
	}
	for i <= middle {
		sorted[k] = buffer[i]
		k++
		i++
	}
	for j <= end {
		sorted[k] = buffer[j]
		k++
		j++
	}

	i = start
	k = 0
	for i <= end {
		buffer[i] = sorted[k]
		i++
		k++
	}
}

func merge_sort(buffer []int, start, end int) {
	if end > start {
		middle := (end + start) / 2
		merge_sort(buffer, start, middle)
		merge_sort(buffer, middle+1, end)
		merge(buffer, start, middle, end)
	}
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	sort.Ints(bt)
	ans := 0
	for i := 0; i < pictures; i++ {
		count := 0
		// fmt.Println(have)
		if have[bt[i]] > 0 {
			prev := bt[i]
			have[prev]--
			for j := i + 1; j < pictures; j++ {
				if have[bt[j]] > 0 && bt[j] > prev {
					// fmt.Print(prev, " ")
					count++
					have[bt[j]]--
					prev = bt[j]
				}
			}
		}
		ans += count
	}

	fmt.Println(ans)
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	// fmt.Println(len(have))
	pairs, lens := 0, len(have)
	for {
		if len(have) <= 1 {
			fmt.Println(pairs)
			return
		}

		pairs += lens - 1
		for key := range have {
			have[key]--
			// fmt.Println(":-", key, have[key])
			if have[key] < 1 {
				delete(have, key)
				lens--
			}
		}

		// fmt.Println("->", pairs, lens)

	}
}

package main

import (
	"fmt"
)

func findMin(a []int, u int) int {
	ret := -1
	for i := 0; i < len(a); i++ {
		if a[i] > u && (ret == -1 || a[i] < a[ret]) {
			ret = i
		}
	}
	return ret
}

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}
	m := findMin(a, -1)
	b := make([]int, n)
	b[0] = a[m]
	a[m] = 0
	ans := 0
	for i := 1; i < n; i++ {
		m = findMin(a, b[i-1])
		if m == -1 {
			m = findMin(a, 0)
		} else {
			ans++
		}
		b[i] = a[m]
		a[m] = 0
	}
	fmt.Println(ans)
}

package main

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

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	scanner.Scan()
	input := scanner.Text()
	numbers := strings.Split(input, " ")

	m := make(map[string]int)
	numElements := 0
	// populate m with frequency
	for _, v := range numbers {
		if _, ok := m[v]; ok {
			m[v]++
		} else {
			m[v] = 1
			numElements++
		}
	}

	happy := 0
	for {

		if numElements <= 1 {
			fmt.Println(happy)
			return
		}
		happy += numElements - 1
		for k, _ := range m {
			m[k]--
			if m[k] <= 0 {
				delete(m, k)
				numElements--
			}
		}
	}

}

package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	paintings := make([]int, n)
	exists := make(map[int]int)

	for i := 0; i < n; i++ {
		var beauty int
		fmt.Scan(&beauty)
		if _, ok := exists[beauty]; !ok {
			paintings = append(paintings, beauty)
		}
		exists[beauty]++
	}

	sort.Sort(sort.IntSlice(paintings))
	var total int

	for len(exists) > 0 {
		for _, p := range paintings {
			if _, ok := exists[p]; ok {
				exists[p]--
				total++
				if exists[p] <= 0 {
					delete(exists, p)
				}
			}
		}
		total--
	}
	fmt.Println(total)
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	sort.Ints(bt)
	ans := 0
	for i := 0; i < pictures; i++ {
		count := 0
		if have[bt[i]] > 0 {
			for j := i + 1; j < pictures; j++ {
				if bt[j] > 0 && bt[j] > bt[i] {
					count++
					bt[j], bt[i] = bt[j]-1, bt[i]-1
				}
			}
		}

		ans += count
	}

	fmt.Println(ans)
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	sort.Ints(bt)
	ans := 0
	for i := 0; i < pictures; i++ {
		count := 0
		// fmt.Println(have)
		if have[bt[i]] > 0 {
			prev := bt[i]
			for j := i + 1; j < pictures; j++ {
				if have[bt[j]] > 0 && bt[j] > prev {
					// fmt.Print(prev, " ")
					count++
					have[bt[j]]--
					prev = bt[j]
				}
			}
		}
		ans += count
	}

	fmt.Println(ans)
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	sort.Ints(bt)
	ans := 0
	for i := 0; i < pictures; i++ {
		count := 0
		if have[bt[i]] > 0 {
			for j := i + 1; j < pictures; j++ {
				if have[bt[j]] > 0 && bt[j] > bt[i] {
					count++
					have[bt[j]], have[bt[i]] = have[bt[j]]-1, have[bt[i]]-1
				}
			}
		}

		ans += count
	}

	fmt.Println(ans)
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	ans := 0
	for i := 0; i < pictures; i++ {
		count := 0
		if have[bt[i]] > 0 {
			for j := i + 1; j < pictures; j++ {
				if bt[j] > 0 && bt[j] > bt[i] {
					count++
					bt[j], bt[i] = bt[j]-1, bt[i]-1
				}
			}
		}

		ans += count
	}

	fmt.Println(ans)
}

package main

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

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

	have := make(map[int]int)
	bt := make([]int, pictures)

	for i := 0; i < pictures; i++ {
		fmt.Fscan(reader, &bt[i])
		have[bt[i]]++
	}

	sort.Ints(bt)
	ans := 0
	for i := 0; i < pictures; i++ {
		count := 0
		// fmt.Println(have)
		if have[bt[i]] > 0 {
			prev := bt[i]
			for j := i + 1; j < pictures; j++ {
				if have[bt[j]] > 0 && bt[j] > prev {
					// fmt.Print(prev, " ")
					count++
					have[bt[j]]--
					prev = bt[j]
				}
			}
		}
		fmt.Println()
		ans += count
	}

	fmt.Println(ans)
}

package main

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

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	input := scanner.Text()
	numbers := strings.Split(input, " ")

	m := make(map[string]int)
	numElements := 0
	// populate m with frequency
	for _, v := range numbers {
		if _, ok := m[v]; ok {
			m[v]++
		} else {
			m[v] = 1
			numElements++
		}
	}

	happy := 0
	for {
		if numElements == 1 {
			fmt.Println(happy)
			return
		}
		happy += numElements - 1
		for k, _ := range m {
			m[k]--
			if m[k] <= 0 {
				delete(m, k)
				numElements--
			}
		}
	}

}


To become the king of Codeforces, Kuroni has to solve the following problem.He is given $$$n$$$ numbers $$$a_1, a_2, \dots, a_n$$$. Help Kuroni to calculate $$$\prod_{1\le i<j\le n} |a_i - a_j|$$$. As result can be very big, output it modulo $$$m$$$.If you are not familiar with short notation, $$$\prod_{1\le i<j\le n} |a_i - a_j|$$$ is equal to $$$|a_1 - a_2|\cdot|a_1 - a_3|\cdot$$$ $$$\dots$$$ $$$\cdot|a_1 - a_n|\cdot|a_2 - a_3|\cdot|a_2 - a_4|\cdot$$$ $$$\dots$$$ $$$\cdot|a_2 - a_n| \cdot$$$ $$$\dots$$$ $$$\cdot |a_{n-1} - a_n|$$$. In other words, this is the product of $$$|a_i - a_j|$$$ for all $$$1\le i < j \le n$$$.

The first line contains two integers $$$n$$$, $$$m$$$ ($$$2\le n \le 2\cdot 10^5$$$, $$$1\le m \le 1000$$$) — number of numbers and modulo. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$).

Output the single number — $$$\prod_{1\le i<j\le n} |a_i - a_j| \bmod m$$$.

NoteIn the first sample, $$$|8 - 5| = 3 \equiv 3 \bmod 10$$$.In the second sample, $$$|1 - 4|\cdot|1 - 5|\cdot|4 - 5| = 3\cdot 4 \cdot 1 = 12 \equiv 0 \bmod 12$$$.In the third sample, $$$|1 - 4|\cdot|1 - 9|\cdot|4 - 9| = 3 \cdot 8 \cdot 5 = 120 \equiv 1 \bmod 7$$$.

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func fastPow(a, p, mod int) int {
	d := 1 % mod
	for p > 0 {
		if (p & 1) == 1 {
			d *= a
			d %= mod
		}
		a *= a
		a %= mod
		p >>= 1
	}
	return d
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n, m := NextInt(), NextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}
	if n > m {
		fmt.Fprintln(out, 0)
		return
	}
	sort.Ints(a)
	counts := make([]int, m)
	res := make([]int, m)
	for i := n - 1; i >= 0; i-- {
		t := a[i] % m
		start := -t
		if start < 0 {
			start += m
		}
		for i := 0; i < m; i++ {
			// stderr("adding to", start, counts[i])
			res[start] += counts[i]
			start++
			if start == m {
				start = 0
			}
		}
		// stderr("increast", t)
		counts[t]++
	}
	// x := m
	// phi := 1
	// for j := 2; j*j <= x; j++ {
	// 	if x%j == 0 {
	// 		pk := 1
	// 		for x%j == 0 {
	// 			x /= j
	// 			pk *= j
	// 		}
	// 		phi *= pk / j * (j - 1)
	// 	}
	// }
	// if x != 1 {
	// 	phi *= x - 1
	// }
	// stderr(phi)
	total := 1
	for i := 0; i < m; i++ {
		// stderr("fastpow", i, res[i]%phi)
		total *= fastPow(i, res[i], m)
		total %= m
	}
	fmt.Fprintln(out, total)
}

// https://codeforces.com/contest/1352/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Println(solver())
}

func solver() int {
	var n, m int
	fmt.Fscan(input, &n, &m)
	a := make([]int, n)
	maa := make(map[int]bool)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
		if maa[a[i]] {
			return 0
		}
		maa[a[i]] = true
	}

	if len(maa) > m {
		return 0
	}

	ans := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			ans = (ans * ((a[i] - a[j]) % m)) % m
		}
	}

	return abs(ans)
}

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

package main

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

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

    var n, m int
    fmt.Fscan(in, &n, &m)
    a := make([]int, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    if n > m {
        fmt.Fprintln(out, 0)
        return
    }
    prd, dif := 1, 0
    for i := 0; i + 1 < n; i++ {
        for j := i + 1; j < n; j++ {
            dif = a[i] - a[j]
            if dif < 0 {
                dif = -dif
            }
            dif %= m
            prd = prd * dif % m
        }
    }
    fmt.Fprintln(out, prd)
}

package main

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

func main() {
	var n, m int64
	fmt.Scanf("%d %d\n", &n, &m)
	reader := bufio.NewReader(os.Stdin)
	line := readLine(reader)
	ans := strings.Split(string(line), " ")
	an := make([]int64, n)
	mm := make(map[int64]struct{})
	for i := int64(0); i < n; i++ {
		x, _ := strconv.ParseInt(ans[i], 10, 64)
		an[i] = x
		if _, ok := mm[x];ok {
			fmt.Println(0)
			return
		}
		if _, ok := mm[x+m];ok {
			fmt.Println(0)
			return
		}
		if _, ok := mm[x-m];ok {
			fmt.Println(0)
			return
		}
		mm[x] = struct{}{}
	}
	result := int64(1)
	for i := int64(0); i < n-1; i++ {
		for j := i+1; j < n; j++ {
			mul := an[i] - an[j]
			if mul < 0 {
				mul = -mul
			}
			result = result * mul
			result = result % m
			if result == 0 {
				fmt.Println(0)
				return
			}
		}
	}
	fmt.Println(result)
}

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

/*
URL:
https://codeforces.com/problemset/problem/1305/C
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	n, m int64
	A    []int64
)

func main() {
	n, m = ReadInt64_2()
	A = ReadInt64Slice(int(n))

	if n <= m {
		solve()
	} else {
		fmt.Println(0)
	}
}

func solve() {
	res := int64(1)
	for i := int64(0); i < n; i++ {
		for j := i + 1; j < n; j++ {
			res *= AbsInt(A[i] - A[j])
			res %= m
		}
	}
	fmt.Println(res)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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

package main

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

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

	var n int
	var mod int64
	Fscan(in, &n, &mod)
	a := make([]int64, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
	abs := func(x int64) int64 {
		if x < 0 {
			return -x
		}
		return x
	}
	if n < 2000 {
		ans := int64(1)
		for i := 0; i < n; i++ {
			for j := i + 1; j < n; j++ {
				ans *= abs(a[i] - a[j])
				ans %= mod
			}
		}
		Fprintln(out, ans)
	} else {
		Fprintln(out, 0)
	}
}

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

package main

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

func main() {

    n, m := scanInt64(), scanInt64()
    givenArray := scanInt64Slice(int(n))
    answer := int64(1)
    if n > m{
		printFmt(0)
		return
	}
    for i := int64(0); i < n; i++{
    	for j := i+1; j < n; j++{
    		tatkalVariable := absoluteValue(givenArray[i] - givenArray[j]) % m
    		answer = answer * tatkalVariable % m
		}
	}
	printFmt(answer)
	close(channelType)
	oWg.Wait()
}

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

var channelType chan string
var in *bufio.Scanner
var oWg *sync.WaitGroup
var printFmt func(a ...interface{}) (n int, err error)

func init() {
	printFmt = fmt.Println

	in = bufio.NewScanner(os.Stdin)
	in.Buffer(make([]byte, 1024), int(1e+9))
	in.Split(bufio.ScanWords)

	channelType = make(chan string, 16)
	oWg = &sync.WaitGroup{}
	oWg.Add(1)

	writer := bufio.NewWriter(os.Stdout)
	go func() {
		defer oWg.Done()
		defer writer.Flush()

		for line := range channelType {
			writer.WriteString(line + "\n")
		}
	}()
}

func scanString() string {in.Scan(); return in.Text() }
func scanInt() int {i, _ := strconv.Atoi(scanString()); return i}
func scanInt64() int64 {i, _ := strconv.ParseInt(scanString(), 10, 64); return i}
func scanStringSlice(n int) []string {s := make([]string, n); for i := 0; i < n; i++ {s[i] = scanString()}; return s}
func scanIntSlice(n int) []int {arr := make([]int, n); for i := 0; i < n; i++ {arr[i] = scanInt()}; return arr}
func scanInt64Slice(n int) []int64 {arr := make([]int64, n); for i := 0; i < n; i++ {arr[i] = scanInt64()}; return arr}

/*********************** Utils ***********************/

func stringEquivalent(v interface{}) string {return fmt.Sprintf("%v", v)}
func spaceSeperatedStringFromArray(a []int) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.Itoa(a[i]) + " ")}; return sb.String()}
func spaceSeperatedStringFromArray64(a []int64) string {sb := strings.Builder{}; sb.Grow(len(a) * 9); for i := range a {sb.WriteString(strconv.FormatInt(a[i], 10) + " ")}; return sb.String()}
func minMaxFromArray(args ...int64) (int64, int64) {minVal, maxVal := int64(math.MaxInt64), int64(math.MinInt64); for _, v := range args {if v < minVal {minVal = v}; if v > maxVal {maxVal = v}}; return minVal, maxVal}
func minElementFromArray(args ...int64) int64 { mn, _ := minMaxFromArray(args...); return mn }
func maxElementFromArray(args ...int64) int64 { _, mx := minMaxFromArray(args...); return mx }
func lcmCalculation(a, b int64) int64 {return a / gcdCalculation(a, b) * b}
func gcdCalculation(a, b int64) int64 {if (a == 0) {return b}; return gcdCalculation(b%a, a)}
func sortInt64Slice(a []int64) {sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })}
func absoluteValue(x int64) int64 {if x < 0 {return -x}; return x}

package main
import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
func scanUint(s *bufio.Scanner) uint64 {
	s.Scan()
	x, _ := strconv.ParseUint(s.Text(), 10, 64)
	return  x
}
func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Buffer(make([]byte, 1<<12), 1<<20)
	s.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	n, m := scanUint(s),scanUint(s)

	a := make([]uint64, n)
	for i := uint64(0); i < n; i++ {
		a[i] = scanUint(s)
	}
	var b uint64 = 1
	if n <= m {
		for i := uint64(0); i < n-1; i++ {
			for j := uint64(i+1); j < n; j++{
				var c uint64
				if a[i] > a[j] {
					c = a[i] - a[j]
				} else {
					c = a[j] - a[i]
				}
				b = (b * c)%m
			}
		}
	}else {
		b = 0
	}
	fmt.Fprintln(w, b)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1305/C

package main

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

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

func Solve(n int, m int, a []int64) int64 {
	if n > m {
		return 0
	}
	product := int64(1)
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			product = product * abs(a[j]-a[i]) % int64(m)
		}
	}
	return product
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &a[i])
	}

	fmt.Fprintf(writer, "%d\n", Solve(n, m, a))
}

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

func fastPow(a, p, mod int) int {
	d := 1
	for p > 0 {
		if (p & 1) == 1 {
			d *= a
			d %= mod
		}
		a *= a
		p >>= 1
	}
	return d
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n, m := NextInt(), NextInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = NextInt()
	}
	if n > m {
		fmt.Fprintln(out, 0)
		return
	}
	sort.Ints(a)
	counts := make([]int, m)
	res := make([]int, m)
	for i := n - 1; i >= 0; i-- {
		t := a[i] % m
		start := -t
		if start < 0 {
			start += m
		}
		for i := 0; i < m; i++ {
			// stderr("adding to", start, counts[i])
			res[start] += counts[i]
			start++
			if start == m {
				start = 0
			}
		}
		// stderr("increast", t)
		counts[t]++
	}
	x := m
	phi := 1
	for j := 2; j*j <= x; j++ {
		if x%j == 0 {
			pk := 1
			for x%j == 0 {
				x /= j
				pk *= j
			}
			phi *= pk / j * (j - 1)
		}
	}
	if x != 1 {
		phi *= x - 1
	}
	stderr(phi)
	total := 1
	for i := 0; i < m; i++ {
		total *= fastPow(i, res[i]%phi, m)
		total %= m
	}
	fmt.Fprintln(out, total)
}

// https://codeforces.com/contest/1352/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Println(solver())
}

func solver() int {
	var n, m int
	fmt.Fscan(input, &n, &m)
	a := make([]int, n)
	maa := make(map[int]bool)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
		maa[a[i]] = true
	}

	if len(maa) > m {
		return 0
	}

	ans := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			ans = (ans * (a[i] - a[j])) % m
		}
	}

	return abs(ans)
}

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

// https://codeforces.com/contest/1352/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Println(solver())
}

func solver() int {
	var n, m int
	fmt.Fscan(input, &n, &m)
	a := make([]int, n)
	maa := make(map[int]bool)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
		maa[a[i]] = true
	}

	if len(maa) >= m {
		return 0
	}

	ans := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			ans = (ans * ((a[i] - a[j]) % m)) % m
		}
	}

	return abs(ans)
}

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

// https://codeforces.com/contest/1352/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Println(solver())
}

func solver() int {
	var n, m int
	fmt.Fscan(input, &n, &m)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
	}

	if n >= m {
		return 0
	}

	ans := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			ans = (ans * (a[i] - a[j])) % m
		}
	}

	return abs(ans)
}

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

// https://codeforces.com/contest/1352/problem/C
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
var input io.Reader

func main() {
	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input = bufio.NewReader(os.Stdin)
	fmt.Println(solver())
}

func solver() int {
	var n, m int
	fmt.Fscan(input, &n, &m)
	a := make([]int, n)
	maa := make(map[int]bool)
	for i := 0; i < n; i++ {
		fmt.Fscan(input, &a[i])
		maa[a[i]] = true
	}

	if len(maa) >= m {
		return 0
	}

	ans := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			ans = (ans * (a[i] - a[j])) % m
		}
	}

	return abs(ans)
}

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

package main

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

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

    var n, m int
    fmt.Fscan(in, &n, &m)
    a := make([]int, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    if n > m {
        fmt.Fprintln(out, 0)
        return
    }
    prd, dif := 1, 0
    for i := 0; i + 1 < n; i++ {
        for j := i + 1; j < n; j++ {
            dif = a[i] - a[j]
            if dif < 0 {
                dif = -dif
            }
            prd = prd * dif % m
        }
    }
    fmt.Fprintln(out, prd)
    out.Flush()
}

package main

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

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

    var n, m int
    fmt.Fscan(in, &n, &m)
    a := make([]int, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    if n > m {
        fmt.Fprintln(out, 0)
        return
    }
    prd, dif := 1, 0
    for i := 0; i + 1 < n; i++ {
        for j := i + 1; j < n; j++ {
            dif = a[i] - a[j]
            if dif < 0 {
                dif = -dif
            }
            dif %= m
            prd = prd * dif % m
        }
    }
    fmt.Fprintln(out, prd)
    out.Flush()
}

package main

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

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

    var n, m int
    fmt.Fscan(in, &n, &m)
    a := make([]int, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    if n >= m {
        fmt.Fprintln(out, 0)
        return
    }
    prd, dif := 1, 0
    for i := 0; i + 1 < n; i++ {
        for j := i + 1; j < n; j++ {
            dif = a[i] - a[j]
            if dif < 0 {
                dif = -dif
            }
            prd = prd * dif % m
        }
    }
    fmt.Fprintln(out, prd)
    out.Flush()
}

package main
import (
	"bufio"
	"fmt"
	"os"
	"math"
)
func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n, m int
	fmt.Fscan(r, &n, &m)
	a := make([]int, n)
	c := make([]int, 0, n*n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	for i := 0; i < n-1; i++ {
		for j := i+1; j < n; j++{
			c = append(c, int(math.Abs(float64(a[i] - a[j]))))
		}
	}
	b := 1
	for _, e := range c {
		if e == 0 {
			b = 0
			break
		}
		b = (b * e)%m
	}
	fmt.Fprintln(w, b)
}

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

	var n, m uint64
	fmt.Fscan(r, &n, &m)
	a := make([]uint64, n)
	c := make([]uint64, 0, n*n)
	for i := uint64(0); i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	for i := uint64(0); i < n-1; i++ {
		for j := uint64(i+1); j < n; j++{
			c = append(c, a[i] - a[j])
		}
	}
	var b uint64 = c[0]
	for _, e := range c[1:] {
		if e == 0 {
			b = 0
			break
		}
		b = (b * uint64(e))%m
	}
	fmt.Fprintln(w, b)
}

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

	var n, m uint64
	fmt.Fscan(r, &n, &m)
	a := make([]uint64, n)
	c := make([]uint64, 0, n*n)
	for i := uint64(0); i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	for i := uint64(0); i < n-1; i++ {
		for j := uint64(i+1); j < n; j++{
			c = append(c, a[i] - a[j])
		}
	}
	var b uint64 = 1
	for _, e := range c {
		if e == 0 {
			b = 0
			break
		}
		b = (b * uint64(e))%m
	}
	fmt.Fprintln(w, b)
}

package main
import (
	"bufio"
	"fmt"
	"os"
	"math"
)
func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n, m int
	fmt.Fscan(r, &n, &m)
	a := make([]int, n)
	c := make([]int, 0, n*n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	for i := 0; i < n-1; i++ {
		for j := i+1; j < n; j++{
			c = append(c, int(math.Abs(float64(a[i] - a[j]))))
		}
	}
	b := 1
	for _, e := range c {
		b *= e
		if b >= m {
			b %= m
		}
	}
	fmt.Fprintln(w, b)
}

package main
import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
func scanUint(s *bufio.Scanner) uint64 {
	s.Scan()
	x, _ := strconv.ParseUint(s.Text(), 10, 64)
	return  x
}
func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Buffer(make([]byte, 1<<12), 1<<20)
	s.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	n, m := scanUint(s),scanUint(s)

	a := make([]uint64, n)
	for i := uint64(0); i < n; i++ {
		a[i] = scanUint(s)
	}
	var b uint64 = 1
	if n < m {
		for i := uint64(0); i < n-1; i++ {
			for j := uint64(i+1); j < n; j++{
				var c uint64
				if a[i] > a[j] {
					c = a[i] - a[j]
				} else {
					c = a[j] - a[i]
				}
				b = (b * c)%m
			}
		}
	}else {
		b = 0
	}
	fmt.Fprintln(w, b)
}

package main
import (
	"bufio"
	"fmt"
	"os"
	"math"
)
func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var n, m int
	fmt.Fscan(r, &n, &m)
	a := make([]int, n)
	c := make([]int, 0, n*n)
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &a[i])
	}
	for i := 0; i < n-1; i++ {
		for j := i+1; j < n; j++{
			c = append(c, int(math.Abs(float64(a[i] - a[j]))))
		}
	}
	b := 1
	for _, e := range c {
		if e == 0 {
			b = 0
			break
		}
		b *= e
		if b >= m {
			b %= m
		}
	}
	fmt.Fprintln(w, b)
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1305/C

package main

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

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

func Solve(n, m int, a []int) int {
	if n > m {
		return 0
	}
	product := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			product *= abs(a[j]-a[i]) % m
		}
	}
	return product % m
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, m, a))
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1305/C

package main

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

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

func Solve(n, m int, a []int) int {
	if n > m {
		return 0
	}
	product := 1
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			product = (product * abs(a[j]-a[i])) % m
		}
	}
	return product
}

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

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

	fmt.Fprintf(writer, "%d\n", Solve(n, m, a))
}


Every evening Vitalya sets n alarm clocks to wake up tomorrow. Every alarm clock rings during exactly one minute and is characterized by one integer ai — number of minute after midnight in which it rings. Every alarm clock begins ringing at the beginning of the minute and rings during whole minute. Vitalya will definitely wake up if during some m consecutive minutes at least k alarm clocks will begin ringing. Pay attention that Vitalya considers only alarm clocks which begin ringing during given period of time. He doesn't consider alarm clocks which started ringing before given period of time and continues ringing during given period of time.Vitalya is so tired that he wants to sleep all day long and not to wake up. Find out minimal number of alarm clocks Vitalya should turn off to sleep all next day. Now all alarm clocks are turned on.

First line contains three integers n, m and k (1 ≤ k ≤ n ≤ 2·105, 1 ≤ m ≤ 106) — number of alarm clocks, and conditions of Vitalya's waking up.  Second line contains sequence of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 106) in which ai equals minute on which i-th alarm clock will ring. Numbers are given in arbitrary order. Vitalya lives in a Berland in which day lasts for 106 minutes.

Output minimal number of alarm clocks that Vitalya should turn off to sleep all next day long.

NoteIn first example Vitalya should turn off first alarm clock which rings at minute 3.In second example Vitalya shouldn't turn off any alarm clock because there are no interval of 10 consequence minutes in which 3 alarm clocks will ring.In third example Vitalya should turn off any 6 alarm clocks.

package main

import (
  //"fmt"
  //"strings"
  "sort"
  "bufio"
  "os"
  "strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func PrintInts(ints ...int) {
  for _, value := range ints {
    writer.WriteString(strconv.Itoa(value))
    writer.WriteByte(' ')
  }
}

var n, m, k, val, ans, left, cnt int
var list = []int{}
var kept = []int{}

func main() {
  defer writer.Flush()
  scanner.Split(bufio.ScanWords)

  n = ReadInt32();
  m = ReadInt32();
  k = ReadInt32();
  for i := 0; i < n; i++ {
    val = ReadInt32()
    list = append(list, val)
  }
  if (k == 1) {
    PrintInts(n)
    return
  }
  sort.Sort(sort.IntSlice(list))
  ans = 0
  left = 0
  cnt = 1
  kept = append(kept, list[0])
  for i := 1; i < n; i++ {
    cnt++
    for left < len(kept) && kept[left] + m <= list[i] {
      left++
      cnt--
    }
    if cnt >= k {
      cnt--
      ans++
    } else {
      kept = append(kept, list[i])
    }
  }
  PrintInts(ans)
}

package main

import (
  //"fmt"
  //"strings"
  "sort"
  "bufio"
  "os"
  "strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func PrintInts(ints ...int) {
  for _, value := range ints {
    writer.WriteString(strconv.Itoa(value))
    writer.WriteByte(' ')
  }
}

var n, m, k, val int
var list = []int{}

func main() {
  defer writer.Flush()
  scanner.Split(bufio.ScanWords)

  n = ReadInt32();
  m = ReadInt32();
  k = ReadInt32();
  for i := 0; i < n; i++ {
    val = ReadInt32()
    list = append(list, val)
  }
  if (k == 1) {
    PrintInts(n)
    return
  }
  sort.Sort(sort.IntSlice(list))
  var ans = 0
  var left = 0
  var cnt = 1
  for i := 1; i < n; i++ {
    cnt++
    for list[left] <= list[i] - m {
      left++
      cnt--
    }
    if cnt >= k {
      cnt--
      ans++
    }
  }
  PrintInts(ans)
}

package main

import (
  //"fmt"
  //"strings"
  "sort"
  "bufio"
  "os"
  "strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func PrintInts(ints ...int) {
  for _, value := range ints {
    writer.WriteString(strconv.Itoa(value))
    writer.WriteByte(' ')
  }
}

var n, m, k, val, ans, left, cnt int
var list = []int{}

func main() {
  defer writer.Flush()
  scanner.Split(bufio.ScanWords)

  n = ReadInt32();
  m = ReadInt32();
  k = ReadInt32();
  for i := 0; i < n; i++ {
    val = ReadInt32()
    list = append(list, val)
  }
  if (k == 1) {
    PrintInts(n)
    return
  }
  sort.Sort(sort.IntSlice(list))
  ans = 0
  left = 0
  cnt = 1
  for i := 1; i < n; i++ {
    cnt++
    for list[left] <= list[i] - m {
      left++
      cnt--
    }
    if cnt >= k {
      cnt--
      ans++
    }
  }
  PrintInts(ans)
}

package main

import (
  //"fmt"
  //"strings"
  "sort"
  "bufio"
  "os"
  "strconv"
)
var scanner *bufio.Scanner = bufio.NewScanner(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

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

func PrintInts(ints ...int) {
  for _, value := range ints {
    writer.WriteString(strconv.Itoa(value))
    writer.WriteByte(' ')
  }
}

var n, m, k, val int
var list = []int{}

func main() {
  defer writer.Flush()
  scanner.Split(bufio.ScanWords)

  n = ReadInt32();
  m = ReadInt32();
  k = ReadInt32();
  for i := 0; i < n; i++ {
    val = ReadInt32()
    list = append(list, val)
  }
  if (k == 1) {
    PrintInts(0)
    return
  }
  sort.Sort(sort.IntSlice(list))
  var ans = 0
  var left = 0
  var cnt = 1
  for i := 1; i < n; i++ {
    cnt++
    for list[left] <= list[i] - m {
      left++
      cnt--
    }
    if cnt >= k {
      cnt--
      ans++
    }
  }
  PrintInts(ans)
}


As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes. The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO». You can output any letter in lower case or in upper case.

NoteIn first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles.

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	f := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&f[i])
		f[i]--
	}
	for i := 0; i < n; i++ {
		if f[f[i]] != i && f[f[f[i]]] == i {
			fmt.Println("yes")
			return
		}
	}
	fmt.Println("no")
}

package main

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

func solve() {
	n := readInt()
	a := readIntArray(n)
	for i := 0; i < n; i++ {
		vis := make([]bool, n)
		next := i
		level := 0
		for !vis[next] {
			vis[next] = true
			next = a[next] - 1
			level++
		}
		if level == 3 && i == next {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

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()
}

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

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

// 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
}

package main

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

const N = 5005

var vis [N]bool
var a [N]int

func dfs(start, q, cnt int, ok *bool) {

	// fmt.Printf("%d %d %d\n", start, q, cnt)

	if *ok == false {
		return
	}

	if cnt == 4 {
		if start == q {
			*ok = true
		} else {
			*ok = false
		}

		return
	}

	if vis[q] == true {
		*ok = false
		return
	}

	vis[q] = true
	dfs(start, a[q], cnt+1, ok)
	vis[q] = false
}

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)

	var n int

	fmt.Fscan(input, &n)

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

	for i := 0; i < n; i++ {
		// fmt.Printf("%d)\n", i)
		var ok bool = true
		dfs(i, i, 1, &ok)

		if ok == true {
			fmt.Println("YES")
			return
		}
	}

	fmt.Println("NO")
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	r := false
	for i := 0; i < n; i++ {
		if check(a, i+1) {
			r = true
			break
		}
	}

	if r {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

func check(a []int, i int) bool {
	return i == a[a[a[i-1]-1]-1]
}

package main

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

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

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

func solution(reader io.Reader, writer io.Writer) {
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	a := make([]int, n)

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

	for i := 0; i < n; i ++ {
		b := a[i]
		c := a[b]
		if i == a[c] {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

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

func solve() {
	n := readInt()
	a := readIntArray(n)
	for i := 0; i < n; i++ {
		vis := make([]bool, n)
		next := i
		level := 0
		for !vis[next] {
			vis[next] = true
			next = a[next] - 1
			level++
		}
		if level > 2 && i == next {
			fmt.Println("YES")
			return
		}
	}
	fmt.Println("NO")
}

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()
}

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

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

// 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
}

package main

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

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func dfs(g, c []int, v int) bool {
	c[v] = 1
	to := g[v]
	if c[to] == 0 {
		b := dfs(g, c, to)
		if b {
			return b
		}
	} else if c[to] == 1 && g[to] != v {
		return true
	}
	c[v] = 2
	return false
}

func solution(reader io.Reader, writer io.Writer) {
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	f := make([]int, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &f[i])
		f[i]--
	}

	c := make([]int, n)
	for i := 0; i < n; i++ {
		if dfs(f, c, i) {
			fmt.Fprintf(writer, "YES\n")
			return
		}
	}
	fmt.Fprintf(writer, "NO\n")
}

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

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

const INF  = 1000000001

type Pair struct {
	first, second int
}

func sort(arr []int) []int {
	if len(arr) < 2 {
		return arr
	}
	left := 0
	right := len(arr) - 1
	pivot := rand.Int() % len(arr)

	arr[pivot], arr[right] = arr[right], arr[pivot]

	for i := 0; i < len(arr); i++ {
		if arr[i] < arr[right] {
			arr[i], arr[left] = arr[left], arr[i]
			left++
		}
	}

	arr[left], arr[right] = arr[right], arr[left]

	sort(arr[:left])
	sort(arr[left+1:])
	return arr
}

func solution(reader io.Reader, writer io.Writer) {
	var n int
	fmt.Fscanf(reader, "%d\n", &n)

	f := make([]int, n)
	u := make([]bool, n)

	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &f[i])
		f[i]--
	}

	var s, p int
	for i := 0; i < n; i++ {
		if u[s] && f[s] != p {
			fmt.Fprintf(writer, "YES\n")
			return
		}
		u[s] = true
		p = s
		s = f[s]
	}

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

func main() {

	//stdin, err := os.Open("file.in")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("file.out")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)
	defer writer.Flush()

	solution(reader, writer)
}


Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers a1, a2, ..., an of n elements!Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process m queries.Each query is processed in the following way:  Two integers l and r (1 ≤ l ≤ r ≤ n) are specified — bounds of query segment.  Integers, presented in array segment [l,  r] (in sequence of integers al, al + 1, ..., ar) even number of times, are written down.  XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are x1, x2, ..., xk, then Mishka wants to know the value , where  — operator of exclusive bitwise OR. Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented.

The first line of the input contains single integer n (1 ≤ n ≤ 1 000 000) — the number of elements in the array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — array elements. The third line of the input contains single integer m (1 ≤ m ≤ 1 000 000) — the number of queries. Each of the next m lines describes corresponding query by a pair of integers l and r (1 ≤ l ≤ r ≤ n) — the bounds of query segment.

Print m non-negative integers — the answers for the queries in the order they appear in the input.

NoteIn the second sample:There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.In the second query there is only integer 3 is presented even number of times — the answer is 3.In the third query only integer 1 is written down — the answer is 1.In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is .In the fifth query 1 and 3 are written down. The answer is .

package main

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

const maxn = 1000005
const maxm = 1000005

type query struct {
	i int
	l int
	r int
}

type queries []query

func (s queries) Len() int {
	return len(s)
}

func (s queries) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s queries) Less(i, j int) bool {
	return s[i].r < s[j].r
}

func (h *queries) Push(x interface{}) {
	*h = append(*h, x.(query))
}

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

var n, m int
var a, res, xorall, xordist, prev []int
var last map[int]int
var q queries

func update(pos int, val int) {
	for x := pos; x <= n; x += x & -x {
		xordist[x] ^= val
	}
}

func get(pos int) int {
	val := 0
	for x := pos; x >= 1; x -= x & -x {
		val ^= xordist[x]
	}
	return val
}

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

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	a = make([]int, n+1)
	xorall = make([]int, n+1)
	xordist = make([]int, n+1)
	prev = make([]int, n+1)
	last = make(map[int]int)

	xorall[0] = 0
	xordist[0] = 0

	for i := 1; i <= n; i++ {
		in.Scan()
		a[i], _ = strconv.Atoi(in.Text())

		xorall[i] = xorall[i-1] ^ a[i]
		xordist[i] = 0

		prev[i] = last[a[i]]
		last[a[i]] = i
	}
	in.Scan()
	m, _ = strconv.Atoi(in.Text())

	q = make(queries, m)
	res = make([]int, m)

	for i := 0; i < m; i++ {
		q[i].i = i

		in.Scan()
		q[i].l, _ = strconv.Atoi(in.Text())

		in.Scan()
		q[i].r, _ = strconv.Atoi(in.Text())
	}

	h := &q
	heap.Init(h)

	at := 1
	for h.Len() > 0 {
		e := heap.Pop(h).(query)
		for at <= e.r {
			if prev[at] >= 1 {
				update(prev[at], a[at])
			}
			update(at, a[at])
			at++
		}
		xa := xorall[e.r] ^ xorall[e.l-1]
		xd := get(e.r) ^ get(e.l-1)

		res[e.i] = xa ^ xd
	}

	for _, e := range res {
		out.WriteString(strconv.Itoa(e))
		out.WriteByte('\n')
	}
}

package main

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

const maxn = 1000005
const maxm = 1000005

type query struct {
	i int
	l int
	r int
}

type queries []query

func (s queries) Len() int {
	return len(s)
}

func (s queries) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s queries) Less(i, j int) bool {
	return s[i].r < s[j].r
}

var n, m int
var a, res, xorall, xordist, prev []int
var last map[int]int
var q queries

func update(pos int, val int) {
	for x := pos; x <= n; x += x & -x {
		xordist[x] ^= val
	}
}

func get(pos int) int {
	val := 0
	for x := pos; x >= 1; x -= x & -x {
		val ^= xordist[x]
	}
	return val
}

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

	in.Split(bufio.ScanWords)
	defer out.Flush()

	in.Scan()
	n, _ = strconv.Atoi(in.Text())

	a = make([]int, n+1)
	xorall = make([]int, n+1)
	xordist = make([]int, n+1)
	prev = make([]int, n+1)
	last = make(map[int]int)

	xorall[0] = 0
	xordist[0] = 0

	for i := 1; i <= n; i++ {
		in.Scan()
		a[i], _ = strconv.Atoi(in.Text())

		xorall[i] = xorall[i-1] ^ a[i]
		xordist[i] = 0

		prev[i] = last[a[i]]
		last[a[i]] = i
	}

	in.Scan()
	m, _ = strconv.Atoi(in.Text())

	q = make(queries, m)
	res = make([]int, m)

	for i := 0; i < m; i++ {
		q[i].i = i

		in.Scan()
		q[i].l, _ = strconv.Atoi(in.Text())

		in.Scan()
		q[i].r, _ = strconv.Atoi(in.Text())
	}

	sort.Sort(q)

	at := 1
	for _, e := range q {
		for at <= e.r {
			if prev[at] >= 1 {
				update(prev[at], a[at])
			}
			update(at, a[at])
			at++
		}
		xa := xorall[e.r] ^ xorall[e.l-1]
		xd := get(e.r) ^ get(e.l-1)

		res[e.i] = xa ^ xd
	}

	for _, e := range res {
		out.WriteString(strconv.Itoa(e))
		out.WriteByte('\n')
	}
}

package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
	"strconv"
)

const maxn = 1000005
const maxm = 1000005

type query struct {
	i int
	l int
	r int
}

type queries []query

func (s queries) Len() int {
	return len(s)
}

func (s queries) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s queries) Less(i, j int) bool {
	return s[i].r < s[j].r
}

var n, m int
var a, res, xorall, xordist, prev []int
var last map[int]int
var q queries

func update(pos int, val int) {
	for x := pos; x <= n; x += x & -x {
		xordist[x] ^= val
	}
}

func get(pos int) int {
	val := 0
	for x := pos; x >= 1; x -= x & -x {
		val ^= xordist[x]
	}
	return val
}

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

	read(&n)

	a = make([]int, n+1)
	xorall = make([]int, n+1)
	xordist = make([]int, n+1)
	prev = make([]int, n+1)
	last = make(map[int]int)

	xorall[0] = 0
	xordist[0] = 0

	for i := 1; i <= n; i++ {
		read(&a[i])
		xorall[i] = xorall[i-1] ^ a[i]
		xordist[i] = 0

		prev[i] = last[a[i]]
		last[a[i]] = i
	}

	read(&m)

	q = make(queries, m)
	res = make([]int, m)

	for i := 0; i < m; i++ {
		q[i].i = i
		read(&q[i].l)
		read(&q[i].r)
	}

	sort.Sort(q)

	at := 1
	for _, e := range q {
		for at <= e.r {
			if prev[at] >= 1 {
				update(prev[at], a[at])
			}
			update(at, a[at])
			at++
		}
		xa := xorall[e.r] ^ xorall[e.l-1]
		xd := get(e.r) ^ get(e.l-1)

		res[e.i] = xa ^ xd
	}

	for _, e := range res {
		out.WriteString(strconv.Itoa(e))
		out.WriteByte('\n')
	}
}

var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isLine(c byte) bool {
	return (c == '\n') || (c == '\r')
}

func isSpace(c byte) bool {
	return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r')
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func skipSpace() {
	for (iPointer < nPointer) && isSpace(iBuffer[iPointer]) {
		iPointer++
	}
}

func skipNotDigit() {
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		iPointer++
	}
}

func read(arg interface{}) bool {
	switch v := arg.(type) {
	case *string:
		skipSpace()
		if iPointer >= nPointer {
			return false
		}
		var a []byte
		for (iPointer < nPointer) && !isSpace(iBuffer[iPointer]) {
			a = append(a, iBuffer[iPointer])
			iPointer++
		}
		*v = string(a)
	case *int:
		skipNotDigit()
		if iPointer >= nPointer {
			return false
		}
		var a int
		for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
			a = a*10 + int(iBuffer[iPointer]-'0')
			iPointer++
		}
		*v = int(a)
	case *int64:
		skipNotDigit()
		if iPointer >= nPointer {
			return false
		}
		var a int64
		for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
			a = a*10 + int64(iBuffer[iPointer]-'0')
			iPointer++
		}
		*v = int64(a)
	default:
		return false
	}
	return true
}

func readln(s *string) bool {
	if iPointer >= nPointer {
		return false
	}
	var a []byte
	for (iPointer < nPointer) && !isLine(iBuffer[iPointer]) {
		a = append(a, iBuffer[iPointer])
		iPointer++
	}
	*s = string(a)
	for (iPointer < nPointer) && isLine(iBuffer[iPointer]) {
		iPointer++
	}
	return true
}

func write(arg interface{}) {
	fmt.Println(arg)
}

package main

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

const maxn = 1000005
const maxm = 1000005

type query struct {
	i int
	l int
	r int
}

type queries []query

func (s queries) Len() int {
	return len(s)
}

func (s queries) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s queries) Less(i, j int) bool {
	return s[i].r < s[j].r
}

var n, m int
var a, res, xorall, xordist, prev []int
var last map[int]int
var q queries

func update(pos int, val int) {
	for x := pos; x <= n; x += x & -x {
		xordist[x] ^= val
	}
}

func get(pos int) int {
	val := 0
	for x := pos; x >= 1; x -= x & -x {
		val ^= xordist[x]
	}
	return val
}

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

	read(&n)

	a = make([]int, n+1)
	xorall = make([]int, n+1)
	xordist = make([]int, n+1)
	prev = make([]int, n+1)
	last = make(map[int]int)

	xorall[0] = 0
	xordist[0] = 0

	for i := 1; i <= n; i++ {
		read(&a[i])
		xorall[i] = xorall[i-1] ^ a[i]
		xordist[i] = 0

		prev[i] = last[a[i]]
		last[a[i]] = i
	}

	read(&m)

	q = make(queries, m)
	res = make([]int, m)

	for i := 0; i < m; i++ {
		q[i].i = i
		read(&q[i].l)
		read(&q[i].r)
	}

	sort.Sort(q)

	at := 1
	for _, e := range q {
		for at <= e.r {
			if prev[at] >= 1 {
				update(prev[at], a[at])
			}
			update(at, a[at])
			at++
		}
		xa := xorall[e.r] ^ xorall[e.l-1]
		xd := get(e.r) ^ get(e.l-1)

		res[e.i] = xa ^ xd
	}

	for _, e := range res {
		fmt.Fprintf(bufout, "%d\n", e)
	}
}

var iPointer int
var nPointer int
var iBuffer []byte

func init() {
	iPointer = 0
	iBuffer, _ = ioutil.ReadAll(os.Stdin)
	nPointer = len(iBuffer)
}

func isLine(c byte) bool {
	return (c == '\n') || (c == '\r')
}

func isSpace(c byte) bool {
	return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r')
}

func isDigit(c byte) bool {
	return (c >= '0') && (c <= '9')
}

func skipSpace() {
	for (iPointer < nPointer) && isSpace(iBuffer[iPointer]) {
		iPointer++
	}
}

func skipNotDigit() {
	for (iPointer < nPointer) && !isDigit(iBuffer[iPointer]) {
		iPointer++
	}
}

func read(arg interface{}) bool {
	switch v := arg.(type) {
	case *string:
		skipSpace()
		if iPointer >= nPointer {
			return false
		}
		var a []byte
		for (iPointer < nPointer) && !isSpace(iBuffer[iPointer]) {
			a = append(a, iBuffer[iPointer])
			iPointer++
		}
		*v = string(a)
	case *int:
		skipNotDigit()
		if iPointer >= nPointer {
			return false
		}
		var a int
		for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
			a = a*10 + int(iBuffer[iPointer]-'0')
			iPointer++
		}
		*v = int(a)
	case *int64:
		skipNotDigit()
		if iPointer >= nPointer {
			return false
		}
		var a int64
		for (iPointer < nPointer) && isDigit(iBuffer[iPointer]) {
			a = a*10 + int64(iBuffer[iPointer]-'0')
			iPointer++
		}
		*v = int64(a)
	default:
		return false
	}
	return true
}

func readln(s *string) bool {
	if iPointer >= nPointer {
		return false
	}
	var a []byte
	for (iPointer < nPointer) && !isLine(iBuffer[iPointer]) {
		a = append(a, iBuffer[iPointer])
		iPointer++
	}
	*s = string(a)
	for (iPointer < nPointer) && isLine(iBuffer[iPointer]) {
		iPointer++
	}
	return true
}

func write(arg interface{}) {
	fmt.Println(arg)
}

package main

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

var (
	inn = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 1000000)
	inn.Split(bufio.ScanWords)
	//prep
    n:=rd();
    a:=make([]int,n);
    for i:=range a{
        a[i]=rd();
    }
    ps:=make([]int,n+1);
    for i:=0;i<n;i++{
        ps[i+1]=ps[i]^a[i];
    }
    m:=rd();
    q:=make([][][2]int,n);
    ans:=make([]int,m);
    for i:=0;i<m;i++{
        x,y:=rd(),rd();
        q[x-1]=append(q[x-1],[2]int{y,i})
    }
    mp:=map[int]int{}
    t:=make([]int,n+1);
    update:=func(i,x int){
        for ;i<=n;i+=i&-i{
            t[i]^=x;
        }
    }
    query:=func(i int)(res int){
        for ;i>0;i-=i&-i{
            res^=t[i];
        }
        return;
    }
    for i:=n-1;i>=0;i--{
        if val,ok:=mp[a[i]];ok{
            update(val,a[i]);
        }
        mp[a[i]]=i+1;
        update(i+1,a[i])
        for _,it:=range q[i]{
            r,idx:=it[0],it[1]
            ans[idx]=ps[r]^ps[i]^query(r);
        }
    }
    for _,it:=range ans{
        Fprintln(out,it);
    }
}

//-------------------template------------------
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func maxll(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	x, _ = strconv.Atoi(string(inn.Bytes()))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	x, _ = strconv.ParseInt(string(inn.Bytes()), 10, 64)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}


It's marriage season in Ringland!Ringland has a form of a circle's boundary of length $$$L$$$. There are $$$n$$$ bridegrooms and $$$n$$$ brides, and bridegrooms decided to marry brides.Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.All objects in Ringland are located on the boundary of the circle, including the capital, bridegrooms' castles and brides' palaces. The castle of the $$$i$$$-th bridegroom is located at the distance $$$a_i$$$ from the capital in clockwise direction, and the palace of the $$$i$$$-th bride is located at the distance $$$b_i$$$ from the capital in clockwise direction.Let's define the inconvenience of a marriage the maximum distance that some bride should walk along the circle from her palace to her bridegroom's castle in the shortest direction (in clockwise or counter-clockwise direction).Help the bridegrooms of Ringland to choose brides in such a way that the inconvenience of the marriage is the smallest possible.

The first line contains two integers $$$n$$$ and $$$L$$$ ($$$1 \leq n \leq 2 \cdot 10^{5}$$$, $$$1 \leq L \leq 10^{9}$$$) — the number of bridegrooms and brides and the length of Ringland. The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i < L$$$) — the distances from the capital to the castles of bridegrooms in clockwise direction. The next line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \leq b_i < L$$$) — the distances from the capital to the palaces of brides in clockwise direction.

In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride.

NoteIn the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of $$$1$$$, and the first bride should also walk the same distance. Thus, the inconvenience is equal to $$$1$$$.In the second example let $$$p_i$$$ be the bride the $$$i$$$-th bridegroom will marry. One of optimal $$$p$$$ is the following: $$$(6,8,1,4,5,10,3,2,7,9)$$$.

package main

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

func main() {
	in := bufio.NewReaderSize(os.Stdin, 2<<16)
	var n, l int
	fmt.Fscan(in, &n, &l)

	a := make([]int, 4*n)
	for i := range a[:n] {
		fmt.Fscan(in, &a[n+i])
	}
	sort.Ints(a[n : 2*n])
	for i, v := range a[n : 2*n] {
		a[i] = v - l
		a[i+2*n] = v + l
		a[i+3*n] = v + 2*l
	}

	b := make([]int, n)
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	sort.Ints(b)

	fmt.Println(sort.Search(l/2+1, func(i int) bool {
		return possible(a, b, l, i)
	}))
}

func possible(a, b []int, l, inconvenience int) bool {
	fr := sort.SearchInts(a, b[0]-inconvenience)
	to := sort.SearchInts(a, b[0]+inconvenience+1) - 1
	if fr > to {
		return false
	}
	for i, b := range b {
		for fr <= to && a[fr+i] < b-inconvenience {
			fr++
		}
		for fr <= to && a[to+i] > b+inconvenience {
			to--
		}
		if fr > to {
			return false
		}
	}
	return true
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 256 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	if cur, _ := solve(a, b, l, ansPos); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 64 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	if cur, _ := solve(a, b, l, ansPos); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[i] - b[(i+shift)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func main() {
	in.Split(bufio.ScanWords)

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

	lo := 0
	hi := n
	for hi-lo > 16 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, m1) < max(a, b, l, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, lo)
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, lo); ans > cur {
			ans = cur
		}
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 16 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	if cur, _ := solve(a, b, l, ansPos); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 512 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	if cur, _ := solve(a, b, l, ansPos); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[i] - b[(i+shift)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func main() {
	in.Split(bufio.ScanWords)

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

	lo := 0
	hi := n
	for hi-lo > 8 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, m1) < max(a, b, l, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, lo)
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, lo); ans > cur {
			ans = cur
		}
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 128 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	if cur, _ := solve(a, b, l, ansPos); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 256 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	for i := 0; i < 4; i++ {
		cur, pos := solve(a, b, l, ansPos)
		if ans > cur {
			ans = cur
		}
		ansPos = pos
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) int {
	lo := 0
	hi := len(a)
	for hi-lo > 16 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
		}
	}
	return ans
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans := solve(a, b, l, 0)
	if cur := solve(a, b, l, n/2); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}

package main

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

var in = bufio.NewScanner(os.Stdin)

func readInt() int {
	if !in.Scan() {
		panic(in.Err())
	}
	res := 0
	for _, b := range in.Bytes() {
		res = res*10 + int(b-'0')
	}
	return res
}

func max(a, b []int, l, shift1, shift2 int) int {
	ma := 0
	n := len(a)
	for i := range a {
		dist := a[(i+shift1)%n] - b[(i+shift1+shift2)%n]
		if dist < 0 {
			dist *= -1
		}
		if dist*2 > l {
			dist = l - dist
		}
		if ma < dist {
			ma = dist
		}
	}
	return ma
}

func solve(a, b []int, l, shift1 int) (ma, pos int) {
	lo := 0
	hi := len(a)
	for hi-lo > 32 {
		m1 := (2*lo + hi) / 3
		m2 := (lo + 2*hi) / 3
		if max(a, b, l, shift1, m1) < max(a, b, l, shift1, m2) {
			hi = m2
		} else {
			lo = m1 + 1
		}
	}
	ans := max(a, b, l, shift1, lo)
	ansPos := lo
	for lo++; lo < hi; lo++ {
		if cur := max(a, b, l, shift1, lo); ans > cur {
			ans = cur
			ansPos = lo
		}
	}
	return ans, ansPos
}

func main() {
	in.Split(bufio.ScanWords)

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

	ans, ansPos := solve(a, b, l, 0)
	if cur, _ := solve(a, b, l, ansPos); ans > cur {
		ans = cur
	}
	fmt.Println(ans)
}