You are given an angle $$$\text{ang}$$$. The Jury asks You to find such regular $$$n$$$-gon (regular polygon with $$$n$$$ vertices) that it has three vertices $$$a$$$, $$$b$$$ and $$$c$$$ (they can be non-consecutive) with $$$\angle{abc} = \text{ang}$$$ or report that there is no such $$$n$$$-gon.  If there are several answers, print the minimal one. It is guarantied that if answer exists then it doesn't exceed $$$998244353$$$.

The first line contains single integer $$$T$$$ ($$$1 \le T \le 180$$$) — the number of queries.  Each of the next $$$T$$$ lines contains one integer $$$\text{ang}$$$ ($$$1 \le \text{ang} < 180$$$) — the angle measured in degrees.

For each query print single integer $$$n$$$ ($$$3 \le n \le 998244353$$$) — minimal possible number of vertices in the regular $$$n$$$-gon or $$$-1$$$ if there is no such $$$n$$$.

NoteThe answer for the first query is on the picture above.The answer for the second query is reached on a regular $$$18$$$-gon. For example, $$$\angle{v_2 v_1 v_6} = 50^{\circ}$$$.The example angle for the third query is $$$\angle{v_{11} v_{10} v_{12}} = 2^{\circ}$$$.In the fourth query, minimal possible $$$n$$$ is $$$180$$$ (not $$$90$$$).

// Package main provides
//
// File:  c.go
// Author: ymiyamoto
//
// Created on Sat Dec 29 00:46:07 2018
//
package main

import "fmt"

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

func main() {
	var N int
	fmt.Scan(&N)
	for i := 0; i < N; i++ {
		var x int
		fmt.Scan(&x)
		n := 180 / gcd(x, 180)
		if (n-2)*180/n < x {
			fmt.Println(n * 2)
		} else {
			fmt.Println(n)
		}
	}
}

package main

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

var stdin = bufio.NewScanner(os.Stdin)

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

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

func main() {
	n := readInt()
	for i := 0; i < n; i++ {
		cur := readInt()
		tmp := gcd(cur, 180)
		if cur == 180-tmp {
			fmt.Println(360 / tmp)
		} else {
			fmt.Println(180 / tmp)
		}
	}
}

package main

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

func gcd(a,b int)int  {
	if b== 0{
		return a
	}
	return gcd(b,a%b)
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		g := gcd(ang,180)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ang >= 178{
			fmt.Printf("%d\n",360/g)
		}else{

			var tt int = 180
		//	fmt.Println(g)
			if ang/g +1 == tt/g{
				tt*=2
			}
			for tt/g < 3 {
				tt += 180
			}
			fmt.Printf("%d\n", tt/g)
		}

	}

}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, d := 0, 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(in, "%d\n", &d)
		ans := solve(d)
		fmt.Fprintf(out, "%d\n", ans)
	}
	return
}

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

func solve(ang int) int {
	_ans := 180 / gcd(ang, 180)
	ans := _ans
	for ans*ang/180 > ans-2 {
		ans = ans + _ans
	}
	return ans
}

package main

import "fmt"

func main() {
	angleMap := map[int]int{}
	for i := 3; i <= 360; i++ {
		unit := float64(180) / float64(i)
		for j := 1; j <= (i - 2); j++ {
			angle := unit * float64(j)
			if angle-float64(int(angle)) > .0 {
				continue
			}
			if angleMap[int(angle)] != 0 {
				continue
			}
			angleMap[int(angle)] = i
		}
	}

	n := 0
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		angle := 0
		fmt.Scan(&angle)
		fmt.Println(angleMap[angle])
	}
}

package main

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

var stdin = bufio.NewReader(os.Stdin)

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func readInt() int {
	l, _ := stdin.ReadString('\n')
	i, _ := strconv.Atoi(l)
	return i
}

func main() {
	n := readInt()
	for i := 0; i < n; i++ {
		cur := readInt()
		tmp := gcd(cur, 180)
		if cur == 180-tmp {
			fmt.Println(360 / tmp)
		} else {
			fmt.Println(180 / tmp)
		}
	}
}

package main

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

var stdin = bufio.NewReader(os.Stdin)

func gcd(x, y int) int {
	for y != 0 {
		x, y = y, x%y
	}
	return x
}

func readInt() int {
	l, _ := stdin.ReadString('\n')
	i, _ := strconv.Atoi(l[:len(l)-1])
	return i
}

func main() {
	n := readInt()
	for i := 0; i < n; i++ {
		cur := readInt()
		tmp := gcd(cur, 180)
		if cur == 180-tmp {
			fmt.Println(360 / tmp)
		} else {
			fmt.Println(180 / tmp)
		}
	}
}

package main

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

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

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ans int = -1
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		for j := 1; j <= ang; j++ {
			if ang%j != 0 {
				continue
			}
			if 180%(ang/j) != 0 {
				continue
			}
			n := 180 * j / ang
			if j <= n-2 && n >= 3 {
				ans = n
				break;
			}
			if j > n-2{
				break
			}
		}
		fmt.Printf("%d\n", ans)
	}

}

package main

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

func gcd(a,b int)int  {
	if b== 0{
		return a
	}
	return gcd(b,a%b)
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		g := gcd(ang,180)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ang >= 178{
			fmt.Printf("%d\n",360/g)
		}else{
			var tt int = 180
			for tt/g < 3 {
				tt += 180
			}
			fmt.Printf("%d\n", tt/g)
		}

	}

}

package main

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

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

	fmt.Fscanf(buf, "%d\n", &t)
	for i:=0 ;i < t;i++{
		var ans int = -1
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		for j:=1;j <= ang;j++{
			if ang %j != 0{
				continue
			}
			if 180%(ang/j) != 0 {
				continue
			}
			n := 180*j/ang
			if j <= n-2{
				ans= n
				break;
			}
		}
		fmt.Printf("%d\n",ans)
	}

}

package main

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

func gcd(a,b int)int  {
	if b== 0{
		return a
	}
	return gcd(b,a%b)
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		g := gcd(ang,180)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ang >= 178{
			fmt.Printf("%d\n",360/g)
		}else{

			var tt int = 180
			if g +1 == tt/g{
				tt*=2
			}
			for tt/g < 3 {
				tt += 180
			}
			fmt.Printf("%d\n", tt/g)
		}

	}

}

package main

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

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

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ans int = -1
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		for j := 1; j <= ang; j++ {
			if ang%j != 0 {
				continue
			}
			if 180%(ang/j) != 0 {
				continue
			}
			n := 180 * j / ang
			if j <= n-2 && n >= 3 {
				ans = n
				break;
			}
		}
		fmt.Printf("%d\n", ans)
	}

}

package main

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

func gcd(a,b int)int  {
	if b== 0{
		return a
	}
	return gcd(b,a%b)
}
func main() {
	fmt.Println(gcd(1,8))
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ans int = -1
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		g := gcd(ang,180)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ans == 179{
			fmt.Printf("360\n",)
		}else{
			fmt.Printf("%d\n",180/g)
		}

	}

}

package main

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

func gcd(a,b int)int  {
	if b== 0{
		return a
	}
	return gcd(b,a%b)
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		g := gcd(ang,180)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ang >= 178{
			fmt.Printf("%d\n",180)
		}else{
			var tt int = 180
			for tt/g < 3 {
				tt += 180
			}
			fmt.Printf("%d\n", tt/g)
		}

	}

}

package main

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

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

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ans int = -1
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ans == 179{
			fmt.Printf("%d\n",180*179)
		}else{
			fmt.Printf("180\n")
		}

	}

}

package main

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

func gcd(a,b int)int  {
	if b== 0{
		return a
	}
	return gcd(b,a%b)
}
func main() {
	buf := bufio.NewReader(os.Stdin)
	var t int

	fmt.Fscanf(buf, "%d\n", &t)
	for i := 0; i < t; i++ {
		var ang int
		fmt.Fscanf(buf, "%d\n", &ang)
		g := gcd(ang,180)
		if ang == 180{
			fmt.Printf("-1\n")
		}else if ang >= 178{
			fmt.Printf("%d\n",360/g)
		}else{
			fmt.Printf("%d\n",180/g)
		}

	}

}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, d := 0, 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(in, "%d\n", &d)
		find := false
		for i := 3; i <= 180; i++ {
			small := float64(180) / float64(i)
			if small*float64(i-2)+1e-9 < float64(d) {
				continue
			}
			div := int(float64(d) / small)
			if math.Abs(small*float64(div)-float64(d)) < 1e-9 {
				fmt.Fprintf(out, "%d\n", i)
				find = true
				break
			}
		}
		if !find {
			fmt.Fprintf(out, "%d\n", -1)
		}
	}
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, d := 0, 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(in, "%d\n", &d)
		find := false
		for i := 3; i <= 180; i++ {
			small := float64(180) / float64(i)
			if small*float64(i-2)+1e-10 < float64(d) {
				continue
			}
			div := int(float64(d) / small)
			if math.Abs(small*float64(div)-float64(d)) < 1e-10 || math.Abs(small*float64(div+1)-float64(d)) < 1e-10 {
				fmt.Fprintf(out, "%d\n", i)
				find = true
				break
			}

		}
		if !find {
			fmt.Fprintf(out, "-1\n")
		}
	}
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, d := 0, 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(in, "%d\n", &d)
		find := false
		for i := 3; i <= 180; i++ {
			small := float64(180) / float64(i)
			if small*float64(i-2)+1e-9 < float64(d) {
				continue
			}
			div := int(float64(d) / small)
			if math.Abs(small*float64(div)-float64(d)) < 1e-9 {
				fmt.Fprintf(out, "%d\n", i)
				find = true
				break
			}

		}
		if !find {
			fmt.Fprintf(out, "−1\n")
		}
	}
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, d := 0, 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(in, "%d\n", &d)
		find := false
		for i := 3; i <= 180; i++ {
			small := float64(180) / float64(i)
			if small*float64(i-2)+1e-9 < float64(d) {
				continue
			}
			div := int(float64(d) / small)
			if math.Abs(small*float64(div)-float64(d)) < 1e-9 {
				fmt.Fprintf(out, "%d\n", i)
				find = true
				break
			}

		}
		if !find {
			fmt.Fprintf(out, "?1\n")
		}
	}
	return
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t, d := 0, 0
	fmt.Fscanf(in, "%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Fscanf(in, "%d\n", &d)
		for i := 3; i <= 180; i++ {
			small := float64(180) / float64(i)
			if small*float64(i-2)+1e-9 < float64(d) {
				continue
			}
			div := int(float64(d) / small)
			if math.Abs(small*float64(div)-float64(d)) < 1e-9 {
				fmt.Fprintf(out, "%d\n", i)
				break
			}
		}
	}
	return
}


After returning from the army Makes received a gift — an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i,  j,  k) (i < j < k), such that ai·aj·ak is minimum possible, are there in the array? Help him with it!

The first line of input contains a positive integer number n (3 ≤ n ≤ 105) — the number of elements in array a. The second line contains n positive integer numbers ai (1 ≤ ai ≤ 109) — the elements of a given array.

Print one number — the quantity of triples (i,  j,  k) such that i,  j and k are pairwise distinct and ai·aj·ak is minimum possible.

NoteIn the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.

package main

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

func main() {

	n := readN()
	a := readA(n)

	// sorted key set of m
	mref := make([]int, 0)
	m := make(map[int]int, n);

	for i := 0; i<n; i++ {
		v := a[i]
		_, ok := m[v];
		if ok {
			m[v] = m[v] + 1
		} else {
			m[v] = 1
		}
		mref = insert(mref, v)
	}

	//fmt.Println(m)
	//fmt.Println(mref)

	sum := 0
	pos := -1
	for i,v := range mref {
		sum += m[v]
		if sum >= 3 {
			pos = i
			break
		}
	}

	var ans int64

	if pos == 0 {
		ans = nck(m[mref[0]], 3)
	} else if pos == 1 {
		ans = nck(m[mref[1]], 3 - m[mref[0]])
	} else if pos == 2 {
		ans = int64(m[mref[2]])
	} else {
		panic("cannot reach here; pos = " + fmt.Sprintf("%v", pos))
	}

	fmt.Println(ans)

}

func insert(arr []int, v int) []int {
	if len(arr) == 3 && v > arr[2] {
		return arr
	}

	pos := -1
	for i,ele := range arr {
		if ele >= v {
			pos = i
			break
		}
	}
	var res []int
	if pos == -1 {
		// append at the end
		res = make([]int, len(arr) + 1)
		copy(res, arr)
		res[len(arr)] = v
	} else {
		if arr[pos] == v {
			return arr
		}
		res = make([]int, 0)
		res = append(res, arr[0:pos]...)
		res = append(res, v)
		if len(res) < 3 {
			//fmt.Println(res)
			//fmt.Println(arr)
			//fmt.Println(pos)
			res = append(res, arr[pos:]...)
		}
		if len(res) > 3 {
			tmp := make([]int, 3)
			copy(tmp, res)
			res = tmp
		}

	}

	return res
}

func nck(n,k int) int64 {
	if n-k < k {
		k = n-k
	}

	var k_fact int64
	k_fact = 1
	for i := int64(1); i<=int64(k); i++ {
		k_fact = k_fact * i
	}

	var n_minus_k_fact int64
	n_minus_k_fact = 1
	for i := int64(n-k+1); i<=int64(n); i++ {
		n_minus_k_fact *= i
	}
	return n_minus_k_fact / k_fact
}

func readN() int {
	var a int
	fmt.Scan(&a)
	//a = 100000

	return a
}

func readA(n int) []int {
	a := make([]int, n)

	reader := bufio.NewReader(os.Stdin)

	//fmt.Scan(a)

	for i := 0; i<n; i++ {
		fmt.Fscan(reader, &a[i])
		//a[i] = 1000000000
	}

	return a
}

//http://codeforces.com/problemset/problem/817/B
package main

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

func main() {

	reader := NewStdReader(os.Stdin)
	n := reader.ReadInt()

	if n == 3 {
		fmt.Print(1)
		return
	}

	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = reader.ReadInt()
	}
	sort.Ints(a)
	if a[3] != a[2] {
		fmt.Print(1)
		return
	}

	c := 1
	for i := 3; i < n; i++ {
		if a[i] == a[2] {
			c++
		} else {
			break
		}
	}
	if a[2] != a[1] {
		fmt.Print(c)
		return
	}

	if a[1] != a[0] {
		d := c + 1
		fmt.Print(Combination(d, 2))
		return
	}

	e := c + 2
	fmt.Print(Combination(e, 3))

}

// Combination computes n choose k
// equal to (n!)/((n-k)! k!)
// Panics on integer overflow
// Returns 0 if either n or k are negative
func Combination(n, k int) int64 {
	if n < 0 || k < 0 || k > n {
		return 0
	}
	if n == 0 && k == 0 {
		return 1
	}

	// Make k the greater of the two
	if k < n-k {
		k = n - k
	}
	// Compute (n!)/(k)!, checking for overflow
	prev := int64(1)
	mul := int64(1)
	for i := k + 1; i <= n; i++ {
		prev = mul
		mul *= int64(i)
		if mul < prev {
			panic("math: integer overflow")
		}
	}
	// Divide by (n-k)!
	for i := 2; i <= n-k; i++ {
		mul = mul / int64(i)
	}
	return mul
}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 20*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}

//http://codeforces.com/problemset/problem/817/B
package main

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

func main() {
	fmt.Print(Combination(1000, 3))

	reader := NewStdReader(os.Stdin)
	n := reader.ReadInt()

	if n == 3 {
		fmt.Print(1)
		return
	}

	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = reader.ReadInt()
	}
	sort.Ints(a)
	if a[3] != a[2] {
		fmt.Print(1)
		return
	}

	c := 1
	for i := 3; i < n; i++ {
		if a[i] == a[2] {
			c++
		} else {
			break
		}
	}
	if a[2] != a[1] {
		fmt.Print(c)
		return
	}

	if a[1] != a[0] {
		d := c + 1
		fmt.Print((d+1)*(d/2) + (d+1)/2*(d%2))
		return
	}

	e := c + 2
	fmt.Print(Combination(e, 3))

}

func Combination(n, k int) int {
	c := 0
	for i := 1; i <= n-2; i++ {
		for j := i + 1; j <= n-1; j++ {
			for k := j + 1; k <= n; k++ {
				c++
			}
		}
	}

	return c
}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 20*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}

package main

import (
	"fmt"
)

func main() {

	n := readN()
	a := readA(n)

	// sorted key set of m
	mref := make([]int, 0)
	m := make(map[int]int, n);

	for i := 0; i<n; i++ {
		v := a[i]
		_, ok := m[v];
		if ok {
			m[v] = m[v] + 1
		} else {
			m[v] = 1
		}
		mref = insert(mref, v)
	}

	//fmt.Println(m)
	//fmt.Println(mref)

	sum := 0
	pos := -1
	for i,v := range mref {
		sum += m[v]
		if sum >= 3 {
			pos = i
			break
		}
	}

	var ans int64

	if pos == 0 {
		ans = nck(m[mref[0]], 3)
	} else if pos == 1 {
		ans = nck(m[mref[1]], 3 - m[mref[0]])
	} else if pos == 2 {
		ans = int64(m[mref[2]])
	} else {
		panic("cannot reach here; pos = " + fmt.Sprintf("%v", pos))
	}

	fmt.Println(ans)

}

func insert(arr []int, v int) []int {
	pos := -1
	for i,ele := range arr {
		if ele >= v {
			pos = i
			break
		}
	}
	var res []int
	if pos == -1 {
		// append at the end
		res = make([]int, len(arr) + 1)
		copy(res, arr)
		res[len(arr)] = v
	} else {
		if arr[pos] == v {
			return arr
		}
		res = make([]int, 0)
		res = append(res, arr[0:pos]...)
		res = append(res, v)
		res = append(res, arr[pos:]...)
	}

	return res
}

func nck(n,k int) int64 {
	if n-k < k {
		k = n-k
	}

	var k_fact int64
	k_fact = 1
	for i := int64(1); i<=int64(k); i++ {
		k_fact = k_fact * i
	}

	var n_minus_k_fact int64
	n_minus_k_fact = 1
	for i := int64(n-k+1); i<=int64(n); i++ {
		n_minus_k_fact *= i
	}
	return n_minus_k_fact / k_fact
}

func readN() int {
	var a int
	fmt.Scan(&a)
	//a = 100000

	return a
}

func readA(n int) []int {
	a := make([]int, n)

	fmt.Scanln(a)

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

	return a
}

package main

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

func main() {

	n := readN()
	a := readA(n)

	// sorted key set of m
	mref := make([]int, 0)
	m := make(map[int]int, n);

	for i := 0; i<n; i++ {
		v := a[i]
		_, ok := m[v];
		if ok {
			m[v] = m[v] + 1
		} else {
			m[v] = 1
		}
		mref = insert(mref, v)
	}

	fmt.Println(m)
	fmt.Println(mref)

	sum := 0
	pos := -1
	for i,v := range mref {
		sum += m[v]
		if sum >= 3 {
			pos = i
			break
		}
	}

	var ans int64

	if pos == 0 {
		ans = nck(m[mref[0]], 3)
	} else if pos == 1 {
		ans = nck(m[mref[1]], 3 - m[mref[0]])
	} else if pos == 2 {
		ans = int64(m[mref[2]])
	} else {
		panic("cannot reach here; pos = " + fmt.Sprintf("%v", pos))
	}

	fmt.Println(ans)

}

func insert(arr []int, v int) []int {
	pos := -1
	for i,ele := range arr {
		if ele >= v {
			pos = i
			break
		}
	}
	var res []int
	if pos == -1 {
		// append at the end
		res = make([]int, len(arr) + 1)
		copy(res, arr)
		res[len(arr)] = v
	} else {
		if arr[pos] == v {
			return arr
		}
		res = make([]int, 0)
		res = append(res, arr[0:pos]...)
		res = append(res, v)
		res = append(res, arr[pos:]...)
	}

	return res
}

func nck(n,k int) int64 {
	if n-k < k {
		k = n-k
	}

	var k_fact int64
	k_fact = 1
	for i := int64(1); i<=int64(k); i++ {
		k_fact = k_fact * i
	}

	var n_minus_k_fact int64
	n_minus_k_fact = 1
	for i := int64(n-k+1); i<=int64(n); i++ {
		n_minus_k_fact *= i
	}
	return n_minus_k_fact / k_fact
}

func readN() int {
	var a int
	fmt.Scan(&a)
	//a = 100000

	return a
}

func readA(n int) []int {
	a := make([]int, n)

	reader := bufio.NewReader(os.Stdin)

	//fmt.Scan(a)

	for i := 0; i<n; i++ {
		fmt.Fscan(reader, &a[i])
		//a[i] = 1000000000
	}

	return a
}

package main

import (
	"fmt"
)

func main() {

	var n int

	fmt.Scan(&n)

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

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

	m := make(map[int]int, n);
	// sorted key set of m
	mref := make([]int, 0)

	for _,v := range a {
		_, ok := m[v];
		if ok {
			m[v] = m[v] + 1
		} else {
			m[v] = 1
		}
		mref = insert(mref, v)
	}

	//fmt.Println(m)
	//fmt.Println(mref)

	sum := 0
	pos := -1
	for i,v := range mref {
		sum += m[v]
		if sum >= 3 {
			pos = i
			break
		}
	}

	ans := -1

	if pos == 0 {
		ans = nck(m[mref[0]], 3)
	} else if pos == 1 {
		ans = nck(m[mref[1]], 3 - m[mref[0]])
	} else if pos == 2 {
		ans = m[mref[2]]
	} else {
		panic("cannot reach here; pos = " + fmt.Sprintf("%v", pos))
	}

	fmt.Println(ans)

}

func insert(arr []int, v int) []int {
	pos := -1
	for i,ele := range arr {
		if ele >= v {
			pos = i
			break
		}
	}
	var res []int
	if pos == -1 {
		// append at the end
		res = make([]int, len(arr) + 1)
		copy(res, arr)
		res[len(arr)] = v
	} else {
		if arr[pos] == v {
			return arr
		}
		res = make([]int, 0)
		res = append(res, arr[0:pos]...)
		res = append(res, v)
		res = append(res, arr[pos:]...)
	}

	return res
}

func nck(n,k int) int {
	k_fact := 1
	for i := 1; i<=k; i++ {
		k_fact *= i
	}
	n_minus_k_fact := 1
	for i := n-k+1; i<=n; i++ {
		n_minus_k_fact *= i
	}
	return n_minus_k_fact / k_fact
}

//http://codeforces.com/problemset/problem/817/B
package main

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

func main() {

	reader := NewStdReader(os.Stdin)
	n := reader.ReadInt()

	if n == 3 {
		fmt.Print(1)
		return
	}

	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = reader.ReadInt()
	}
	sort.Ints(a)
	if a[3] != a[2] {
		fmt.Print(1)
		return
	}

	c := 1
	for i := 3; i < n; i++ {
		if a[i] == a[2] {
			c++
		} else {
			break
		}
	}
	if a[2] != a[1] {
		fmt.Print(c)
		return
	}

	if a[1] != a[0] {
		d := c + 1
		fmt.Print((d+1)*(d/2) + (d+1)/2*(d%2))
		return
	}

	e := c + 2
	fmt.Print(Combination(e, 3))

}

// Combination computes n choose k
// equal to (n!)/((n-k)! k!)
// Panics on integer overflow
// Returns 0 if either n or k are negative
func Combination(n, k int) int64 {
	if n < 0 || k < 0 || k > n {
		return 0
	}
	if n == 0 && k == 0 {
		return 1
	}

	// Make k the greater of the two
	if k < n-k {
		k = n - k
	}
	// Compute (n!)/(k)!, checking for overflow
	prev := int64(1)
	mul := int64(1)
	for i := k + 1; i <= n; i++ {
		prev = mul
		mul *= int64(i)
		if mul < prev {
			panic("math: integer overflow")
		}
	}
	// Divide by (n-k)!
	for i := 2; i <= n-k; i++ {
		mul = mul / int64(i)
	}
	return mul
}

type StdReader struct {
	buf *bufio.Reader
}

func NewStdReader(reader io.Reader) *StdReader {
	return &StdReader{bufio.NewReaderSize(reader, 20*1024*1024)}
}

func (std *StdReader) ReadInt() int {

	r := 0
	sign := 1
	ok := false

	for {
		b, e := std.buf.ReadByte()

		if e != nil {
			break
		}
		if b >= '0' && b <= '9' {
			r = r*10 + int(b-'0')
			ok = true
		} else {
			if ok {
				break
			} else {
				if b == '-' {
					sign = -sign
				}
			}
		}
	}

	return sign * r
}


Ghosts live in harmony and peace, they travel the space without any purpose other than scare whoever stands in their way.There are $$$n$$$ ghosts in the universe, they move in the $$$OXY$$$ plane, each one of them has its own velocity that does not change in time: $$$\overrightarrow{V} = V_{x}\overrightarrow{i} + V_{y}\overrightarrow{j}$$$ where $$$V_{x}$$$ is its speed on the $$$x$$$-axis and $$$V_{y}$$$ is on the $$$y$$$-axis.A ghost $$$i$$$ has experience value $$$EX_i$$$, which represent how many ghosts tried to scare him in his past. Two ghosts scare each other if they were in the same cartesian point at a moment of time.As the ghosts move with constant speed, after some moment of time there will be no further scaring (what a relief!) and the experience of ghost kind $$$GX = \sum_{i=1}^{n} EX_i$$$ will never increase.Tameem is a red giant, he took a picture of the cartesian plane at a certain moment of time $$$T$$$, and magically all the ghosts were aligned on a line of the form $$$y = a \cdot x + b$$$. You have to compute what will be the experience index of the ghost kind $$$GX$$$ in the indefinite future, this is your task for today.Note that when Tameem took the picture, $$$GX$$$ may already be greater than $$$0$$$, because many ghosts may have scared one another at any moment between $$$[-\infty, T]$$$.

The first line contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 \leq n \leq 200000$$$, $$$1 \leq |a| \leq 10^9$$$, $$$0 \le |b| \le 10^9$$$) — the number of ghosts in the universe and the parameters of the straight line. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$V_{xi}$$$, $$$V_{yi}$$$ ($$$-10^9 \leq x_i \leq 10^9$$$, $$$-10^9 \leq V_{x i}, V_{y i} \leq 10^9$$$), where $$$x_i$$$ is the current $$$x$$$-coordinate of the $$$i$$$-th ghost (and $$$y_i = a \cdot x_i + b$$$). It is guaranteed that no two ghosts share the same initial position, in other words, it is guaranteed that for all $$$(i,j)$$$ $$$x_i \neq x_j$$$ for $$$i \ne j$$$.

Output one line: experience index of the ghost kind $$$GX$$$ in the indefinite future.

NoteThere are four collisions $$$(1,2,T-0.5)$$$, $$$(1,3,T-1)$$$, $$$(2,4,T+1)$$$, $$$(3,4,T+0.5)$$$, where $$$(u,v,t)$$$ means a collision happened between ghosts $$$u$$$ and $$$v$$$ at moment $$$t$$$. At each collision, each ghost gained one experience point, this means that $$$GX = 4 \cdot 2 = 8$$$.In the second test, all points will collide when $$$t = T + 1$$$.   The red arrow represents the 1-st ghost velocity, orange represents the 2-nd ghost velocity, and blue represents the 3-rd ghost velocity.

package main

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

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

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

func main() {
	defer writer.Flush()

	var n, a, b int64
	scanf("%d %d %d\n", &n, &a, &b)
	x, vx, vy := make([]int64, n), make([]int64, n), make([]int64, n)
	for i := range x {
		scanf("%d %d %d\n", &x[i], &vx[i], &vy[i])
	}
	count, m, l := int64(0), make(map[int64]map[int64]int64), make(map[int64]int64)
	for i := range x {
		k, ok := m[a*vx[i]-vy[i]]
		if !ok {
			m[a*vx[i]-vy[i]] = make(map[int64]int64)
			m[a*vx[i]-vy[i]][vx[i]], l[a*vx[i]-vy[i]] = 1, 1
		} else {
			count += 2 * (l[a*vx[i]-vy[i]] - k[vx[i]])
			m[a*vx[i]-vy[i]][vx[i]]++
			l[a*vx[i]-vy[i]]++
		}
	}
	printf("%d\n", count)
}


Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

The first line contains integer n (1 ≤ n ≤ 50) — the number of strings. This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

NoteIn the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".

package main

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

func I(i ...interface{}) error {
	_, err := fmt.Fscan(r, i...)
	return err
}

func O(o ...interface{}) {
	fmt.Fprint(w, o...)
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	if f, err := os.Open("in.goC"); err == nil {
		r = bufio.NewReader(f)
	} else {
		r = bufio.NewReader(os.Stdin)
	}
	w = bufio.NewWriter(os.Stdout)
}

func main() {
	defer w.Flush()
	var n int
	for I(&n) == nil {
		solve(n)
	}
}

func solve(n int) {
	ss := make([]string, n)
	for i := range ss {
		I(&ss[i])
	}
	var res int64 = 1e13
	for o := 0; o < n; o++ {
		var ans int64
		for i := 0; i < n; i++ {
			for j := 0; j < len(ss[0]); j++ {
				if shift(ss[i], j) == ss[o] {
					ans += int64(j)
					break
				} else if j == len(ss[0])-1 {
					O("-1\n")
					return
				}
			}
		}
		if res > ans {
			res = ans
		}
	}
	O(res, "\n")
}

func shift(s string, num int) string {
	return s[num:] + s[:num]
}

package main

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

func solve() {
	n := readInt()
	a := make([][]byte, n)
	for i := 0; i < n; i++ {
		a[i] = []byte(readString())
	}
	ans := 1000000
	for i := 0; i < len(a[0]); i++ {
		b := rot(a[0], i)
		total := i
		cnt := 1
	outer:
		for j := 1; j < n; j++ {
			for k := 0; k < len(a[0]); k++ {
				c := rot(a[j], k)
				ok := true
				for l := 0; l < len(c); l++ {
					if c[l] != b[l] {
						ok = false
						break
					}
				}
				if ok {
					cnt++
					total += k
					continue outer
				}
			}
		}
		if cnt == n {
			ans = min(total, ans)
		} else {
			fmt.Println(-1)
			return
		}
	}
	fmt.Println(ans)
}

func rot(buf []byte, i int) []byte {
	b := make([]byte, 0)
	b = append(b, buf[i:]...)
	b = append(b, buf[:i]...)
	return b
}

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

// 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"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	a := make([][]byte, n)
	for i := 0; i < n; i++ {
		a[i] = []byte(readString())
	}
	ans := 1000000
outer:
	for i := 0; i < len(a[0]); i++ {
		b := rot(a[0], i)
		total := i
	inner:
		for j := 1; j < n; j++ {
			matched := false
		shift:
			for k := 0; k < len(a[0]); k++ {
				c := rot(a[j], k)
				for l := 0; l < len(c); l++ {
					if c[l] != b[l] {
						continue shift
					}
				}
				total += k
				matched = true
				continue inner
			}
			if !matched {
				continue outer
			}
		}
		ans = min(total, ans)
	}
	if ans == 1000000 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

func rot(buf []byte, i int) []byte {
	b := make([]byte, 0)
	b = append(b, buf[i:]...)
	b = append(b, buf[:i]...)
	return b
}

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

// 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"
	"os"
	"strconv"
)

func solve() {
	n := readInt()
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = readString()
	}
	ans := 1000000
outer:
	for i := 0; i < len(a[0]); i++ {
		b := a[0][i:] + a[0][:i]
		total := i
	inner:
		for j := 1; j < n; j++ {
			matched := false
			for k := 0; k < len(a[0]); k++ {
				c := a[j][k:] + a[j][:k]
				if c == b {
					total += k
					matched = true
					continue inner
				}
			}
			if !matched {
				continue outer
			}
		}
		ans = min(total, ans)
	}
	if ans == 1000000 {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

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

// 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 (
	"fmt"
	"os"
	"sort"
	"strings"
)

func sortString(w string) string {
	s := strings.Split(w, "")
	sort.Strings(s)
	return strings.Join(s, "")
}

func main() {
	var arrS []string
	var s, tmp string
	var t, count, min int
	min = 1000000000
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%s\n", &s)
		arrS = append(arrS, s)
	}
	tmp = sortString(arrS[0])
	for i := 1; i < t; i++ {
		if tmp != sortString(arrS[i]) {
			fmt.Println("-1")
			os.Exit(0)
		}
	}
	for i := 0; i < t; i++ {
		for j := 0; j < t; j++ {
			if j == i {
				continue
			}
			tmp = arrS[j]
			for tmp != arrS[i] {
				tmp = tmp[1:] + tmp[:1]
				count++
				if count > 2501 {
					fmt.Println("-1")
					os.Exit(0)
				}
			}
		}
		if min > count {
			min = count
		}
		count = 0
	}
	fmt.Println(min)
}

package main

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

func Solve(io *FastIO) {
	N := io.NextInt()

	S := make([]string, N)
	T := make([]string, N)
	for i := 0; i < N; i++ {
		S[i] = io.NextLine()
		T[i] = S[i] + S[i]
	}
	M := len(S[0])

	best := math.MaxInt32
	for i := 0; i < M; i++ {
		k := i + M
		find := T[0][i:k]
		moves := 0
		for j := 0; j < N; j++ {
			idx := strings.Index(T[j], find)
			if idx < 0 {
				moves = math.MaxInt32
				break
			}
			moves += idx
		}
		best = Min(best, moves)
	}

	if best < math.MaxInt32 {
		io.Printf("%d\n", best)
	} else {
		io.Printf("-1\n");
	}
}

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

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

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

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Solve(&io)
	io.FlushOutput()
}

package main

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

func solve() {
	n := readInt()
	a := make([][]byte, n)
	for i := 0; i < n; i++ {
		a[i] = []byte(readString())
	}
	ans := 100000
	for i := 0; i < n; i++ {
		total := 0
		for j := 0; j < n; j++ {
			c := cost(a[i], a[j])
			if c == -1 {
				fmt.Println(-1)
				return
			}
			total += c
		}
		ans = min(total, ans)
	}
	fmt.Println(ans)
}

func cost(a, b []byte) int {
	c := make([]byte, len(b))
	copy(c, b)
	res := 0
	// fmt.Println(string(a), string(c))
	for i := 0; i < len(a); i++ {
		// fmt.Println(i, string(a), string(c))
		if a[i] == c[0] {
			c = c[1:]
		} else {
			ok := false
			// fmt.Println(string(c))
			for j := 0; j < len(c); j++ {
				if a[i] != c[0] {
					v := c[0]
					c = c[1:]
					c = append(c, v)
					res++
				} else {
					c = c[1:]
					ok = true
					break
				}
			}
			if !ok {
				return -1
			}
		}
	}
	return res
}

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

// 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"
	"os"
	"strconv"
)

func solve() {
	a := []byte(readString())
	cnt := 0
	for ii := 0; ii < len(a)/2; ii++ {
		if a[ii] != a[len(a)-ii-1] {
			cnt++
		}
	}
	if len(a)%2 == 1 && cnt <= 1 {
		fmt.Println("YES")
		return
	}
	if cnt != 1 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

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, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, 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 (
	"fmt"
	"os"
	"sort"
	"strings"
)

func sortString(w string) string {
	s := strings.Split(w, "")
	sort.Strings(s)
	return strings.Join(s, "")
}

func main() {
	var arrS []string
	var s, tmp string
	var t, count, min int
	min = 1000000000
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		fmt.Scanf("%s\n", &s)
		arrS = append(arrS, s)
	}
	tmp = sortString(arrS[0])
	for i := 1; i < t; i++ {
		if tmp != sortString(arrS[i]) {
			fmt.Println("-1")
			os.Exit(0)
		}
	}
	for i := 0; i < t; i++ {
		for j := 0; j < t; j++ {
			if j == i {
				continue
			}
			tmp = arrS[j]
			for tmp != arrS[i] {
				tmp = tmp[1:] + tmp[:1]
				count++
				if count > 50 {
					fmt.Println("-1")
					os.Exit(0)
				}
			}
		}
		if min > count {
			min = count
		}
		count = 0
	}
	fmt.Println(min)
}


You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes.

The first line contains two integers n and k (1 ≤ k ≤ n ≤  105) — the size of the array a and the number of subsegments you have to split the array to. The second line contains n integers a1,  a2,  ...,  an ( - 109  ≤  ai ≤  109).

Print single integer — the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.

NoteA subsegment [l,  r] (l ≤ r) of array a is the sequence al,  al + 1,  ...,  ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4,  - 5,  - 3,  - 2,  - 1). The only minimum is min( - 4,  - 5,  - 3,  - 2,  - 1) =  - 5. The resulting maximum is  - 5.

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

const debug = true

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

	in1 := gsli(reader)
	k := in1[1]

	in := gsli(reader)

	min := in[0]
	max := in[0]

	for _, e := range in {
		if e > max {
			max = e
		}
		if e < min {
			min = e
		}
	}

	if k == 1 {
		fmt.Println(min)
	} else if k == 2 {
		if in[0] >= in[len(in)-1] {
			fmt.Println(in[0])
		} else {
			fmt.Println(in[len(in)-1])
		}
	} else {
		fmt.Println(max)
	}
}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

func maxInt(x, y int) int {
	if x > y {
		return x
	}
	return y
}

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}

package main

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

var stdin *bufio.Reader
var stdout *bufio.Writer

func init() {
	stdin = bufio.NewReader(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func printf(format string, arg ...interface{}) {
	fmt.Fprintf(stdout, format, arg...)
}

func scan(arg ...interface{}) (int, error) {
	return fmt.Fscan(stdin, arg...)
}

func main() {
	for true {
		var n, k int
		if _, err := scan(&n, &k); err != nil {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			scan(&a[i])
		}
		if k == 1 {
			min := a[0]
			for i := 0; i < n; i++ {
				if a[i] < min {
					min = a[i]
				}
			}
			printf("%d\n", min)
		} else if k == 2 {
			max := a[0]
			if a[n-1] > max {
				max = a[n-1]
			}
			printf("%d\n", max)
		} else {
			max := a[0]
			for i := 0; i < n; i++ {
				if max < a[i] {
					max = a[i]
				}
			}
			printf("%d\n", max)
		}
	}
	stdout.Flush()
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\r\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], err = strconv.Atoi(parts[i])
		if err != nil {
			fmt.Println(parts[i], err.Error())
		}
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k >= 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

// Codeforces helper functions
// 20 Jan 18
// samadhi

package main

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

const debug = true

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

	in1 := gsli(reader)
	k := in1[1]

	in := gsli(reader)

	min := in[0]
	max := in[0]

	for _, e := range in {
		if e > max {
			max = e
		}
		if e < min {
			min = e
		}
	}

	if k == 1 {
		fmt.Println(min)
	} else {
		fmt.Println(max)
	}
}

// Input helpers

// If you don't pass in a reference to the reader, and just instantiate a new
// one every time, it will work locally, with IO typed at human-speed, but not
// on Codeforces.

// Get Space-separated Line as Strings
//
// Example input:
// 1 test 382.0
// Example output:
// ["1", "test", "382.0"]

func gsls(reader *bufio.Reader) (arr []string) {
	// Get a full line
	input, _ := reader.ReadString('\n')

	//Strip newline and return chars, if any
	input = strings.TrimSuffix(input, "\n")
	input = strings.TrimSuffix(input, "\r")

	// Split at the spaces into an array
	arr = strings.Split(input, " ")

	return
}

// Get Space-separated Line as Integers
// Any non-valid integers will be 0
//
// Example input:
// test 1 12.3 -117
// Example output:
// [0, 1, 0, -117]

func gsli(reader *bufio.Reader) (arr []int) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.Atoi(e)
		arr = append(arr, cur)
	}

	return
}

// Get Space-separated Line as 64-bit Floats
// Any non-valid floats will be 0
//
// Example input:
// test 128.328 12 -11.0
// Example output:
// [0, 128.328, 12, -11]

func gslf(reader *bufio.Reader) (arr []float64) {
	stringArr := gsls(reader)

	for _, e := range stringArr {
		cur, _ := strconv.ParseFloat(e, 64)
		arr = append(arr, cur)
	}

	return
}

// String helpers

// Is Lowercase
// Given some string[i], return whether it is lower case or not.

func isl(b byte) bool {
	var r rune
	r = rune(b)
	return unicode.IsLower(r)
}

// Math helpers

// Positve Modulo
// Returns x mod y, but always positive

func posMod(x, y int) int {
	return (x%y + y) % y
}

// Max Int
// Returns the max of two ints

func maxInt(x, y int) int {
	if x > y {
		return x
	}
	return y
}

// Distance formule
// Calculates the distance to the origin

func dist(x, y float64) float64 {
	return math.Sqrt(math.Pow(x, 2) + math.Pow(y, 2))
}

// Output helpers

func yes() {
	fmt.Println("Yes")
}

func no() {
	fmt.Println("No")
}

// Debug helpers

// From https://groups.google.com/forum/#!topic/golang-nuts/Qlxs3V77nss

func dbg(s string, a ...interface{}) {
	if debug {
		fmt.Printf(s, a...)
	}
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	line := scanner.Text()
	parts := strings.Split(line, " ")
	n, _ := strconv.ParseInt(parts[0], 10, 64)
	k, _ := strconv.ParseInt(parts[1], 10, 64)
	scanner.Scan()
	line = scanner.Text()
	parts = strings.Split(line, " ")
	arr := make([]int, int(n))
	for i := 0; i < int(n); i++ {
		a, _ := strconv.ParseInt(parts[i], 10, 64)
		arr[i] = int(a)
	}
	fmt.Println(solve(int(n), int(k), arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, _ := ioutil.ReadAll(os.Stdin)
	lines := strings.Split(string(data), "\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\r\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], err = strconv.Atoi(parts[i])
		if err != nil {
			fmt.Println(parts[i], err.Error())
		}
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(n, k, arr)
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		fmt.Println(err.Error())
	}
	lines := strings.Split(string(data), "\n")
	parts := strings.Split(lines[0], " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	parts = strings.Split(lines[1], " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], err = strconv.Atoi(parts[i])
		if err != nil {
			fmt.Println(parts[i], err.Error())
		}
	}
	fmt.Println(n, k, arr)
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	line := scanner.Text()
	parts := strings.Split(line, " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	scanner.Scan()
	line = scanner.Text()
	parts = strings.Split(line, " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}

package main

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

func main() {
	scanner := bufio.NewReader(os.Stdin)
	line, _ := scanner.ReadString('\n')
	parts := strings.Split(strings.Trim(line, "\n"), " ")
	n, _ := strconv.Atoi(parts[0])
	k, _ := strconv.Atoi(parts[1])
	line, _ = scanner.ReadString('\n')
	parts = strings.Split(strings.Trim(line, "\n"), " ")
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		arr[i], _ = strconv.Atoi(parts[i])
	}
	fmt.Println(solve(n, k, arr))
}

func solve(n, k int, arr []int) int {
	st := NewSparseTable(arr, func(d ...int) int {
		res := d[0]
		for _, v := range d {
			if v < res {
				res = v
			}
		}
		return res
	})
	if k == 1 {
		return st.QueryRangeFast(0, len(arr)-1)
	}
	if k > 3 {
		res := arr[0]
		for i := 1; i < len(arr); i++ {
			if arr[i] > res {
				res = arr[i]
			}
		}
		return res
	}
	m := st.QueryRangeFast(0, 0)
	for i := 0; i < len(arr)-1; i++ {
		m = max(m, max(st.QueryRangeFast(0, i), st.QueryRangeFast(i+1, len(arr)-1)))
	}
	return m
}

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

// SparseTable is fast in multiple range maximum/minimum queryies in immutable array.
//
// Ref: https://cp-algorithms.com/data_structures/sparse-table.html .
type SparseTable struct {
	t  [][]int
	f  func(d ...int) int
	n  int
	lg []int
}

// NewSparseTable creates a sparse table with the range rule l. Time complexity: O(nlgn).
//
// NOTICE: Parameter f must be an [idempotent function](https://en.wikipedia.org/wiki/Idempotence) if you want to do range queries in O(1). If not, wrong query results will be returned.
func NewSparseTable(data []int, f func(d ...int) int) *SparseTable {
	n := len(data)
	t := make([][]int, n+1)
	for i := 0; i < n; i++ {
		t[i] = append(t[i], data[i])
	}
	for i := 1; 1<<i <= n && 1<<i > 0; i++ {
		for j := 0; j+(1<<i) <= n && j+(1<<i) > 0; j++ {
			t[j] = append(t[j], f(t[j][i-1], t[j+(1<<(i-1))][i-1]))
		}
	}
	lg := make([]int, n+1)
	lg[1] = 0
	for i := 2; i <= n; i++ {
		lg[i] = lg[i/2] + 1
	}
	return &SparseTable{
		t:  t,
		f:  f,
		n:  n,
		lg: lg,
	}
}

// QueryRangeFast queries the range [left, right] with function f.
//
// NOTICE: Only available if f is an [idempotent function](https://en.wikipedia.org/wiki/Idempotence).
//
// Time complexity: O(1).
func (s *SparseTable) QueryRangeFast(left, right int) int {
	l := right - left + 1
	return s.f(s.t[left][s.lg[l]], s.t[right-(1<<s.lg[l])+1][s.lg[l]])
}

// QueryRange queries the range [left, right] with function f.
//
// Time complexity: O(lgn).
func (s *SparseTable) QueryRange(left, right int) int {
	l := right - left + 1
	res := s.t[left][s.lg[l]]
	for left += 1 << s.lg[l]; left <= right; left += 1 << s.lg[l] {
		l = right - left + 1
		res = s.f(res, s.t[left][s.lg[l]])
	}
	return res
}