peterverebics/CodeNet_Python_428 huggingface dataset:
# Judge status: Accepted
k,n = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
from collections import deque
d = deque()
d.append(k-l[n-1]+l[0])
for i in range(n-1):
  d.append(l[i+1]-l[i])
print(k-max(d))
# Judge status: Wrong Answer
N = int(input())
K = int(input())

X = int(input())
Y = int(input())

print(K*X + max(0,(N-K))*Y)
# Judge status: Wrong Answer
import numpy as np
N, K = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

for k in range(K):
    B = np.zeros(N)
    for i in range(N):
        B[int(max(0, i - A[i])):int(min(i + A[i], N - 1))] += 1
    A = B

for a in A:
    print(int(a), end=" ")
print()
# Judge status: Accepted
#参考：http://at274.hatenablog.com/entry/2020/01/24/060000

N = int(input())
# cnt[i][j] := 先頭がi, 末尾がjで始まるような数
# 個数を格納できる行列の準備
cnt = [[0] * 10 for i in range(10)]
# Nまでカウントする（11>(1,1) 10231>(1,1))
for n in range(1, N + 1):
    cnt[int(str(n)[0])][int(str(n)[-1])] += 1

ans = 0
for i in range(10):
    for j in range(10):
        ans += cnt[i][j] * cnt[j][i]

print(ans)
# Judge status: Accepted
D, T, S = list(map(int, input().split()))
if T*S >= D:
  print('Yes')
else: 
  print('No')
# Judge status: Accepted
# -*- coding: utf-8 -*-
N = int(input())
A_list = list(map(int, input().split()))

# lcm & gcd not math
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

from functools import reduce
def lcm_multi(num_list):
    return reduce(lcm, num_list, 1)

div_num = 10 ** 9 + 7
lcm_num = lcm_multi(tuple(A_list))
ans = 0
for i in range(N):
    ans += (lcm_num // A_list[i])
print(ans % div_num)
# Judge status: Accepted
x  = int(input())
t = 100
ans = 0
while t<x:
  t += t//100
  ans +=1

print(ans)
# Judge status: Accepted
N, Q=map(int, input().split())
S = input()
t = [0]*(N + 1)
for i in range(N):
    t[i+1] = t[i] + (1 if S[i:i+2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1]-t[l-1])
# Judge status: Accepted
N, M = map(int, input().split())

lst = [list(map(int, input().split())) for _ in range(M)]
lst.sort(key=lambda x: x[0])

n = 10 ** N


for i in range(n):
    moji = str(i)
    k = len(moji)

    if k != N:
        continue

    flag = True

    for s, c in lst:
        if s > k or moji[s - 1] != str(c):
            flag = False
            break

    if flag:
        print(str(i))
        exit()

print('-1')
# Judge status: Wrong Answer
import math
n,k =map(int,input().split())
h= list(map(int,input().split()))
dp=[0]*n
dp[1]=abs(h[1]-h[0])
for i in range(2,n):
    mindp=dp[i-1]+ abs(h[i]- h[i-1])
    for j in range(max(i-k,0),i-2):
        mindp= min(dp[j]+ abs(h[i]- h[j]), mindp)
    dp[i]=mindp
print(dp[n-1])
# Judge status: Accepted
n = int(input())
c = input()

cnt = 0
r = c.count('R')

r_s = c[:r].count('R')

print(r - r_s)
# Judge status: Accepted
h, w = map(int, input().split())
 
if h == 1 or w == 1:
	print(1)
    
elif h % 2 == 1 and w % 2 == 1:
	print((h - 1) * w // 2 + (w+1) // 2 )
else:
	print(h*w//2)
# Judge status: Wrong Answer

if __name__ == '__main__':
    n = int(input())
    r = lambda n: [''] if n <= 0 else [e + j for e in r(n - 1) for j in [chr(ord('a') + e) for e in range(n)]]
    a = r(n)
    for e in a:
        print(e)
# Judge status: Accepted
n = int(input())
xy = []
ans = 0
for _ in range(n):
    xy.append([int(i) for i in input().split()])
for i in range(n-1):
    for j in range(i+1, n):
        ans += ((xy[j][0] - xy[i][0])**2 + (xy[j][1] - xy[i][1])**2)**0.5
print(ans*2/n)
# Judge status: Accepted
import bisect

N=int(input())
L=list(map(int,input().split()))
L.sort()
ans=0
for i in range(N):
    for j in range(i+1,N):
        ans+=bisect.bisect_left(L,L[i]+L[j])-j-1
print(ans)
# Judge status: Accepted
from itertools import accumulate
N, K = map(int, input().split())
A = list(map(int, input().split()))
for k in range(min(50, K)):
    B = [0]*N
    for i in range(N):
        B[max(0, i-A[i])] += 1
        r = i+A[i]+1
        if r < N:
            B[r] -= 1
    A = list(accumulate(B))
print(*A)
# Judge status: Accepted
H, N = [int(x) for x in input().split()]
attacks = [int(x) for x in input().split()]
print("Yes" if H - sum(attacks) <= 0 else "No")
# Judge status: Wrong Answer
def framod(x,y,p=10**9+7):
  for i in range(x+1,y+1):
    x = (x*i)%p
  return x
    
#########################################

n,a,b=map(int,input().split())
p=10**9+7

ans=0
for i in [a,b]:
  upper = framod(n-i+1,n,p)
  lower = framod(1,i,p)
  ret = (upper*pow(lower,p-2,p))%p
  ans = (ans+ret)%p
print(pow(2,n,p)-ans-1)
# Judge status: Accepted
a, b=map(int, input().split())
if a<=b:
    ans = [a for i in range(b)]
else:
    ans = [b for i in range(a)]
print(*ans, sep="")
# Judge status: Accepted
N,K=map(int,input().split())
d={}
for i in range(N):
  a,b=map(int,input().split())
  if a not in d:
    d[a]=b
  else:
    d[a]+=b
d=sorted(d.items())
for i in d:
  K-=i[1]
  if K<=0:
    print(i[0])
    break
# Judge status: Wrong Answer
a,b = map(int,input().split())

if a <= 8 and b <= 8:
    print('Yey!')
else:
    print(':(')
# Judge status: Accepted
# coding: utf-8
S = sorted(input())
A = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for i in A:
    if i not in S:
        print(i)
        exit()
else:
    print('None')
# Judge status: Wrong Answer
N = int(input())
N3 = N//3
N5 = N//5
N15 = N//15

S = N * (N+1) / 2
S3 = N3 * (N3+1) / 2 *3
S5 = N5 * (N5+1) / 2 *5
S15 = N15 * (N15+1) / 2 *15

sum = S - (S3 + S5 -S15)
print(sum)
# Judge status: Wrong Answer
S = str(input())
if S[0] == S[1] == S[2]:
    print('No')
else:
    print('Yes')
# Judge status: Accepted
N, M, Q = map(int, input().split())
X=[0]*Q
for i in range(Q):
    X[i] = list(map(int, input().split()))
from itertools import combinations_with_replacement
def solve(N,M,Q,X):
    ans = 0
    for c in combinations_with_replacement(range(1,M+1), N-1):
        A = [1]+list(c)
        score = 0
        for x in X:
            if A[x[1]-1]-A[x[0]-1]==x[2]:
                score += x[3]
        ans = max(ans,score)
    return ans
print(solve(N,M,Q,X))
# Judge status: Wrong Answer
n,k=map(int,input().split());h=sorted([int(input()) for _ in range(n)])
a,b=h[:k],h[k-1:];print(min(max(a)-min(a),max(b)-min(b)))
# Judge status: Wrong Answer
import numpy as np
import numpy

N,K =(float(x) for x in input().split())
list1 = np.array([float(s) for s in input().split()])
print(list1)

list2=np.sort(list1)

i=0
sum=0
while i<N-K:
  sum=sum+list2[i]
  i=i+1

print(sum)
# Judge status: Accepted
H = int(input())
n = 0
while(H != 1):
    H = H//2
    n += 1
#print(n)
print(pow(2, n+1)-1)
# Judge status: Wrong Answer
# coding: utf-8
# Your code here!
s=input()
K=int(input())

ans=[]
#122
for item in s[:len(s)-1]:
    cost=(123-ord(item))
    if K>=cost:
        K-=cost
        ans.append("a")
    else:
        ans.append(item)

K%=26
last=ord(s[-1])
if last+K>122:
    ans.append(chr(last+K-26))
else:
    ans.append(chr(last+K))

print("".join(ans))
# Judge status: Wrong Answer
S = input()
if all([x in ['R', 'U', 'D'] for x in S[::2]]):
    print('Yes')
elif all([x in ['L', 'U', 'D'] for x in S[1::2]]):
    print('Yes')
else:
    print('No')
# Judge status: Wrong Answer
N,M = map(int,input().split())
A = [int(input()) for _ in range(M)]

dp = [0]*(N+1)
dp[0] = 1
dp[1] = 1

for i in range(2,N+1):
  if i not in A:
    dp[i] = (dp[i-1] + dp[i-2])%1000000007
  else:
    dp[i] = 0

print(dp[N])
# Judge status: Wrong Answer
N=int(input())
d=[int(s) for s in input().split(" ")]

d.sort()

if (N//2)%2==0:
    print(0)
else:
    K=d[(N//2)]-d[(N//2)-1]
    if d[N//2]==d[(N//2)-2]:
        K=0
    print(K)
# Judge status: Accepted
A,B,K = list(map(int,input().split()))
if K <= A:
  print('{} {}'.format(A-K,B))
if A < K <= A+B:
  print('{} {}'.format(0,A+B-K))
if A+B < K:
  print('{} {}'.format(0,0))
# Judge status: Accepted
# coding = SJIS

s = str(input())
if len(set(s)) == len(s):
    print("yes")
else:
    print("no")
# Judge status: Accepted
a = list(input())
a.sort()
if a[0] == "a" and a[1] == "b" and a[2] == "c":
  print("Yes")
else:
  print("No")
# Judge status: Wrong Answer
from collections import Counter
from math import sqrt
from bisect import bisect_left
n = int(input())
a = Counter(map(int, input().split()))
nums = sorted(list(a.keys()))
ans = n
for num, count in a.items():
    if count > 1:
        ans -= count
        continue
    index = bisect_left(nums, sqrt(num))
    search_range = nums[:index + 1]
    for i in search_range:
        if num % i == 0 and num != i:
            ans -= count
            break
print(ans)
# Judge status: Accepted
h, n = map(int,input().split())
ans = int(h // n)
if h % n:
    ans += 1
print(ans)
# Judge status: Wrong Answer
N = int(input())
M0 = M1 = 0
m0 = m1 = float('inf')

for _ in range(N):
    x, y = map(int, input().split())
    M0 = max(M0, x - y)
    m0 = min(m0, x - y)
    M1 = max(M1, x + y)
    m1 = min(m1, x + y)

print(max(M0 - m0, M1 - m1))
# Judge status: Accepted
def resolve():
	n, m, x, y = map(int, input().split())
	xp = list(map(int, input().split()))
	yp = list(map(int, input().split()))
	for i in range(-99, y+1):
		ok = True
		if i <= x or i > y:
			continue
		for cx in xp:
			if cx >= i:
				ok = False
				break
		for cy in yp:
			if cy < i:
				ok = False
				break
		if ok:
			print('No War')
			return
	print('War')
resolve()
# Judge status: Accepted
n, m = map(int, input().split())
l = []
r = []
for i in range(m):
    L, R = map(int, input().split())
    l += [L]
    r += [R]

l_max = max(l)
r_min = min(r)

print(max(r_min - l_max + 1, 0))
# Judge status: Accepted
print((2*int(input())-2)//11+1)
# Judge status: Accepted
a,b,c=map(int,input().split('/'))
if a>2019 or(a==2019 and b>4):print('TBD')
else:print('Heisei')
# Judge status: Accepted
n = int(input())
a = [list(map(int,input().split())) for i in range(2)]
ans = 0
for i in range(n):
  k = sum(a[1][i:])+sum(a[0][0:i+1])
  if k > ans:
    ans = k
print(ans)
# Judge status: Accepted
n = list(input())

for i in n:
    if n.count(i)%2 != 0:
        print("No")
        exit()
print("Yes")
# Judge status: Wrong Answer
N=int(input())
array=[]
for i in range(N):
    if i%3!=0 and i%5!=0:
        array.append(i)

value=sum(array)
print(value)
# Judge status: Wrong Answer
import math
a,b,c,d=map(int,input().split())
def gcd(a,b):
    if b>a:
        b,a=a,b
    if b==0:
        return a
    else:
        return gcd(b,a%b)


def warikiri(n,c,d):
    koubai=(c*d)//gcd(c,d)
    return n-math.floor(n/d)-math.floor(n/c)+math.floor(n/koubai)
print(warikiri(b,c,d)-warikiri(a-1,c,d))
# Judge status: Wrong Answer
x, a, b = map(int, input().split())
c = 1 if a < b else -1
print("A" if x < (a + b) * c / 2 else "B")
# Judge status: Wrong Answer
from sys import stdin, setrecursionlimit
from collections import defaultdict


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = [int(input()) for _ in range(n)]
    ans = 0
    min = float('inf')
    dic = defaultdict(int)
    for ai in a:
        dic[ai] += 1
        if ai <= min:
            ans += 1
            min = ai
        elif dic[ai] > ans:
            ans += 1
    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
# Judge status: Accepted
N, M = map(int, input().split())
H = list(map(int, input().split()))

h = []
for i in H:
    h.append(i)
for i in range(M):
    a, b = map(int, input().split())
    if h[a-1] <= H[b-1] and h[a-1] != -1:
        h[a-1] = -1
    if h[b-1] <= H[a-1] and h[b-1] != -1:
        h[b-1] = -1

ans = N
for i in h:
    if i == -1:
        ans -= 1

print(ans)
# Judge status: Accepted
n,k = map(int, input().split())
a = list(map(int,input().split()))
 
list_a = sorted(a, reverse = True)
 
if k >= n:
    print(0)
else:
    attack = sum(list_a[k:])
    print(attack)
# Judge status: Accepted
n=int(input())
A=list(map(int,input().split()))
if 0 in A:
  print(0)
  exit()
a=1
f=0
for i in range(n):
  a=a*A[i]
  if a>10**18:
    f=1
    break
if f==0:
  print(a)
else:
  print(-1)
# Judge status: Accepted
n = int(input())
#k, x = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))
import bisect
al.sort();bl.sort();cl.sort()
ans=0
for bi in bl:
    c1 = bisect.bisect_right(al, bi-1)
    c2 = n-bisect.bisect_left(cl, bi+1)
    ans += c1*c2
print(ans)
# Judge status: Accepted
n, m = map(int, input().split())
S = [list(map(int, input().split())) for _ in range(m)]
for i in range(10 ** n):
	flag = True
	st = str(i)
	if len(st) != n:
		continue
	for s, c in S:
		if int(st[s - 1]) != c:
			flag = False
			break
	if flag:
		print(int(st))
		break
else:
	print(-1)
# Judge status: Accepted
a,b=map(int,input().split())
if a<6:
  print(0)
elif a<13:
  print(b//2)
else:
  print(b)
# Judge status: Wrong Answer
import sys
stdin=sys.stdin

ip=lambda: int(sp())

lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()

a,b,c=lp()
if a+b+c>=22:
  print('burst')
  
else:
  print('win')
# Judge status: Wrong Answer

a = int(input())
b = int(input())
h = int(input())

print((a+b)*h/2)
# Judge status: Accepted
import sys

A = list(map(int, sys.stdin.readline().strip().split()))
print(A.index(0) + 1)
# Judge status: Accepted
import bisect

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort()
B.sort()
C.sort()

ans = 0

for b in B:
    n_lower_B = bisect.bisect_left(A, b)
    n_higher_B = N - bisect.bisect_right(C, b)
    ans += n_lower_B * n_higher_B

print(ans)
# Judge status: Accepted
N = []

for i in range(6):
    N.append(int(input()))

ans = "Yay!"
for i in range(5):
    for j in range(i+1,5):
        dist = abs(N[j] - N[i])
        
        if dist > N[5]:
            ans = ":("

print(ans)

##5m
# Judge status: Wrong Answer
n=int(input())
l=list(map(int,input().split()))
mx=l[0]
an=0
for i in range(1,n):
  if l[i]<mx:
    an+=mx-l[i]
  else:
    l[i]=mx
print(an)
# Judge status: Wrong Answer
import math

r=(input("半径を入力してください"))
r1=int(r)
area1 = math.pi*r1**2
ratio = area1/math.pi
print(ratio)
# Judge status: Accepted
from itertools import *
N,M,Q = map(int,input().split())
abcd = [list(map(int,input().split())) for q in range(Q)]
print(max(sum(d for a,b,c,d in abcd if i[b-1]-i[a-1]==c) for i in combinations_with_replacement(range(M),N)))
# Judge status: Accepted
n = int(input())
if n<3:
    print(0)
    exit()
mod=10**9+7

memo=[0]*(n+1)
memo[0]=1
for i in range(3,n+1):
    memo[i]=sum(memo[:i-2])%mod
print(memo[n])
# Judge status: Accepted
n,a,b = map(int, input().split())
if (b-a)%2==0:
    ans = (b-a)//2
else:
    ans = min(n-a, b-1)
    ans = min(ans, min(a-1, n-b) + (b-a-1)//2 + 1)
    # ans = ((b-a)+2-1)//2 + 1
print(ans)
# Judge status: Accepted
a, b, c, x = [int(input()) for i in range(4)]
print(sum(500*i+100*j+50*k == x for i in range(a+1) for j in range(b+1) for k in range(c+1)))
# Judge status: Accepted
N = int(input())
v = list(map(int, input().split()))

v.sort()
for i in range(N-1):
  v[1] = (v[0]+v[1]) / 2
  v.pop(0)
  
print(sum(v))
# Judge status: Wrong Answer
a,b=map(int,input().split())

if (a>0 and b>0) or (((a+b) % 2)==0):
    print("Positive")
elif a<=0<=b:
    print("Zero")
else:
    print("Negative")
# Judge status: Accepted
P, Q, R = map(int, input().split())

print(min(P+Q,Q+R,R+P))
# Judge status: Accepted
n,m=map(int,input().split())
freq=[0]*n
for _ in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    freq[a]+=1
    freq[b]+=1
for i in range(n):
    print(freq[i])
# Judge status: Wrong Answer
s=input()
t=input()
a=t.count(s)
b=len(t)-a
print(b)
# Judge status: Wrong Answer
s = input()
k = int(input())

asc = [0] * len(s)
for i in range(len(s)):
    asc[i] = ord(s[i]) - ord("a")
i = 0
for i in range(len(s)):
    if 26 - asc[i] <= k or i == len(s) - 1:
        x = 26 - asc[i]
        asc[i] = (asc[i] + min(k, x)) % 26
        k -= x

if k > 0:
    asc[-1] = (asc[-1] + k % 26) % 26
for i in range(len(s)):
    asc[i] += ord("a")
    asc[i] = chr(asc[i])
print("".join(asc))
# Judge status: Wrong Answer
a,b,c= map(int,input().split())
cnt = 0
if a == b and b == c:
    if a%2 == 0 and b%2 == 0 and c%2 == 0:
        print(-1)
        exit()
    else:
        print(0)
while a%2 == 0 and b%2 == 0 and c%2 == 0:
    cnt +=1
    x =(b+c)//2
    y =(c+a)//2
    z =(b+a)//2
    a = x
    b = y
    c = z
print(cnt)
# Judge status: Wrong Answer
n = list(input())

if n[0] == '1':
    print(9 * (len(n) - 1))
elif n.count('9') == len(n) - 1:
    print(9 * (len(n) - 1) + 8)
else:
    print((int(n[0]) - 1 ) + (9 * (len(n) - 1)))
# Judge status: Accepted

l = list(map(int, list(input().strip())))
t = 0
for i in range(len(l)):
    t += l[i]
if t % 9 == 0:
    print("Yes")
else:
    print("No")
# Judge status: Accepted
a,b,c=map(int, input().split())
k=int(input())
num=0

while a >= b:
    b *= 2
    num += 1

while b >= c:
    c *= 2
    num += 1
    
if num > k:
    print("No")
else:
    print("Yes")
# Judge status: Wrong Answer
x = int(input())
if x <= 6:
    print(1)
elif x % 11 <= 6:
    print((x // 11)*2 + 1)
else:
    print((x // 11)*2 + 2)
# Judge status: Wrong Answer
A,B,C = map(int,input().split())
if(A>B and A>C):
    print(str(A*10+B+C))
if(B>A and B>C):
    print(str(B*10+A+C))
else:
    print(str(C*10+A+B))
# Judge status: Accepted
n, T = map(int, input().split())
min = 1001

for i in range(n):
    c, t = map(int,input().split())
    if(t <= T and c < min):
        min = c
        
print(min if min != 1001 else "TLE")
# Judge status: Accepted
import math

a, b = map(int, input().split(' '))

ret = -1
for i in range(1, 1251):
    if math.floor(i * 0.08) == a and math.floor(i * 0.1) == b:
        ret = i
        break
print(ret)
# Judge status: Accepted
O = input()
E = input()
pw = [None] * (len(O) + len(E))
for i in range(len(O)):
    pw[2 * i] = O[i]
for j in range(len(E)):
    pw[2 * j + 1] = E[j]
print(''.join(pw))
# Judge status: Accepted

N = int(input())

ans = ''

while N != 0:
    
    
    if N % 2 !=0:
        N -=1
        ans = '1'+ans
    else:
        ans = '0'+ans
    
    N = N//-2

    
if ans == '':
    ans ='0'
    
    
print(ans)
# Judge status: Accepted
m,n=map(int,input().split())
if m==n :
  print("Yes")
else :
  print("No")
# Judge status: Wrong Answer
n = int(input())
ans = 1
i=1
while i*i<n:
    if n%i==0:
        ans = i
    i+=1

ans += n//ans
ans -= 2
print(ans)
# Judge status: Wrong Answer
N = int(input())
s = [input() for i in range(N)]
M = int(input())
t = [input() for i in range(M)]
a = 0
for i in range(0,N):
    for j in range(0,M):
        if a <= (s.count(s[i]) - t.count(t[j])):
            a = s.count(s[i]) - t.count(t[j])
print(a)
# Judge status: Accepted
S=input()
day=['SUN','MON','TUE','WED','THU','FRI','SAT']
print(7-(day.index(S)))
# Judge status: Accepted
n = int(input())
V = list(map(int, input().split()))
V.sort()
s = V[0]
for i in range(1, n):
    s = (s + V[i])/2
print(s)
# Judge status: Wrong Answer
n,Q=map(int,input().split())
s=input()

a=[0]*(n+1)
for i in range(2,n+1):
    if s[i-2]=='A' and s[i-1]=='C':
        a[i]=a[i-1]+1
    else:
        a[i]=a[i-1]

for i in range(Q):
    l,r=map(int,input().split())
    print(a[r-1]-a[l-1])
# Judge status: Accepted
import sys
input = sys.stdin.readline

A,B = list(map(int,input().split()))
if A>=13:
    print(B)
elif A <= 5:
    print(0)
else:
    print(B//2)
# Judge status: Accepted
X,Y=map(int,input().split())
ans=0
while (X<=Y):
  ans+=1
  X*=2
print(ans)
# Judge status: Wrong Answer
import sys
X = list(sys.stdin.readline().strip())
N = len(X)
NHALF = N // 2

def solve2():
    ns = 0
    nt = 0
    i = 0
    while nt < NHALF:
        if X[i] == 'T':
            nt += 1
            i += 1
        else:
            ns += 1
            i += 1
    return N - 2*ns


print(solve2())
# Judge status: Accepted
import math,numpy, collections

n = int(input())
ans = n
cnt = 0
for i in range(n+1):
    cnt = 0
    x = i
    while x >0:
        cnt += x%6
        x = x//6
    y = n-i
    while y >0:
        cnt += y%9
        y = y//9
    ans = min(ans,cnt)

print(ans)
# Judge status: Accepted
W, a, b = map(int, input().split())

if a + W < b:
    print(b - (a + W))
elif a > b + W:
    print(a - (b + W))
else:
    print(0)
# Judge status: Accepted
# ABC 156: B – Digits
N, K = [int(s) for s in input().split()]

digits = 1

while N >= K:
    digits += 1
    N //= K

print(digits)
# Judge status: Accepted
n = int(input()); a = []; b = []; x = 0
for i in range(n): A, B = map(int, input().split()); a.append(A); b.append(B)
for i in range(n-1, -1, -1): x += b[i]-(a[i]+x-1)%b[i]-1
print(x)
# Judge status: Accepted
k, x = map(int, input().split())
print('Yes' if k * 500 >= x else 'No')
# Judge status: Accepted
st = input()
sn = len(st)

n, s, e, w = 0, 0, 0, 0
for i in range(sn):
  if st[i] == "N":
    n += 1
  if st[i] == "S":
    s += 1
  if st[i] == "E":
    e += 1
  if st[i] == "W":
    w += 1

false = (n > 0 and s == 0) or (s > 0 and n == 0) or (e > 0 and w == 0) or (w > 0 and e == 0)
if false:
  print("No")
else:
  print("Yes")
# Judge status: Accepted
S = input()
T = input()

if T[0:len(S)] == S:
    print('Yes')
else:
    print('No')
# Judge status: Wrong Answer
import numpy as np
n = int(input())

def div_game(n):
    zs = [int(z) for z in np.arange(2,np.sqrt(n)+1) if (n % z == 0)]
    if (zs == []):
        if (n == 1): 
            return 0
        return 1
    ns = []
    for z in zs:
        if (n % z != 0):
            continue
        n /= z
        ns.append(n)
    return len(ns)

print(div_game(n))
# Judge status: Wrong Answer
import numpy as np
n,m = list(map(int, input().split()))
info = [list(map(int, input().split())) for i in range(m)]

info = np.array(info)
pref = {}
for i in range(1,n+1):
    pref[i] = info[info[:,0] == i, 1]

id = {}
for p,years in pref.items():
    id[p] = {}
    for i,y in enumerate(years):
        id[p][y] = str(p).zfill(6) + str(i+1).zfill(6)

for p,y in info:
    print(id[p][y])
# Judge status: Wrong Answer
n, k = map(int, input().split())

cnt = 0
ans = 0

for i in range(n):
    a, b = map(int, input().split())
    cnt += b
    if cnt >= k and ans == 0:
        ans = a

print(ans)
# Judge status: Accepted
S = input()

c = 0
cmax = -1
for s in S:
  if s in ['A', 'C', 'G', 'T']:
    c += 1
  else:
    if c > cmax:
      cmax = c
    c = 0
    
if c > cmax:
  cmax = c
c = 0

print(cmax)
# Judge status: Accepted
# -*- coding:utf-8 -*-
N,x = map(int,input().split())
a = list(map(int,input().split()))

cnt = 0
a.sort()

while x > 0:
    if cnt >= N:
        if x > 0:
            cnt -= 1
            break
        else:
            break
    elif a[0] > x:
        break
    else:
        tmp = a.pop(0)
        x -= tmp
        cnt+=1


print(cnt)
# Judge status: Accepted
n = int(input())

c = [0] * n
f = [0] * n
s = [0] * n

for i in range(n - 1):
    c[i], s[i], f[i] = map(int, input().split())

lapse = [0] * n

for i in range(n):
    for j in range(i + 1, n):

        t = 0
        while s[j - 1] + t * f[j - 1] < lapse[i]:
            t += 1

        lapse[i] = s[j - 1] + t * f[j - 1] + c[j - 1]

    print(lapse[i])
# Judge status: Accepted
a,b,c=map(int,input().split())
d=0
if a==5 or b==5 or c==5:
  if a==7 or b==7 or c==7:
    if a+b+c==17:
      print("YES")
      d=1
if d==0:
  print("NO")
# Judge status: Wrong Answer
cake = 4
donaut = 7
N = int(input())
count = 0

for i in range(0, cake + 1):
	for k in range(0, donaut + 1):
		if N == cake * i + donaut * k:
			count += 1

if count == 0:
	print('No')
else:
	print('Yes')
# Judge status: Accepted
N=int(input())
a=list(map(int,input().split()))

a.sort()
flg=0
if(a[0]==a[1]):
    flg=1
for i in range(1,N-1):
    if(a[i]==a[i-1]):
        flg=1
if flg==1:
    print("NO")
else:
    print("YES")
# Judge status: Wrong Answer
N = input()
a = input()
M = list(map(int, a.split()))

X = set(M)
Y = len(X)

if N == Y:
    print("YES")
else:
    print("NO")
# Judge status: Accepted
N=int(input())
L=list(map(int,input().split()))
A=[float("inf")]*(N+10)
A[0]=0
for i in range(1,N+1):
	A[i]=A[i-1]+L[i-1]
X=float("inf")
for i in range(len(A)):
	X=min(X,abs(A[N]-A[i]-A[i]))
print(X)