As you have noticed, there are lovely girls in Arpa’s land.

People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crush_{i}. [Image] 

Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.

The game consists of rounds. Assume person x wants to start a round, he calls crush_{x} and says: "Oww...wwf" (the letter w is repeated t times) and cuts off the phone immediately. If t > 1 then crush_{x} calls crush_{crush}_{x} and says: "Oww...wwf" (the letter w is repeated t - 1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (t = 1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.

Mehrdad has an evil plan to make the game more funny, he wants to find smallest t (t ≥ 1) such that for each person x, if x starts some round and y becomes the Joon-Joon of the round, then by starting from y, x would become the Joon-Joon of the round. Find such t for Mehrdad if it's possible.

Some strange fact in Arpa's land is that someone can be himself's crush (i.e. crush_{i} = i).


-----Input-----

The first line of input contains integer n (1 ≤ n ≤ 100) — the number of people in Arpa's land.

The second line contains n integers, i-th of them is crush_{i} (1 ≤ crush_{i} ≤ n) — the number of i-th person's crush.


-----Output-----

If there is no t satisfying the condition, print -1. Otherwise print such smallest t.


-----Examples-----
Input
4
2 3 1 4

Output
3

Input
4
4 4 4 4

Output
-1

Input
4
2 1 4 3

Output
1



-----Note-----

In the first sample suppose t = 3. 

If the first person starts some round:

The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if x is 1.

The process is similar for the second and the third person.

If the fourth person starts some round:

The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when x is 4.

In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.

Difficulty: MEDIUM_HARD

Solutions:
import sys
input = sys.stdin.readline
n = int(input())
l = list(map(lambda x: int(x) - 1, input().split()))
use = []
valid = 1
for i in range(n):
	t = i
	for j in range(n + 5):
		t = l[t]
		if t == i:
			if (j + 1) % 2 == 0:
				use.append((j + 1) // 2)
			else:
				use.append(j + 1)
			break
	else:
		valid = 0
if not valid:
	print('-1')
else:
	ans = 1
	for i in use:
		t = ans
		while ans % i:
			ans += t
	print(ans)


def lcm(a, b):
	sum = a + b
	large = a if a > b else b
	small = sum - large
	for i in range(1, small + 1):
		if large * i % small == 0:
			return large * i
	return None
n = int(input())
crush = list(map(int, input().split(' ')))
visited = [False] * n
graph_len = []
if sum(crush) != n * (n + 1) / 2:
	print('-1')
else:
	for i in range(n):
		if visited[i]:
			continue
		idx = i
		len1 = 0
		while not visited[idx]:
			visited[idx] = True
			idx = crush[idx] - 1
			len1 += 1
		len1 = int(len1 / 2) if len1 % 2 == 0 else len1
		if len1 not in graph_len:
			graph_len.append(len1)
	n1 = graph_len[0]
	for i in range(1, len(graph_len)):
		n2 = graph_len[i]
		n1 = lcm(n1, n2)
	print(n1)


from math import gcd

def main():
	bb = [-1] * int(input())
	aa = [int(s) - 1 for s in input().split()]
	for (b, a) in enumerate(aa):
		if bb[a] != -1:
			print(-1)
			return
		bb[a] = b
	cc = set()
	for (a, b) in enumerate(bb):
		if b != -1:
			c = 0
			while bb[a] != -1:
				bb[a] = -1
				c += 1
				a = aa[a]
		cc.add(c)
	r = 1
	for c in cc:
		c //= 2 - c % 2
		r = r * c // gcd(r, c)
	print(r)

def __starting_point():
	main()
__starting_point()


import math

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def solve():
	n = int(input())
	ar = list(map(int, input().split()))
	ar = [0] + ar
	mark = [0] * (n + 1)
	ans = list()
	for i in range(1, n + 1):
		if mark[i] == 0:
			start = i
			ver = i
			count = 0
			while mark[ver] == 0:
				mark[ver] = 1
				ver = ar[ver]
				count = count + 1
			if ver == start:
				if count % 2 == 0:
					ans = ans + [count // 2]
				else:
					ans = ans + [count]
			else:
				print(-1)
				return
	f = 1
	for x in ans:
		f = f * x // gcd(f, x)
	print(f)
solve()


gcd = lambda a, b: a if b == 0 else gcd(b, a % b)
n = int(input())
a = [0] + list(map(int, input().split()))
t = 1
for i in range(1, n + 1):
	if i == a[i]:
		continue
	x = i
	cnt = 1
	while a[x] != i and cnt <= n:
		x = a[x]
		cnt += 1
	if cnt == n + 1:
		print(-1)
		return
	if cnt % 2 == 0:
		cnt //= 2
	t = t * cnt // gcd(t, cnt)
print(t)


from math import gcd

def count(x, a, n):
	c = 0
	t = x
	while 1:
		x = a[x - 1]
		c += 1
		if t == x:
			break
		if c >= n:
			return 0
			break
	if c % 2 == 0:
		return c // 2
	else:
		return c

def xyz():
	n = int(input())
	a = [int(i) for i in input().split()]
	l = 1
	for i in range(n):
		p = count(i + 1, a, n)
		if p == 0:
			return -1
		l = p * l // gcd(p, l)
	return l
print(xyz())


import math
n = int(input())
a = list(map(int, input().split()))
if len(set(a)) != len(a):
	print(-1)
else:
	(ans, cycle) = (None, None)
	visited = [False] * (n + 1)
	for i in range(n):
		if visited[i + 1]:
			continue
		(count, v) = (0, [])
		(index, value) = (i + 1, -1)
		while value != i + 1:
			visited[index] = True
			v.append(index)
			value = a[index - 1]
			index = value
		cycle = [len(v) // 2, len(v)][len(v) % 2]
		if i == 0:
			ans = cycle
		else:
			ans = ans * cycle // math.gcd(ans, cycle)
	print(ans)


from __future__ import print_function
from collections import *
from sys import stdin
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x: reversed(range(x))
mod = int(1000000000.0) + 7
inf = float('inf')
(n,) = gil()
nxt = [0] + gil()
vis = [0] * (n + 1)
c = set()

def cycle(p):
	tt = 0
	start = p
	while vis[p] == 0:
		tt += 1
		vis[p] = 1
		p = nxt[p]
	if p != start:
		print(-1)
		exit()
	return tt
for p in range(1, n + 1):
	if vis[p]:
		continue
	tt = cycle(p)
	if tt & 1:
		c.add(tt)
	else:
		c.add(tt // 2)
if 1 in c:
	c.remove(1)
c = list(c)
if len(c) <= 1:
	print(c[0] if c else 1)
	exit()
pr = {}

def getPrime(x):
	ppr = {}
	for i in range(2, int(sqrt(x)) + 1):
		po = 0
		while x % i == 0:
			x //= i
			po += 1
		if po:
			ppr[i] = po
	if x > 1:
		ppr[x] = 1
	return ppr
for v in c:
	for (pi, po) in getPrime(v).items():
		pr[pi] = max(pr.get(pi, 0), po)
ans = 1
for (pi, po) in pr.items():
	ans *= pi ** po
print(ans)


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())
A = [0] + list(map(int, input().split()))
g = {}
for i in range(1, n + 1):
	g[A[i]] = i
if len(g) != n:
	print(-1)
else:
	clc = 1
	for i in g:
		x = g[i]
		cnt = 1
		while x != i:
			x = g[x]
			cnt += 1
		if cnt % 2 == 0:
			cnt //= 2
		clc = lcm(clc, cnt)
	print(clc)


from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
inf = float('inf')
from collections import defaultdict as dd
from collections import Counter, deque
from heapq import *
import math
from math import floor, ceil, sqrt

def geti():
	return map(int, input().strip().split())

def getl():
	return list(map(int, input().strip().split()))

def getis():
	return map(str, input().strip().split())

def getls():
	return list(map(str, input().strip().split()))

def gets():
	return input().strip()

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def solve():
	n = geta()
	a = getl()
	edges = dd(list)
	for i in range(n):
		edges[i].append(a[i] - 1)
	dist = [[inf] * n for _ in range(n)]

	def dfs(root, par, temp=0):
		vis[root] = True
		for node in edges[root]:
			dist[par][node] = temp + 1
			if node not in vis:
				dfs(node, par, temp + 1)
	ans = []
	vis = dd(int)
	ok = dd(int)

	def dfs(root, par, dist=0):
		if root == par and dist:
			ok[par] = True
			if dist & 1:
				ans.append(dist)
			else:
				ans.append(dist // 2)
			return
		if root in vis:
			return
		vis[root] = True
		for node in edges[root]:
			dfs(node, par, dist + 1)
	need = []
	for i in range(n):
		if i not in vis:
			need.append(i)
			dfs(i, i)
	for i in need:
		if i not in ok:
			print(-1)
			break
	else:
		ans = Counter(ans)
		ans = sorted(ans)
		vis = [False] * (len(ans) + 1)
		ok = []
		for i in range(len(ans)):
			if ans[i] == 1 or vis[i]:
				continue
			temp = []
			for j in range(i, len(ans)):
				if ans[j] % ans[i] == 0:
					temp.append(ans[j])
					vis[j] = True
			now = temp[0]
			g = temp[0]
			for k in range(1, len(temp)):
				now *= temp[k]
				g = math.gcd(g, temp[k])
				now = now // g
				g = now
			if g != now:
				ok.append(now // g)
			else:
				ok.append(now)
		now = 1
		if ok:
			now = ok[0]
			g = ok[0]
		for k in range(1, len(ok)):
			now *= ok[k]
			g = math.gcd(g, ok[k])
			now = now // g
			g = now
		print(now)
solve()


from math import gcd
n = int(input())
crush = list(map(int, input().split()))
crush = [0] + crush
(vis, dis, cyc, flag) = ([0] * (n + 1), [-1] * (n + 1), [], 1)

def dfs(u, d):
	dis[u] = d
	vis[u] = 1
	nonlocal flag
	v = crush[u]
	if vis[v] == 0:
		dfs(v, d + 1)
	elif dis[v] == 1:
		cyc.append(d)
	else:
		flag = 0
	dis[u] = -1
for i in range(1, n + 1):
	if not flag:
		break
	if vis[i] == 0:
		dfs(i, 1)
if not flag or len(cyc) == 0:
	print(-1)
else:

	def lcm(a, b):
		return a * b // gcd(a, b)
	L = cyc[0]
	if not L % 2:
		L //= 2
	for i in range(1, len(cyc)):
		s = cyc[i]
		if not s % 2:
			s //= 2
		L = lcm(L, s)
	print(L)


from math import *
n = int(input())
a = list([int(s) - 1 for s in input().split()])
seen = [0] * n
ans = 1
lcm = 1
for i in range(n):
	if not seen[i]:
		j = i
		leng = 0
		while not seen[j]:
			seen[j] = 1
			leng += 1
			j = a[j]
		if i == j:
			if leng % 2 == 0:
				leng //= 2
			lcm = lcm // gcd(lcm, leng) * leng
		else:
			ans = -1
			break
if ans == -1:
	print(-1)
else:
	print(lcm)


n = int(input())
w = list(map(int, input().split()))
w = [x - 1 for x in w]
ws = sorted(w)
for i in range(n):
	if i != ws[i]:
		print(-1)
		return
a = []
vis = [False] * n
for i in range(n):
	if not vis[i]:
		a.append(0)
		j = i
		while not vis[j]:
			a[-1] += 1
			vis[j] = True
			j = w[j]

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
ans = 1
for i in a:
	if i % 2 == 0:
		i //= 2
	ans = ans // gcd(ans, i) * i
print(ans)


def make_function(n, crushes):

	def f(original_node):
		node = original_node
		x = 0
		for i in range(n):
			node = crushes[node]
			x += 1
			if node == original_node:
				break
		else:
			return -1
		return x / 2 if x % 2 == 0 else x
	return f

def lcd(numbers):
	gcd = lambda x, y: int(x) if y == 0 else int(gcd(y, x % y))
	ret = 1
	for number in numbers:
		ret = ret * number / gcd(ret, number)
	return ret

def main():
	n = int(input())
	a = list(map(int, input().split()))
	crushes = {i: x for (i, x) in zip(list(range(1, n + 1)), a)}
	f = make_function(n, crushes)
	numbers = list(map(int, list(map(f, crushes))))
	if -1 in numbers:
		print(-1)
		return
	print(int(lcd(numbers)))
main()


def read_int():
	return int(input())

def read_ints():
	return map(int, input().split(' '))

def gcd(x, y):
	return x if y == 0 else gcd(y, x % y)
n = read_int()
a = list(read_ints())
vis = [False for i in range(n)]
loop = 0
ok = True
for i in range(n):
	if vis[i]:
		continue
	current = i
	length = 0
	while not vis[current]:
		vis[current] = True
		current = a[current] - 1
		length += 1
	if current != i:
		ok = False
		print(-1)
		break
	if length % 2 == 0:
		length //= 2
	if loop == 0:
		loop = length
	else:
		loop = loop * length // gcd(loop, length)
if ok:
	print(loop)


def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return int(a * b / gcd(a, b))

def run(n, crush):
	visited = [False] * n
	cycle_size = 1
	for i in range(0, n):
		if visited[i]:
			continue
		x = i
		c = 0
		while not visited[x]:
			visited[x] = True
			x = crush[x] - 1
			c += 1
		if x != i:
			return -1
		if c % 2 == 0:
			c /= 2
		cycle_size = lcm(cycle_size, c)
	return cycle_size
n = int(input())
crush = [int(x) for x in input().split()]
print(run(n, crush))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b / gcd(a, b)

def dfs(sad, num, posjeceno):
	posjeceno[sad] = 1
	if posjeceno[a[sad] - 1] == 1:
		return (num + 1, posjeceno)
	return dfs(a[sad] - 1, num + 1, posjeceno)

def trazi(oznaka):
	global memo
	if memo[oznaka] == []:
		memo[oznaka] = dfs(oznaka, 0, [0] * n)
	return memo[oznaka]
n = int(input())
a = list(map(int, input().split()))
d = set()
brojac = n
memo = [[] for e in range(n)]
while brojac > -1:
	brojac -= 1
	if memo[brojac] == []:
		trazi(brojac)
		l = memo[brojac][1]
		for w in range(n):
			if l[w] == 1:
				if trazi(w)[1] != l:
					d = -10
					break
		if d == -10:
			break
	(vrati, l) = trazi(brojac)
	if vrati % 2 == 0:
		d.add(float(vrati / 2))
	else:
		d.add(float(vrati))
if d == -10:
	print(-1)
else:
	sol = 1
	for r in d:
		sol = lcm(sol, r)
	print(int(sol))


from math import gcd
n = int(input())
arr = map(int, input().split())
arr = list(map(lambda x: x - 1, arr))
res = 1
for i in range(n):
	(p, k) = (0, i)
	for j in range(n):
		k = arr[k]
		if k == i:
			p = j
			break
	if k != i:
		print(-1)
		exit()
	p += 1
	if p % 2 == 0:
		p //= 2
	res = res * p // gcd(res, p)
print(res)


def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
	a[i] -= 1
u = [False] * n
ans = 1
for i in range(n):
	if u[i] == False:
		x = i
		c = 1
		while True:
			u[x] = True
			if a[x] == i:
				break
			if u[a[x]] != False:
				print(-1)
				return
			x = a[x]
			c += 1
		if c % 2 == 0:
			c //= 2
		ans = ans * c // gcd(ans, c)
print(ans)


def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())
a = list(map(int, input().split()))
if sorted(a) != [i + 1 for i in range(n)]:
	print(-1)
else:
	ans = 1
	used = [0 for i in range(n)]
	for i in range(n):
		if used[i] == 0:
			j = i
			am = 0
			while used[j] == 0:
				am += 1
				used[j] = 1
				j = a[j] - 1
			if am % 2:
				ans = lcm(ans, am)
			else:
				ans = lcm(ans, am // 2)
	print(ans)


def gcd(x, y):
	return x if y == 0 else gcd(y, x % y)

def lcm(x, y):
	return x // gcd(x, y) * y

def dfs(x, y):
	if vis[x]:
		return y
	vis[x] = 1
	return dfs(c[x - 1], y + 1)
n = int(input())
c = []
for x in input().split():
	c.append(int(x))
vis = [0] * (n + 1)
ind = [0] * (n + 1)
for i in range(n):
	ind[i + 1] += 1
	ind[c[i]] += 1
mk = 1
for i in range(n):
	if ind[i + 1] & 1:
		mk = 0
ans = 1
if mk:
	for i in range(n):
		if vis[i + 1] == 0:
			val = dfs(i + 1, 0)
		if val % 2 == 0:
			val //= 2
		ans = lcm(ans, val)
	print(ans)
else:
	print('-1')


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())
c = [0] + list(map(int, input().split()))
g = [[] for i in range(n + 1)]
use = set()
for i in range(1, n + 1):
	g[c[i]].append(i)
	use.add(c[i])
if len(use) != n:
	print(-1)
	exit()
s = []
for i in range(1, n + 1):
	p = c[i]
	l = 1
	while p != i:
		p = c[p]
		l += 1
	if l % 2 == 0:
		s.append(l // 2)
	else:
		s.append(l)
x = s[0]
for i in range(1, len(s)):
	x = lcm(x, s[i])
print(x)


input()
crush = [0] + [int(x) for x in input().split()]
visited = set()
circle_sizes = []

def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	for i in range(len(crush)):
		if i not in visited:
			(start, cur, count) = (i, i, 0)
			while cur not in visited:
				visited.add(cur)
				count += 1
				cur = crush[cur]
			if cur != start:
				return -1
			circle_sizes.append(count if count % 2 else count // 2)
	if len(circle_sizes) == 1:
		return circle_sizes[0]
	ans = lcm(circle_sizes[0], circle_sizes[1])
	for size in circle_sizes[2:]:
		ans = lcm(ans, size)
	return ans
print(solve())


def gcd(a, b):
	return a if b == 0 else gcd(b, a % b)

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())
c = [int(v) - 1 for v in input().split(' ')]
t = 1
for i in range(n):
	k = i
	for j in range(1, n + 2):
		k = c[k]
		if k == i:
			break
	if j > n:
		print(-1)
		exit()
	t = lcm(t, j if j % 2 != 0 else j // 2)
print(t)


from functools import reduce
n = int(input())
crush = [int(i) - 1 for i in input().split()]

def parity_treat(n):
	if n % 2 == 0:
		return n // 2
	else:
		return n

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)

def lcmm(*args):
	return reduce(lcm, args)
if len(set(crush)) < n:
	print(-1)
else:
	component_size = []
	visited = set()
	for i in range(n):
		if i not in visited:
			tmp = 1
			start = i
			visited.add(start)
			j = crush[start]
			while j != start:
				visited.add(j)
				j = crush[j]
				tmp += 1
			component_size.append(tmp)
	component_size = [parity_treat(i) for i in component_size]
	print(lcmm(*component_size))


n = int(input())
L = list(map(int, input().split()))

def gcd(a, b):
	while b != 0:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a // gcd(a, b) * b
if len(set(L)) < n:
	print(-1)
else:
	a = 1
	for i in range(n):
		if L[i] == 0:
			continue
		u = i
		c = 0
		while L[u] != 0:
			c += 1
			v = L[u] - 1
			L[u] = 0
			u = v
		if c % 2 == 0:
			c //= 2
		a = lcm(a, c)
	print(a)