CodeEff/ECCO huggingface dataset:
# Original solution
a,b,c,k = list(map(int, input().split()))

ans = 0
if k <= a:
    ans = k
    k = 0
else:
    ans = a
    k -= a
if k <= b:
    k = 0
else:
    k -= b
ans = ans - k
print(ans)


# Optimized solution
a,b,c,k=list(map(int,input().split()))
print((min(k,a)-max(k-a-b,0)))
# Original solution
a,b,c,k=list(map(int,input().split()))
print((min(k,a)-max(k-a-b,0)))

# Optimized solution
a,b,c,k=list(map(int,input().split()))
print((min(2*a+b-k,a,k)))
# Original solution
a,b,c,k = list(map(int, input().split()))
if k <= a:
    ans = k
    print(ans)
else:
    ans = a
    k = k-a
    if k <= b:
        print(ans)
    else:
        k = k-b
        print((ans-k))

# Optimized solution
a,b,c,k = list(map(int, input().split()))
if k <= a:
    print(k)
else:
    ans = a
    k = k-a
    if k <= b:
        print(ans)
    else:
        k = k-b
        print((ans-k))
# Original solution
#Solver:Rute
a,b,c,k = list(map(int,input().split()))
if k<=a:
  print(k)
elif a<k<a+b:
  print(k)
else:
  #-1のカードの枚数超過 (k-(a+b))枚
  print((a-(k-(a+b))))

# Optimized solution
#Solver:Rute
a,b,c,k = list(map(int,input().split()))
if 0<=k<=a:
  print(k)
elif a<k<=a+b:
  print(a)
else:
  print((a-(k-(a+b))))
# Original solution
A, B, C, K = list(map(int, input().split()))
if K <= A:
    print(K)
    exit(0)
K -= A
ans = A
if K <= B:
    print(ans)
    exit(0)
K -= B
print((ans-K))


# Optimized solution
A, B, C, K = list(map(int, input().split()))
if K <= A:
    print(K)
    exit(0)
K -= A
if K <= B:
    print(A)
    exit(0)
print((A-(K-B)))
# Original solution

def resolve():
    A, B, C, K = map(int, input().split())

    ans = 0
    cnt = min(K, A)
    ans += cnt * 1
    K -= cnt
    if K < 1:
        return print(ans)

    cnt = min(K, B)
    ans += cnt * 0
    K -= cnt
    if K < 1:
        return print(ans)

    ans += K * -1

    print(ans)

if __name__ == "__main__":
    resolve()


# Optimized solution

def resolve():
    a, b,c,k = list(map(int, input().split()))

    ans = 0
    used = min(k,a)
    ans += used * 1
    k -= used
    used = min(k, b)
    k -= used
    ans += k*(-1)
    print(ans)


if __name__ == "__main__":
    resolve()
# Original solution
a,b,c,k = list(map(int, input().split()))
count = 0

if a <= k:
    count += a
else:
    count += k
    print(count)
    exit()
k -= a

if b <= k:
    k -= b
else:
    print(count)
    exit()


for i in range(k):
    count -= 1

print(count)

# Optimized solution
a,b,c,k = list(map(int, input().split()))
count = 0

if a <= k:
    count += a
else:
    count += k
    print(count)
    exit()
k -= a

if b <= k:
    k -= b
else:
    print(count)
    exit()


count -= k

print(count)
# Original solution
a, b, c, k = list(map(int, input().split()))
if k <= a:
  print(k)
elif k <= (a+b):
  print(a)
else:
  print((a-(k-a-b)))

# Optimized solution
a, b, c, k = list(map(int, input().split()))
if k <= a:
    print(k)
elif k <= a+b:
    print(a)
else:
    print((a-(k-a-b)))
# Original solution
a,b,c,k = list(map(int,input().split()))
if a+b >= k:
    if a > k:
        print(k)
    else:
        print(a)
else :
    print((a-(k-a-b)))

# Optimized solution
a, b, c, k = list(map(int, input().split()))
ans = 0
if k < a:
    ans = k
elif k <= a+b:
    ans = a
else:
    ans = a - (k-a-b)
print(ans)
# Original solution
a,b,c,k = list(map(int,input().split()))
if a + b >= k:
    ans = min(a,k)
else:
    ans = a - (k-a-b)
print(ans)

# Optimized solution
a,b,c,k = list(map(int,input().split()))
print((min(a,k)-max(k-a-b,0)))
# Original solution
a,b,c,k = list(map(int, input().split()))
cnt = 0
import sys
if a==k:
    cnt = a
    print(cnt)
    sys.exit(0)
for i in range(k):
    if a>0:
        cnt += 1
        a -= 1
    elif b>0:
        cnt += 0
        b -= 1
    elif c>0:
        cnt -= 1
        c -= 1

print(cnt)

# Optimized solution
a,b,c,k = list(map(int, input().split()))
cnt = 0

if a>=k:
    cnt += k
else:
    k -= a
    cnt += a
    if k>0:
        k -= b
        if k>0:
            cnt -= k

print(cnt)
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

a, b, c, k = list(map(int, readline().split()))
if a + b >= k:
    print((min(a, k)))
else:
    print((a - (k - a - b)))


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

a, b, c, k = list(map(int, read().split()))
print((min(a, k) if a + b >= k else 2 * a + b - k))
# Original solution
a, b, c, d = list(map(int, input().split()))
ans = 0

if a == d:
    print(a)
else:
    for _ in range(a):
        ans += 1
    
    if d-a != 0:
        for _ in range(d-a):
            ans += 0

    if d-a-b != 0:
        for _ in range(d-a-b):
            ans += -1
    
    print(ans)

# Optimized solution
a, b, c, d = list(map(int, input().split()))

if a >= d:
    print((d*1))
elif a < d:
    if d-(a+b) <= 0:
        print(a)
    elif d-(a+b+c) <= 0:
        print((a + (d-a-b)*(-1)))
# Original solution
a, b, c, k = list(map(int, input().split()))
r = 0
if a < k:
	r += a
	if a + b + c < k:
		r -= c
	elif a + b < k:
		r -= k - a - b
else:
	r = k
print(r)


# Optimized solution
a, b, c, k = list(map(int, input().split()))
pa = min([a, k])
k -= pa
pb = min([b, k])
k -= pb
pc = min([c, k])
print((pa - pc))
# Original solution
A,B,C,K = list(map(int,input().split()))

ans = min(K,A)
K -= A
if K > 0:
    K -= B
if K > 0:
    ans -= K
print(ans)


# Optimized solution
A,B,C,K = list(map(int,input().split()))
print((min(K,A) - max(0,K-A-B)))
# Original solution
a = input().rstrip().split(' ')
A,B,C,K = int(a[0]),int(a[1]),int(a[2]),int(a[3])
s = 0

if K - A <= 0:
    print(K)
else:
    K -= A
    if K - B <= 0:
        print(A)
    else:
        K -= B
        print((A-K))

# Optimized solution
A,B,C,K = list(map(int,input().split()))

xa = min(K,A)
K -= xa
xb = min(K,B)
K -= xb


ans = xa - K
print(ans)
# Original solution
a, b, c, k = list(map(int, input().split()))

if k < a:
    print(k)
    exit()

k -= (a+b)
if k <= 0:
    print(a)
    exit()
else:
    ans = a

for i in range(k):
    ans -= 1
print(ans)

# Optimized solution
a, b, c, k = list(map(int, input().split()))

if k < a:
    print(k)
    exit()

k -= (a+b)
if k <= 0:
    print(a)
    exit()
else:
    ans = a

print((ans - k))
# Original solution
a,b,c,k =list(map(int, input().split()))
ans = 0
while k > 0:
    if a >= k:
        ans += k
        break
    else:
        ans += a
        k -= a
    if b >= k:
        break
    else:
        k -= b
    ans -= k
    break

print(ans)

# Optimized solution
a,b,c,k =list(map(int, input().split()))

ans = 0
x = min(k , a)
ans += x
k2 = k - x
x2 = min(k2, b)
k3 = k2 - x2
x3 = min(k3, c)
ans -= k3

print(ans)
# Original solution
A,B,C,K=list(map(int,input().split()))
ans=0
if K >= A:
    ans+=A
    K-=A
    A=0
while K > 0 and A > 0:
    ans+=1
    K-=1
    A-=1
if K >= B:
    K-=B
    B=0
while K > 0 and B > 0:
    K-=1
    B-=1
if K >= C:
    ans-=C
    K-=C
    C=0
while K > 0 and C > 0:
    ans-=1
    K-=1
    C-=1
print(ans)

# Optimized solution
A,B,C,K=list(map(int,input().split()))
ans=0
if K >= A:
    ans+=A
    K-=A
    A=0
else:
    ans=K*1
    K=0
if K >= B:
    K-=B
    B=0
else:
    K=0
if K >= C:
    ans-=C
    K-=C
    C=0
else:
    ans=ans+K*(-1)
    C=0
print(ans)
# Original solution
import sys
a, b, c, k = list(map(int, input().split()))
max = 0
if a < k:
    for i in range(a, -1, -1):
        for j in range(b, -1, -1):
            for l in range(c+1):
                if i+j+l == k:
                    print((i-l))
                    sys.exit()
else:
    for i in range(k, -1, -1):
        for j in range(b, -1, -1):
            for l in range(c+1):
                if i+j+l == k:
                    print((i-l))
                    sys.exit()

# Optimized solution
import sys
a, b, c, k = list(map(int, input().split()))
if a >= k:
    print(k)
    sys.exit()
else:
    if b >= k-a:
        print(a)
        sys.exit()
    else:
        print((a-(k-a-b)))
        sys.exit()
# Original solution
a,b,c,k = list(map(int,input().split()))

ans = 0

if k <= a:
    print(k)
elif a<k<=b+a:
    print(a)
elif b+a<k<a+b+c:
    print((a-(k-a-b)))
else:
    print((a-c))



# Optimized solution
import sys
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9 + 7
input = sys.stdin.readline

def inp():
    return int(eval(input()))
def inpl():
    return list(map(int,input().split()))
def inpm():
    return list(map(int,input().split()))

a,b,c,k = inpm()

print((min(min(k,a),2*a+b-k)))
# Original solution
import sys
A, B, C, K = list(map(int, input().split()))
N = 0
if K - A < 0:
    print(K)
    sys.exit()
else:
    N = A
    K = K - A
if K - B < 0:
    print(N)
    sys.exit()
else:
    K = K - B
print((N - K))

# Optimized solution
import sys
A, B, C, K = list(map(int, input().split()))
c = 0
if K <= A:
    c += K
    print(c)
    sys.exit()
else:
    c += A
    K -= A
if K <= B:
    print(c)
    sys.exit()
else:
    K -= B
    c -= K
    print(c)
# Original solution
import sys 
a,b,c,k = list(map(int,input().split()))
max=0
if(a==k):
    print(a)
elif(c==0):
    print((a%k))
    
else:
    while(a>0 and k>0):
        max+=1
        a-=1
        k-=1
    if(k<=0):
        print(max)
    else:
        while(b>0 and k>0):
            b-=1
            k-=1
        if(k<=0):
            print(max)
        else:
            while(c>0 and k>0):
                max-=1
                c-=1
                k-=1
            if(k<=0):
                print(max)
        



# Optimized solution
import sys 
a,b,c,k = list(map(int,input().split()))
max=0
if(a==k):
    print(a)
elif(a>k):
    print(k)
else:
    if(a<k):
        max+=a
        k=k-a
    if(b>k):
        print(max)
    else:
        k=k-b
        if(k<=0):
            print(max)
        else:
            if(c>=k):
                max-=k
                print(max)
            elif(c<k):
                max-=c
                print(max)
# Original solution
a, b, c, k = list(map(int, input().split()))

suma = 0

for i in range(k):
    if a == 0 and b == 0:
        suma = suma - k
        break
    elif a == 0 and c == 0:
        break
    elif b == 0 and c == 0:
        suma = suma + k
        break
    if a > 0:
        a = a - 1
        k = k - 1
        suma += 1
    elif b > 0:
        b = b - 1
        k = k - 1
    elif c > 0:
        c = c - 1
        k = k - 1
        suma -= 1
print(suma)

# Optimized solution
a, b, c, k = list(map(int, input().split()))

suma = 0


if k > a:
    if k > a + b:
        if k == a + b + c:
            print((a - c))
        else:
            k = k - (a + b)
            print((a-k))
    else:
        print(a)
else:
    print(k)
# Original solution
a, b, c, k = list(map(int, input().split()))
ans = 0
if a >= k:
    ans = k
    k = 0
else:
    k -= a
    ans = a
while k > 0:
    k -= 1
    if b > 0:
        b -= 1
    else:
        c -= 1
        ans -= 1
print(ans)

# Optimized solution
a, b, c, k = list(map(int, input().split()))
ans = 0
if a >= k:
    ans = k
    k = 0
else:
    k -= a
    ans = a
k -= b
if k > 0:
    ans -= k
print(ans)
# Original solution
A,B,C,K = list(map(int, input().split()))
num = 0
ans = 0

if(A == K):
  print(A)
  exit()
if(A+B == K):
  print(A)
  exit()

for i in range(0,K):
  if(i < A):
    ans += 1
  elif(i < A+B):
    ans += 0
  else:
    ans -= 1
print(ans)

# Optimized solution
A,B,C,K = list(map(int, input().split()))
num = 0
ans = 0

if(A >= K):
  print(K)
elif(A < K and K <=(A+B)):
  print(A)
else:
  ans = A - (K-(A+B))
  print(ans)
# Original solution
a,b,c,k=list(map(int,input().split(" ")))
if a >= k:
  print(k)
elif b >= k-a:
  print(a)
else:
  print((2*a+b-k))

# Optimized solution
a,b,c,k=list(map(int,input().split()))

if a >= k:
    print(k)
    
elif a+b < k:
    print((a-(k-a-b)))
    
else:
    print(a)
# Original solution
#!/usr/bin/env python3
import sys


def solve(A: int, B: int, C: int, K: int):
    sum = 0
    l = K - A
    if l <= 0:
        sum += K
    else:
        sum += A
    l -= B
    if l > 0:
        sum -= l
    print(sum)
        
        
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    solve(A, B, C, K)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    A, B, C, K = list(map(int, input().split()))
    score = min(K, A) * 1
    K -= A + B
    if K > 0:
        score -= K
    print(score)
if __name__ == '__main__':
    main()
# Original solution
A,B,C,K = list(map(int, input().split()))


ans=0

if K <= A:
  ans=K

elif K <= A + B :
  ans=A
else:
  ans= A - (K - A - B)
  
print(ans)

# Optimized solution
A,B,C,K = list(map(int, input().split()))

if K <= A:print(K)
elif K <= A + B :print(A)
else:print((A*2 + B - K))
# Original solution
a,b,c,k = list(map(int,input().split()))

xa = min(a,k)
k -= xa
xb = min(b, k)
k -= xb
xc = k
print((xa-xc))


# Optimized solution
a,b,c,k = list(map(int,input().split()))
if a+b >= k:
    print((min(a,k)))
else:
    print((a-(k-a-b)))
# Original solution
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

A, B, C, K = list(map(int, read().split()))

ans = 0


ans += min(A, K)
K -= A
if K <= 0:
    print(ans)
    sys.exit()

K -= B
if K <= 0:
    print(ans)
    sys.exit()

ans -= min(C, K)


print(ans)


# Optimized solution
import sys
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

A, B, C, K = list(map(int, read().split()))

ans = 0
a = min(A, K)
K -= a
b = min(B, K)
K -= b
c = K

print((a - c))
# Original solution
A, B, C, K = list(map(int, input().split()))
ct = 0
if B == 0:
    if C == 0:
        print(A)
        exit()

for i in range(K):
    if A > 0:
        ct += 1
        A -= 1
    else:
        if B > 0:
            B -= 1
        else:
            ct -= 1
print(ct)


# Optimized solution
A, B, C, K = list(map(int, input().split()))
if (K - A) <= 0:
    print(K)
else:
    if (K - A - B) <= 0:
        print(A)
    else:
        c = (K - A - B)
        print((A - c))
# Original solution
def main():
    n = int(eval(input()))
    a = [list(map(int, input().split())) for i in range(n)]

    dist = [[a[i][j] for j in range(n)] for i in range(n)]
    total = sum([sum(a[i]) for i in range(n)])
    for k in range(n):
        for i in range(n):
            if k==i:
                continue
            for j in range(n):
                if i == j or k ==j:
                    continue
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    print((-1))
                    exit()
                if dist[i][j] == dist[i][k] + dist[k][j]:
                    total -= dist[i][j]
    print((total//2))
    

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    n = int(eval(input()))
    a = [list(map(int, input().split())) for i in range(n)]

    total = sum([sum(a[i]) for i in range(n)])
    for k in range(n):
        for i in range(n):
            if k == i:
                continue
            for j in range(n):
                if i == j or k == j:
                    continue
                if a[i][j] > a[i][k] + a[k][j]:
                    print((-1))
                    exit()
    for i in range(n):
        for j in range(n):
            for k in range(n):
                if i == k or j == k:
                    continue
                if a[i][j] == a[i][k] + a[k][j]:
                    total -= a[i][j]
                    break

    print((total//2))


if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))
A = [list(map(int, input().split())) for i in range(N)]

INF = 10 ** 9 + 1
for i in range(N):
    A[i][i] = INF

ans = 0
for i in range(N):
    Ai = A[i]
    for j in range(i):
        d = min(list(map(sum, list(zip(Ai, A[j])))))

        if A[i][j] > d:
            print('-1')
            exit()
        elif A[i][j] < d:
            ans += A[i][j]

print(ans)


# Optimized solution
N = int(eval(input()))
A = [list(map(int, input().split())) for i in range(N)]

# Warshall-Floyd法
dist = [list(A[i]) for i in range(N)]
via = [[None for i in range(N)] for j in range(N)]
for k in range(N):
    for i in range(N):
        for j in range(N):
            d = dist[i][k] + dist[k][j]
            if dist[i][j] >= d:
                dist[i][j] = d
                if i != k and j != k:
                    via[i][j] = k

ans = 0
for i in range(N):
    for j in range(i):
        if dist[i][j] < A[i][j]:
            ans = -1
            break

        if via[i][j] == None:
            ans += dist[i][j]

    if ans == -1:
        break

print(ans)
# Original solution
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *

def read():
  return int(eval(input()))
 
def reads():
  return [int(x) for x in input().split()]

N = read()
A = []
for _ in range(N):
  A.append(reads())

necc = [[True] * N for _ in range(N)]

for i, j, k in combinations(list(range(N)), 3):
  a, b, c = sorted((
    (A[i][j], i, j),
    (A[i][k], i, k),
    (A[j][k], j, k)
  ))
  if a[0] + b[0] < c[0]:
    print((-1)); exit()
  elif a[0] + b[0] == c[0]:
    necc[c[1]][c[2]] = False

ans = sum(A[i][j] for i, j in combinations(list(range(N)), 2) if necc[i][j])
print(ans)

# Optimized solution
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *

def read():
  return int(eval(input()))
 
def reads():
  return [int(x) for x in input().split()]

N = read()
A = []
for _ in range(N):
  A.append(reads())

necc = [[True] * N for _ in range(N)]

def fail():
  print((-1)); exit()

for i, j, k in combinations(list(range(N)), 3):
  if A[i][j] + A[j][k] < A[i][k] or A[i][j] + A[i][k] < A[j][k] or A[i][k] + A[j][k] < A[i][j]:
    fail()
  if A[i][j] + A[j][k] == A[i][k]:
    necc[i][k] = False
  if A[i][j] + A[i][k] == A[j][k]:
    necc[j][k] = False
  if A[i][k] + A[j][k] == A[i][j]:
    necc[i][j] = False

ans = sum(A[i][j] for i, j in combinations(list(range(N)), 2) if necc[i][j])
print(ans)
# Original solution

def solve():
  n  = int(eval(input()))
  a = []
  for i in range(n):
    tp = list(int(i) for i in input().split())
    a.append(tp)
  a2 = []
  #warshall
  for k in range(n):
    for i in range(n):
      for j in range(n):
        if not k in [i,j]:
         if a[i][j] > a[i][k] + a[k][j]:
          #距離短縮可能
           print((-1))
           exit()
         elif a[i][j] ==  a[i][k] + a[k][j]:
          a2.append((i,j))
  for i,j in a2:
    a[i][j] = 0
  ans = 0
  for i in range(n):
    for j in range(n):
      if i < j:
        ans += a[i][j]
  print(ans)
solve()

# Optimized solution
import sys
input = sys.stdin.readline
def solve():
  n  = int(eval(input()))
  a = [list(int(i) for i in input().split()) for _ in range(n)]
  a2 = []
  #warshall
  for k in range(n):
    for i in range(n):
      for j in range(i+1,n):
        if not k in [i,j]:
         if a[i][j] > a[i][k] + a[k][j]:
          #距離短縮可能
           print((-1))
           exit()
         elif a[i][j] ==  a[i][k] + a[k][j]:
          a2.append((i,j))
  for i,j in a2:
    a[i][j] = 0
  ans = 0
  for i in range(n):
    for j in range(i+1,n):
        ans += a[i][j]
  print(ans)
solve()
# Original solution
while True:
  n = int(eval(input()))
  if not n:
    break
  ss = [s for s in input()[::-1]]
  for i in range(n):
    new = []
    app = new.append
    last = ss.pop()
    count = 1
    while ss:
      a = ss.pop()
      if a == last:
        count += 1
      else:
        app(str(count))
        app(last)
        last = a
        count = 1
    app(str(count))
    app(last)
    new = "".join(new)
    ss = [s for s in new[::-1]]
#    print("".join(new))
  print(("".join(new)))


# Optimized solution
def main():
  while True:
    n = int(eval(input()))
    if not n:
      break
    ss = [s for s in input()[::-1]]
    for i in range(n):
      new = []
      app = new.append
      last = ss.pop()
      count = 1
      while ss:
        a = ss.pop()
        if a == last:
          count += 1
        else:
          app(str(count))
          app(last)
          last = a
          count = 1
      app(str(count))
      app(last)
      new = "".join(new)
      ss = [s for s in new[::-1]]
  #    print("".join(new))
    print(("".join(new)))

main()
# Original solution
def f(a):
	ans = ""
	s = a[0]
	c = 1
	for i in range(1,len(a)):
		if a[i] == s:
			c += 1
		else:
			ans += str(c) + s
			s = a[i]
			c = 1
	ans += str(c) + s
	return ans
	
while 1:
	n = eval(input())
	if n == 0: break
	a = input()
	for i in range(n):
		a = f(a)
	print(a)

# Optimized solution
def f(a,n):
	for j in range(n):
		aa = ""
		s = a[0]
		c = 1
		for i in range(1,len(a)):
			if a[i] == s:
				c += 1
			else:
				aa += str(c) + s
				s = a[i]
				c = 1
		a = aa + str(c) + s
	return a
	
while 1:
	n = eval(input())
	if n == 0: break
	a = input()
	print(f(a,n))
# Original solution
import itertools
import math

N = int(eval(input()))
P = int(''.join(input().split()))
Q = int(''.join(input().split()))

N_list = []
for i in itertools.permutations(list(range(1,N+1))):
    N_list.append(int(''.join([str(x) for x in i])))

pointer1 = 0
pointer2 = 0

for i,x in enumerate(N_list):
  if x == P:
    pointer1 = i
  if x == Q:
    pointer2 = i
    
print((abs(pointer1 - pointer2)))

# Optimized solution
import itertools

n = int(eval(input()))
p = [int(x) for x in input().split()]
q = [int(x) for x in input().split()]

a,b = 0,0
for i,x in enumerate(itertools.permutations(list(range(1,n+1)))):

    if list(x) == p:
        a = i
    if list(x) == q:
        b = i
        
print((abs(a-b)))
# Original solution
import itertools
n=int(eval(input()))
p=list(map(int,input().split()))
q=list(map(int,input().split()))
d=[]
k=0
s=0
a=0
b=0
nums = list(range(1,n+1))
d= [ list(balls) for balls in itertools.permutations(nums, n)]
for i in d:
  k+=1
  s+=1
  if i==p:
    a=s
  if i==q:
    b=k
print((abs(a-b)))
    

# Optimized solution
import itertools
n=int(eval(input()))
p=list(map(int,input().split()))
q=list(map(int,input().split()))
nums = list(range(1,n+1))
d= [ list(i) for i in itertools.permutations(nums)]
a=0
b=0
for i in range(len(d)):
  if p==d[i]:
    a=i
  if q==d[i]:
    b=i
print((abs(a-b)))
# Original solution
# -*- coding: utf-8 -*-
from itertools import permutations as perm

N = int(eval(input()))
P = tuple(list(map(int, input().split())))
Q = tuple(list(map(int, input().split())))

a = 0
b = 0

for Pi in sorted(perm(P)):
    if P != Pi:
        a += 1
    if P == Pi:
        a += 1
        break
    
for Qi in sorted(perm(Q)):
    if Q != Qi:
        b += 1
    if Q == Qi:
        b += 1
        break

print((abs(a-b)))

# Optimized solution
# -*- coding: utf-8 -*-
from itertools import permutations as p
N = int(eval(input()))
A = [i for i in range(1, N+1)]
P = tuple(list(map(int, input().split())))
Q = tuple(list(map(int, input().split())))
cntp = 1
cntq = 1
for i in sorted(p(A)):
    if i != P:
        cntp += 1
    if i == P:
        cntp += 1
        break
for i in sorted(p(A)):
    if i != Q:
        cntq += 1
    if i == Q:
        cntq += 1
        break
print((abs(cntp-cntq)))