Harryxun/stf_run huggingface dataset:
# line = "4 3 2 1"
# L = [int(x) for x in line.split()]
# n = len(L)

n = int(input())
line = input()
L = [int(x) for x in line.split()]

flags = [False] * n
loops = []

for i, x in enumerate(L):
    k = x - 1
    if flags[k]:
        continue
    if i == k:
        flags[i] = True
        continue

    loop = []
    while x not in loop:
        flags[k] = True
        loop.append(x)
        x = L[k]
        k = x - 1
    loops.append(loop)

cost = 0
for loop in loops:
    m = min(loop)
    cost += m * (len(loop)-1) + sum(loop) - m

print(cost)

### FIXED CODE ###

n = int(input())
line = input()
L = [int(x) for x in line.split()]

flags = [False] * n
loops = []

S = sorted(L)
for idx, x in enumerate(L):
    k = S.index(x)
    if flags[k]:
        continue
    if k == idx:
        flags[k] = True
        continue

    ws = []
    while x not in ws:
        flags[k] = True
        ws.append(x)
        x = L[k]
        k = S.index(x)
    loops.append(ws)

cost = 0
for ws in loops:
    n = len(ws)
    a = sum(ws) + (n - 2) * min(ws)
    b = sum(ws) + min(ws) + (n + 1) * min(L)
    cost += min(a, b)

print(cost)
# -*- coding:utf-8 -*-
import sys
sys.setrecursionlimit(1000000)

def preDatasets(q):
	line = input()
	print(line)
	datas[q] = line
	return datas

def dfs(x, y, sym):
	tile[x][y] = '.'
	for i in range(len(DX)):
		nx = x + DX[i]
		ny = y + DY[i]
		if nx>=0 and nx<N and ny>=0 and ny<M and tile[nx][ny]==sym:
			dfs(nx, ny, sym)

def solve():
	res = 0
	for i in range(N):
		for j in range(M):
			if not tile[i][j] == '.':
				if tile[i][j] == '@':
					dfs(i, j, '@')
					res += 1
				elif tile[i][j] == '#':
					dfs(i, j, '#')
					res += 1
				elif tile[i][j] == '*':
					dfs(i, j, '*')
					res += 1
	print(res)

index = 0	
counter=0
datas = [[0 for i in range(1)]for j in range(1)] 
DX = [-1, 0, 1, 0]
DY = [0, -1, 0, 1]
while True:
	line = input()
	
	if ' ' in line:
		N = int(datas[index].split(' ')[0])
		M = int(datas[index].split(' ')[1])
		tile = [[0 for i in range(N)]for j in range(M)]

	if N == 0:
		break

	if not counter<=M:
		for i in range(len(tile)):
			tile[i] = list(datas[index+i+1])
		solve()
		index += N+1

	counter+=1

### FIXED CODE ###

# -*- coding:utf-8 -*-
import sys
def dfs(x, y, sym):
	tile[x][y] = "."
	for i in range(len(DX)):
		nx = x + DX[i]
		ny = y + DY[i]
		if (0<=nx and nx<N and 0<=ny and ny<M and tile[nx][ny] is sym):
			dfs(nx, ny, sym)

def solve():
	res = 0
	for i in range(N):
		for j in range(M):
			sym = tile[i][j]
			if tile[i][j] != ".":
				if tile[i][j] is sym:
					dfs(i, j, sym)
					res += 1
	print(res)

counter=0
DX = [-1,0,1,0]
DY = [0,-1,0,1]
sys.setrecursionlimit(10**7)
while True:
	line = input()
	if " " in line:
		nums = line.split(" ")
		N = int(nums[0])
		M = int(nums[1])
		tile = [[0 for i in range(M)] for j in range(N)]
	else:
		tile[counter-1] = list(line)
	if N is 0 and M is 0:
		break
	counter += 1
	if counter-1 is N:
		solve()
		counter = 0
s_vec1 = input()
s_vec2 = input()
def abc_057_2(o,e):
    s_vec3 = []
    for i in range(0,len(e)):
        s_vec3.append(o[i])
        s_vec3.append(e[i])
    if len(o) == len(e):
        pass
    else:
        s_vec3.append(o[i+1])
    return(''.join(s_vec3))
print(abc_057_2(s_vec1,s_vec2))

### FIXED CODE ###

n_vec = list(map(int, input().split()))
n1 = n_vec[0]
n2 = n_vec[1]
n3 = n_vec[2]
def abc_057_1(a,b,c):
    if b-a == c-b:
        return(print('YES'))
    else:
        return(print('NO'))
abc_057_1(n1,n2,n3)
S = input()
hantei = True

for i in range(0,len(S),2):
  if S[i]+S[i+1] != 'hi':
    hantei = False

if hantei == True:
  print('Yes')
else:
  print('No')

### FIXED CODE ###

S = input()
hantei = True

for i in range(0,len(S)-1,2):
  if S[i]+S[i+1] != 'hi':
    hantei = False
if len(S) % 2 == 1:
  hantei = False
    
if hantei == True:
  print('Yes')
else:
  print('No')
a,b = map(int,input().split())
if (a  * b % ==0):
  print("Even")
else:
  print("Odd")

### FIXED CODE ###

a,b = map(int,input().split())
if a*b % 2 ==0:
  print("Even")
else:
  print("Odd")
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
Z = []
for i in range(1, N):
    Z.append(abs(A[i] - A[i - 1]))
Z.sort()
i = 0
while True:
    if i <= len(Z):
        break
    if Z[i] == 0:
        del Z[i]
        i -= 1
    i += 1

    
for i in range(1, len(Z)):
    ans = -1
    if Z[i - 1] % Z[i] != 0:
        ans = 1
    
for i in A:
    if K == i:
        ans = 1
        
if K >= max(A):
    ans = -1
    
if ans == -1:
    print("IMPOSSIBLE")
else:
    print("POSSIBLE")


### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
import copy
Z = copy.deepcopy(A)
#for i in range(1, N):
 #   Z.append(abs(A[i] - A[i - 1]))
Z.sort()
i = 0

while True:
    if i >= len(Z):
        break
    if Z[i] == 0:
        del Z[i]
        i -= 1
    elif i > 0 and Z[i] == Z[i - 1]:
        del Z[i]
        i -= 1
    i += 1

ans = -1

import fractions
gcd = Z[0]
for i in range(1, len(Z)):
    gcd = fractions.gcd(gcd, Z[i])

if K % gcd == 0:
    ans = 1
    
if K > max(A):
    ans = -1
       
for i in A:
    if K == i:
        ans = 1
        break

if ans == -1:
    print("IMPOSSIBLE")
else:
    print("POSSIBLE")
N, M = map(int, input().split())
list_A=[]
for i in range(M):
    A = list(map(int, input().split()))
    list_A.append( A )

list_p = list(map(int, input().split()))

i = 0



list_func = []#スイッチ点灯の全組み合わせを入れる
cnt=0

def func(i):
    if (i == N):
        flag = True
        for k in range(M):
            sum = 0
            for j in list_A[k]:
                sum += j
            sum = sum - list_A[k][0]
            if sum %2 != list_p[k]:
                flag = False
        if flag == True:
            cnt+=1
        return
    list_func.append(1)
    func(i+1)
    list_func.pop(-1)

    list_func.append(0)
    func(i+1)
    list_func.pop(-1)

    return


func(0)
print (cnt)

### FIXED CODE ###

N, M = map(int, input().split())
list_Connect=[]
for i in range(M):
    A = list(map(int, input().split()))
    A.pop(0)
    list_Connect.append( A )

list_p = list(map(int, input().split()))

i = 0



state = []#スイッチ点灯の全組み合わせを入れる
sum=0

def func(i):
    global sum
    if i == N:
        flag = True
        for i in range(M):
            cnt= 0
            for j in list_Connect[i]:
                cnt += state[j-1]
            if cnt %2 != list_p[i]:
                flag = False
                break
        if flag == True:
           sum+=1
    else:
        state.append(1)
        func(i+1)
        state.pop(-1)
        state.append(0)
        func(i+1)
        state.pop(-1)

    return


func(0)
print (sum)
import numpy as np

A,B,H,M = map(int,input().split())

th_a = 30*H
th_b = 6*M

theta = th_a -th_b

if theta <= 0:
  theta = -theta

if theta == 0:
  if A >= B:
    print(A-B)
  else:
    print(B-A)

elif theta == 180:
  print(A+B)

else:
  print(np.sqrt(A**2 + B**2 - 2*A*B*cos(theta)))

### FIXED CODE ###

import numpy as np

A,B,H,M = map(int,input().split())

th_a = 30*H + 0.5 * M
th_b = 6*M

theta = th_a -th_b

if theta <= 0:
  theta = -theta
if theta >= 180:
  theta = 360 - theta

if theta == 0:
  if A >= B:
    print(A-B)
  else:
    print(B-A)

elif theta == 180:
  print(A+B)

else:
  print(np.sqrt(A**2 + B**2 - 2*A*B*np.cos(theta*np.pi/180)))
def solve():
  N = int(input())
  X = input()
  bl = X.count('1')
  bitp = 0
  for i in range(N):
    bitp += int(X[i])*pow(2,N-1-i)
    bitp %= bl+1
  bitm = 0
  if bl>0:
    for i in range(N):
      bitm += int(X[i])*pow(2,N-1-i)
      bitm %= bl-1
  
  ans = [0]*N
  dp = [0]*(N+1)
  for i in range(1,N+1):
    dp[i] = dp[i%(bin(i).count('1'))]+1
  for i in range(N):
    if X[i]=='1':
      bit1 = bitm - pow(2,N-1-i)
      bl1 = bl-1
      if bl1==0:
        ans[i] = 0
        continue
    else:
      bit1 = bitp + pow(2,N-1-i)
      bl1 = bl+1
    ans[i] = dp[bit1%bl1]+1
  return ans
print(*solve(),sep='\n')

### FIXED CODE ###

def solve():
  N = int(input())
  X = input()
  x = sum(map(int,list(X)))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = dp[i%sum(map(int,list(bin(i)[2:])))]+1
  cnt1 = 0
  if x>1:
    for i in range(N):
      cnt1 += int(X[-i-1])*pow(2,i,x-1)
      cnt1 %= x-1
  cnt2 = 0
  for i in range(N):
    cnt2 += int(X[-i-1])*pow(2,i,x+1)
    cnt2 %= x+1
  
  ans = [0]*N
  for i in range(N):
    if X[i]=='1':
      if x==1:
        ans[i] = 0
      else:
        num = (cnt1-pow(2,N-i-1,x-1))%(x-1)
        ans[i] = dp[num]+1
    else:
      num = (cnt2+pow(2,N-i-1,x+1))%(x+1)
      ans[i] = dp[num]+1
  return ans
print(*solve(),sep='\n')
if input() + input() >= input():
    print("Yes")
else:
    print("No")

### FIXED CODE ###

a, b, c = map(int, input().split())
if a + b >= c:
    print("Yes")
else:
    print("No")
A,B, C,K=map(int,input().split())
if K<= A:
    prin(K)
elif K<=A+B:
    print(A)
else:
    print(2*A+B-K)  

### FIXED CODE ###

A,B, C,K=map(int,input().split())
if K<= A:
    print(K)
elif K<=A+B:
    print(A)
else:
    print(2*A+B-K)
n, a, b = map(int, input())
print(min(a * n, b))

### FIXED CODE ###

n, a, b = map(int, input().split())
print(min(a * n, b))
n=int(raw_input())
maxv=n
for a in xrang(1,n+1):
  b=n-a
  if a>b: break
  sa=sum([int(x) for x in str(a)])
  sb=sum([int(x) for x in str(b)])
  maxv=min(maxv, sa+sb)
print(maxv)

### FIXED CODE ###

n = input()

powers10 = [10**i for i in range(1, 6)]
print(10 if int(n) in powers10 else sum(int(d) for d in n))
from math import gcd

n = int(input())
a = list(map(int, input().split()))
p = list(a)
s = list(a)
for i in range(1, n):
    p[i] = gcd(p[i - 1], p[i])
for i in range(n - 2, -1, -1):
    s[i] = gcd(s[i], s[i + 1])

ans = max(p[-2], s[1])
ans = max(ans, max(gcd(p[i - 1], s[i + 1]) for i in range(1, n - 1)))
print(ans)


### FIXED CODE ###

from fractions import gcd

n = int(raw_input())
a = list(map(int, raw_input().split()))
p = list(a)
s = list(a)
for i in range(1, n):
    p[i] = gcd(p[i - 1], p[i])
for i in range(n - 2, -1, -1):
    s[i] = gcd(s[i], s[i + 1])

ans = max(p[-2], s[1])
if n > 2:
    ans = max(ans, max(gcd(p[i - 1], s[i + 1]) for i in range(1, n - 1)))

print(ans)
circumference = 2 * math.pi * input()
print(circumference)

### FIXED CODE ###

R = int(input())
circumference = 2 * 3.14 * R
print(circumference)
s=input()

for i in range(len(s)):
  if i%2==0:
    if s[i]==="L":
      print("No")
      sys.exit()
      
  else:
    if s[i]=="R":
      print("No")
      sys.exit()
      
print("Yes")

    
        
      

### FIXED CODE ###

import sys

s = input()

for i in range(len(s)):
    if i%2 == 0:
        if s[i] == "L":
            print("No")
            sys.exit()
            
    else:
        if s[i] == "R":
            print("No")
            sys.exit()
print("Yes")
def gcd(a, b):
    if a % b == 0:
        return b
    else:
        return gcd(b, a % b)

def factorize(n):
    result = {}
    dev = 2
    while dev ** 2 <= n:
        cnt = 0
        while n % dev == 0:
            n //= dev
            cnt += 1
        result[dev] = cnt
        dev += 1
    if n > 1:
        if max(result) == n:
            result[n] += 1
        else:
            result[n] = 1
    return result

A, B = [int(x) for x in input().split()]
fct = factorize(gcd(A, B))
print(len(fct) + 1)

### FIXED CODE ###

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

def factorize(n):
    a = 2
    lst = []
    while a * a <= n:
        if n % a == 0:
            n = n / a
            lst.append(a)
        else:
            a += 1
    if n > 1:
        lst.append(n)
    return lst

A, B = [int(x) for x in input().split()]
lst = factorize(gcd(A, B))
print(len(set(lst)) + 1)
N=input()
lenN=len(N)
ans=0


for i in range(1,lenN,2):
    ans+=10**i*9//10
    #print(ans)

if lenN%2==1:
    ans+=int(N[1:])+1
    
print(ans)
    
    


#0をひかない

### FIXED CODE ###

N=input()
lenN=len(N)
ans=0

for i in range(1,int(N)+1):
    if len(str((i)))%2==1:
        ans+=1
    
print(ans)
def main(n,s):
    if n == 1:
        return 1
        
    count = 1
    for i in range(1,n):
        if s[i] != s[i-1]:
            count += 1
    return count

inp = input().split("\n")
n = int(inp[0])
s = inp[1]


print(main(n,s))

### FIXED CODE ###

length = input()
string = input()

color = 1
prev = string[0]
modified = string[0]


for c in string :
    if c == prev :
        continue
    else :
        prev = c
        color = color + 1
        modified = modified + c

print(color)
x,y=map(int, input().split())
flag=0
for i in range(100):
 for j in range(100):
  if i+j==x and i*2+j*4=y:
    flag=1
    exit()
  else:
    pass
if flag==1:
  print("Yes")
else:
  print("No")


### FIXED CODE ###

a,b=map(int,input().split())
ans=1
for i in range(0,b):
  for j in range(0,b):
   if i+j==a and i*2+4*j==b:
    ans+=1
    break
   else:
     pass

if ans>1:
  print("Yes")
else:
  print("No")
n,k=map(int,input().split())
xs=[0]+list(map(int,input().split()))
from itertools import accumulate
As=list(accumulate(xs))
r = 0
for i in range(k+1):
    for j in range(i, k+1):
        x = i, j-i, k-j
        m1 = As[i] + As[n] - As[n-j+i]
        ss = sorted(z for z in (xs[:i]+xs[n-j+i+1:]) if z < 0)
        m = m1 - sum(ss[:min(k-j,len(ss))])
#        if m > r:
#            print(x, m, m1)
        r = max(r, m)
print(r)


### FIXED CODE ###

n,k=map(int,input().split())
xs=[0]+list(map(int,input().split()))
from itertools import accumulate
As=list(accumulate(xs))
r = 0
for i in range(k+1):
    for j in range(i, k+1):
        x = i, j-i, k-j
        if j > n:
            continue
        m1 = As[i] + As[n] - As[n-j+i]
        m = m1 - sum(sorted(z for z in (xs[:i]+xs[n-j+i+1:]) if z < 0)[:k-j])
#        if m > r:
#            print(x, m, m1)
        r = max(r, m)
print(r)
s = input()
x = int(input())
tank = []
for c in list(s):
    if c=='a':
        tank.append(0)
    else:
        tank.append(ord('z') + 1 - ord(c))
res = ''
for i in range(len(tank)):
    if tank[i] == 0:
        res += s[i]
    elif tank[i] <= x:
        x -= tank[i]
        tank[i] = 0
        res += 'a'
    else:
        res += s[i]
else:
    if x != 0:
        n = ord(s[-1])+x
        res = res[:-1] + chr(n)
print(res)

### FIXED CODE ###

s = input()
x = int(input())
tank = []
for c in list(s):
    if c=='a':
        tank.append(0)
    else:
        tank.append(ord('z') + 1 - ord(c))
res = ''
for i in range(len(tank)):
    if tank[i] == 0:
        res += s[i]
    elif tank[i] <= x:
        x -= tank[i]
        tank[i] = 0
        res += 'a'
    else:
        res += s[i]

c = res[-1]
x %= 26
if ord(c)+x <= ord('z'):
    res = res[:-1] + chr(ord(c)+x)
else:
    res = res[:-1] + chr(ord(c)+x-ord('z')+ord('a')-1)
print(res)
N = int(input()) 
for i in range(N) :
   a,b,c = sort(list(map(int,input().split(" "))))
  
   if a**2 + b**2== c**2:
     print("YES")
   else
     print("NO")


### FIXED CODE ###

n=int(input())
for i in range(n):
    a,b,c = input().split()
    x = int(a)
    y = int(b)
    z = int(c)
    p = [x,y,z]
    p.sort()
    if pow(p[0],2) + pow(p[1],2) == pow(p[2],2):
        print('YES')
    else:
        print('NO')
#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    targets = LI()
    MAX_INT = max(targets) + 1
    targets = set(targets)
    is_pw = True

    if len(targets) != N:
        is_pw = False

    if len(targets) == 1:
        if targets[0] == 1:
            print("setwise coprime")
            return
        print("not coprime")
        return

    for i in range(2, MAX_INT):
        origin = i
        kosuu = 0
        while origin <= MAX_INT:
            if origin in targets:
                kosuu += 1
            origin += i

        if kosuu <= 1:
            continue
        elif kosuu < len(targets):
            is_pw = False
        else:
            print("not coprime")
            return

    if is_pw:
        print("pairwise coprime")
        return
    print("setwise coprime")


main()


### FIXED CODE ###

#!/usr/bin/env python3
import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    targets = LI()
    MAX_INT = max(targets) + 1
    counter = cl.Counter(targets)
    targets = set(targets)
    is_pw = True

    num_1 = counter[1]
    if num_1 > 1:
        N -= num_1 - 1

    if len(targets) != N:
        is_pw = False

    if len(targets) == 1:
        if 1 in targets:
            print("pairwise coprime")
            return
        print("not coprime")
        return

    for i in range(2, MAX_INT):
        origin = i
        kosuu = 0
        while origin <= MAX_INT:
            if origin in targets:
                kosuu += 1
            origin += i

        if kosuu <= 1:
            continue
        elif kosuu < len(targets):
            is_pw = False
        else:
            print("not coprime")
            return

    if is_pw:
        print("pairwise coprime")
        return
    print("setwise coprime")


main()
import numpy as np

H, W, M = map(int, input().split())
h = np.zeros(H)
w = np.zeros(W)
m = np.zeros((H, W))
ans = 0
for _ in range(M):
    tmp_h, tmp_w = map(int, input().split())
    h[tmp_h-1] += 1
    w[tmp_w-1] += 1
    m[tmp_h-1, tmp_w-1] = 1.

h_max = np.max(h)
h_max_index = list(zip(*np.where(h == h_max)))
w_max = np.max(w)
w_max_index = list(zip(*np.where(w == w_max)))


for i in h_max_index:
    for j in w_max_index:
        try:
            if m[i[0]][j[0]] == 1.:
                tmp = h_max + w_max -1
            else:
                tmp = h_max + w_max
            if ans < tmp:
                ans = tmp
        except IndexError:
            pass
print(int(ans))


### FIXED CODE ###

import numpy as np

H, W, M = map(int, input().split())
h = np.zeros(H)
w = np.zeros(W)
m = {}
flag = 0

for _ in range(M):
    tmp_h, tmp_w = map(int, input().split())
    h[tmp_h-1] += 1
    w[tmp_w-1] += 1
    m[(tmp_h-1, tmp_w-1)] = 1.

h_max = np.max(h)
h_max_index = list(zip(*np.where(h == h_max)))
w_max = np.max(w)
w_max_index = list(zip(*np.where(w == w_max)))

for i in h_max_index:
    for j in w_max_index:
        if (i[0], j[0]) not in m.keys():
            flag = 1
            break

if flag == 1:
    print(int(h_max + w_max))
else:
    print(int(h_max + w_max - 1))
yyyy = int(input())
mm = int(input())
dd = int(input())
if yyyy < 2019:
    print('Heisei')
    break
elif yyyy > 2019:
    print('TBD')
    break
else:
    if mm < 5:
        print('Heisei')
        break
    else:
        print('TBD')

### FIXED CODE ###

S = input()
if S <= '2019/04/30' :
    print('Heisei' )
else:
    print('TBD')
import sys

def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    diff_idx = -1
    min_idx = -1
    _min = 10*6
    for i in range(len(a)-1):
        if a[i] * a[i+1] <= 0 and (a[i] != 0 or a[i+1] != 0):
            diff_idx = i
            break
        tmp_min = abs(a[i]) + abs(a[i+1])
        if tmp_min < _min:
            _min = tmp_min
            min_idx = i

    start_idx = diff_idx if diff_idx != -1 else tmp_min
    ops = []

    a2 = a[start_idx:]
    a2.extend(a[:start_idx])

    b,s = (a[0],a[1]) if a[0] > a[1] else (a[1],a[0])

    if a2[2] < 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    for i in range(2,len(a2)-1):
        b = curr
        s = a[i]

        if curr < a[i]:
            b,s = s,b

        if a2[i+1] < 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    if a2[-1] < 0:
        ops.append((curr, a2[-1]))
        curr = curr - a2[-1]
    else:
        ops.append((a2[-1], curr))
        curr = a2[-1] - curr

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()

### FIXED CODE ###

import sys

def bigsmall(x,y):
    return (x,y) if x>y else (y,x)

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()

    if len(a) == 2:
        b,s = bigsmall(a[0],a[1])
        print(b-s)
        print(b,s)
        sys.exit()

    ops = []
    curr = 0
    b,s = bigsmall(a[0],a[-1])
    if a[1] <= 0:
        ops.append((b,s))
        curr = b - s
    else:
        ops.append((s,b))
        curr = s - b

    del a[-1]
    a.append(-1) # to set answer > 0

    for i in range(1, len(a)-1):
        b,s = bigsmall(curr,a[i])

        if a[i+1] <= 0:
            ops.append((b,s))
            curr = b - s
        else:
            ops.append((s,b))
            curr = s - b

    print(curr)
    for x,y in ops:
        print(x,y)

if __name__=='__main__':
    main()
import sys
n = int(input())
a = input()
if n == 2:
  print(abs(int(a[0]-int(a[1]))))
  sys.exit()

key = [0]*(n-1)
judge = False
for i in range(n-1):
  key[i] = abs(int(a[i])-int(a[i+1]))
  if key[i] == 1:
    judge = True

comb = 1
ans = 0
for i in range(n-1):
  ans += comb*key[i]
  ans %= 2
  comb *= (n-3-i)
  comb *= pow(i+1, 7, 2)
if ans == 0 and judge == False:
  ans = 2
print(ans)

### FIXED CODE ###

import sys
import random
input = sys.stdin.readline


def solve(n, s):
    sub = [int(s[i])-1 for i in range(n)]
    
    table = [0]*(n+1)
    p = 2
    for i in range(len(bin(n))-1):
        for j in range(p, n+1, p):
            table[j] += 1
        p *= 2
    
    for i in range(n):
        table[i+1] += table[i]
    
    ans = 0
    for i in range(n):
        if sub[i]%2:
            judge = table[n-1]-table[n-1-i]-table[i]
            if not judge:
                ans += 1
    
    
    if ans%2:
        res = 1
    else:
        if 1 in sub:
            res = 0
        else:
            res = 0
            for i in range(n):
                if sub[i]:
                    judge = table[n-1]-table[n-1-i]-table[i]
                    if not judge:
                        res += 1
            return res%2*2
    
    return res
    
    
def main():
    n = int(input())
    s = input()
    
    ans = solve(n, s)
    print(ans)
    
    
if __name__ == "__main__":
    main()
import sys
sys.setrecursionlimit(1000000)
def rec(n):
    if n == 1: 
        return 1
    return rec(n/2)*2+1

n = int(input())
ans = 0
ans += rec(n)
print(ans)

### FIXED CODE ###

import math
h = int(input())
l = 1

while (h != 1):
    h = math.floor(h / 2)
    l += 1
ans = 0

for i in range(l):
    ans += 2**i
print(ans)