A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.

The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.

Determine if it is possible for the islanders to arrange the statues in the desired order.


-----Input-----

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.

The second line contains n space-separated integers a_{i} (0 ≤ a_{i} ≤ n - 1) — the statue currently placed on the i-th island. If a_{i} = 0, then the island has no statue. It is guaranteed that the a_{i} are distinct.

The third line contains n space-separated integers b_{i} (0 ≤ b_{i} ≤ n - 1) — the desired statues of the ith island. Once again, b_{i} = 0 indicates the island desires no statue. It is guaranteed that the b_{i} are distinct.


-----Output-----

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.


-----Examples-----
Input
3
1 0 2
2 0 1

Output
YES

Input
2
1 0
0 1

Output
YES

Input
4
1 2 3 0
0 3 2 1

Output
NO



-----Note-----

In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.

In the second sample, the islanders can simply move statue 1 from island 1 to island 2.

In the third sample, no sequence of movements results in the desired position.

Difficulty: competition

Starter code:

Solutions:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
if n <= 3:
    print('YES')
else:
    a.remove(0)
    b.remove(0)
    i = a.index(1)
    a = a[i:] + a[:i]
    i = b.index(1)
    b = b[i:] + b[:i]
    print(['NO','YES'][a == b])


n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.remove(0)
b.remove(0)
i = a.index(b[0])
if(a[i:] + a[:i] == b):
    print('YES')
else:
    print('NO')

readInts=lambda: list(map(int, input().split()))

n=int(input())
a=readInts()
a.remove(0)
b=readInts()
b.remove(0)
pos=a.index(b[0])
a=a[pos:]+a[:pos]
print('YES' if a==b else 'NO')

n = int(input())
start = [x for x in input().split(' ') if not x == '0']
end = [x for x in input().split(' ') if not x == '0']

index1 = start.index('1')
index2 = end.index('1')

for i in range(n - 1):
    if not start[index1] == end[index2]:
        print('NO')
        break
    index1 = (index1 + 1) % (n - 1)
    index2 = (index2 + 1) % (n - 1)
else:
    print('YES')

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.remove(0)
b.remove(0)
i = a.index(b[0])

if(a[i:] + a[:i] == b):
    print('YES')
else:
    print('NO')

3.5
n=int(input())
a=input().split()
b=input().split()
a.remove('0')
b.remove('0')
k=a.index(b[0])
a=a[k:]+a[:k]
if a==b:
    print('YES')
else:
    print('NO')

import sys
n = int(input())
a = []
b = []
x = map(int, input().split())
for i in x:
    if i > 0:
        a.append(i)
x = map(int, input().split())
for i in x:
    if i > 0:
        b.append(i)
n = n - 1
for i in range(0, n):
    if b[i] == a[0]:
        for j in range(0, n):
            if a[j] != b[(i + j) % n]:
                print("NO")
                return
        break
print("YES")

n=int(input())
a=input().split()
b=input().split()
a.remove('0')
b.remove('0')
k=a.index(b[0])
a=a[k:]+a[:k]
if a==b:
    print('YES')
else:
    print('NO')

import math, re, sys, string, operator, functools, fractions, collections
sys.setrecursionlimit(10**7)
RI=lambda x=' ': list(map(int,input().split(x)))
RS=lambda x=' ': input().rstrip().split(x)
dX= [-1, 1, 0, 0,-1, 1,-1, 1]
dY= [ 0, 0,-1, 1, 1,-1,-1, 1]
mod=int(1e9+7)
eps=1e-6
pi=math.acos(-1.0)
MAX=200010
#################################################
n=RI()[0]
a=RI()
b=RI()
a=[i for i in a if i]
b=[i for i in b if i]
if len(a)!=len(b) or (len(a)==n and a!=b):
    print("NO")
    return
ID=[0]*MAX
for i in range(len(a)):
    ID[a[i]]=(a[i-1],a[(i+1)%len(a)])
for i in range(len(b)):
    if ID[b[i]]!=(b[i-1],b[(i+1)%len(b)]):
        print("NO")
        return
print("YES")








n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

i = 0
t = 0 if a[0] != 0 else 1
for j in range(n):
    if a[t] == b[j]:
        i = j
        break
bl = True
for j in range(n):
    if b[i % n] != a[j]:
        if a[j] == 0:
            continue
        elif b[i % n] == 0 and b[(i + 1) % n] == a[j]:
            i += 2
            continue
        bl = False
        break
    i += 1
print("YES" if bl else "NO")


def main():
    input()
    aa = input().split()
    bb = input().split()
    aa.remove("0")
    bb.remove("0")
    a = aa.index("1")
    b = bb.index("1")
    print(("NO", "YES")[aa[a:] + aa[:a] == bb[b:] + bb[:b]])


def __starting_point():
    main()

__starting_point()

N = int(input()) #количество островов
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A.remove(0)
B.remove(0)
ind_b = B.index(A[0])
R = B[ind_b:] + B[0:ind_b]
if A == R:
    print("YES")
else:
    print("NO")


def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	print(solver(a, b))
	
def solver(a, b):
	a.remove(0)
	b.remove(0)
	index = b.index(a[0])
	if b[index:] + b[:index] == a:
		return "YES"
	else:
		return "NO"

# def isRotation(a, b):
# 	if len(a) != len(b):
# 		return False
# 	else:
# 		b += b
# 		for i in range(len(a)):
# 			if a == b[i: i + len(a)]:
# 				return True
# 		return False

#def rotateLeft(L):
#	L = L[1:] + [L[0]]
#	return L

# L1 = [3, 4, 2, 1]
# L2 = [1, 2, 3, 4]
# print(isRotation(L1, L2))
main()
# a = [1, 0, 2]
# b = [2, 0, 1]
# print(solver(a, b))

# a2 = [1, 0]
# b2 = [0, 1]
# print(solver(a2, b2))

# a3 = [1, 2, 3, 0]
# b3 = [0, 3, 2, 1]
# print(solver(a3, b3))


no_islands = int(input())
initial = input().split()
final = input().split()
initial.remove('0')
final.remove('0')

first = initial.index(final[0])
if initial[first:] + initial[:first] == final:
	print('YES')
else:
	print('NO')

n=int(input())
a=input().split()
b=input().split()
a.remove('0')
b.remove('0')
k=a.index(b[0])
a=a[k:]+a[:k]
if a==b:
    print('YES')
else:
    print('NO')


input()
f = lambda: (' ' + input()).replace(' 0', '')
a, b = f(), f()
print('YES' if a in b + b else 'NO')

n = int(input())
s1 = list(input().strip().split())
s2 = list(input().strip().split())
s1.remove('0')
s2.remove('0')
s1 = ''.join(s1)
s2 = ''.join(s2)
s1 = s1+s1
if s2 in s1:
    print("YES")
else:
    print("NO")


n=int(input())

a=input().split()

b=input().split()

a.remove('0')

b.remove('0')

k=a.index(b[0])

a=a[k:]+a[:k]

if a==b:

    print('YES')

else:

    print('NO')





# Made By Mostafa_Khaled


n = int(input())

a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

if a[0] != 0:
    index_b = b.index(a[0])
else:
    index_b = b.index(a[1])

for index_a in range(0,len(a)):
    if a[index_a] == 0:
        continue
    if b[index_b%len(a)] == 0:
        index_b += 1

    if a[index_a] != b[index_b%len(a)]:
        print('NO')
        return
    index_b += 1

print('YES')


#! Python3

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

l2 = []
l1 = []
i = 0
ind = a.index(0)
while(ind != n):
    l1.append(a[ind])
    ind = ind+1
while(len(l1) != n):
    l1.append(a[i])
    i += 1

i=0
l3 = []
ind = b.index(0)
while(ind != n):
    l3.append(b[ind])
    ind = ind+1
while(len(l3) != n):
    l3.append(b[i])
    i += 1

i=0
ind = l3.index(l1[1])
while(ind != n):
    l2.append(l3[ind])
    ind = ind+1
while(len(l2) != n-1):
    if l3[i] != 0:
        l2.append(l3[i])
    i += 1

if(l1[1:] == l2):
    print("YES")
else:
    print("NO")


import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

st = set()
idx = 0

i = 0
j = 0
while i < n and a[i] == 0: i += 1
while j < n and a[i] != b[j]: j += 1

b = b[j:] + b[:j] 
j = 0
while i < n:
    if a[i] != 0:
        while j < n and b[j] == 0: j += 1
        if b[j] != a[i] or j == n:
            print('NO')
            return
        j += 1
    i += 1
print('YES')
        


n = int(input())
l1 = list(map(int, input().split()))
l2 = list(map(int, input().split()))

for k in range(n):
    if l1[k] == 0:
        l1 = l1[:k] + l1[k + 1:]
        break
for k in range(n):
    if l2[k] == 0:
        l2 = l2[:k] + l2[k + 1:]
        break

i = 0
j = 0
ans = True
for k in range(n - 1):
    if l1[k] == 1:
        i = k
    if l2[k] == 1:
        j = k
    
for k in range(n - 1):
    if l1[(i + k) % (n - 1)] != l2[(j + k) % (n - 1)]:
        ans = False
        break
    
if ans:
    print("YES")
else:
    print("NO")


def run():
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]

    i = a.index(1) 
    j = b.index(1) 
    ok = True
    cont = 0
    while cont < n:
        if(a[i] != b[j]):
            ok = False
            break
        i = (i + 1)%n
        j = (j + 1)%n
        
        if(a[i] == 0):
            i = (i + 1)%n
        if(b[j] == 0):
            j = (j + 1)%n

        cont +=1
    
    print('YES' if ok else 'NO')



def __starting_point():
    run()
__starting_point()

n = int(input())
s1 = input().split()
s2 = input().split()

st1 = 0
st2 = 0
for i in range(n):
    if s1[i] == '1':
        st1 = i
    if s2[i] == '1':
        st2 = i

i = (st1 + 1) % n
j = (st2 + 1) % n
f = True
steps = 0
while steps < n:
    if s1[i] == '0':
        i = (i + 1) % n
    if s2[j] == '0':
        j = (j + 1) % n
    if s1[i] != s2[j]:
        f = False
        break
    i = (i + 1) % n
    j = (j + 1) % n
    steps += 1

if f:
    print('YES')
else:
    print('NO')