Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

"""
最大公約数をもっとも落としている数Xを削る（＝見ないことにする）、という発想に置き換える。
方針１：
Xを特定する。
どうやって特定する？
各Aを素因数分解する。このとき、他の

方針２：
約数をカウントする。すべてのAの約数をカウントして総和する。
このとき、カウントがN個になる約数は初期状態での公約数である。
一方で、カウントがN-1個の約数は、その約数をもたない数が一つだけ存在するということなので、
そのような数Aを削ったときに、公約数になる。
したがって、カウントがN-1個以上の約数のうち最大のものが、数Xを削ったときの最大公約数である。
問題はどうやって約数をカウントするかだ。愚直にやるとO(N√max(A))かかるため間に合わない。
ただ、約数の数自体はせいぜい1000個くらいのはず。いったん素因数分解してから、各素因数を何個累乗させるかで考えればよいか。
"""

from collections import Counter

N = int(input())
A = list(map(int,input().split()))
count = Counter([])

for a in A:
    tmp = []
    d = 2
    while d*d <= a:
        while a%d == 0:
            tmp.append(d)
            a //= d
        d += 1
    if a != 1:
        tmp.append(a)
    divides = [1]
    for k,v in Counter(tmp).items():
        M = len(divides)
        for i in range(1,v+1):
            for j in range(M):
                divides.append(divides[j]*pow(k,i))
    
    for d in divides:
        if d not in count:
            count[d] = 0
        count[d] += 1

ans = 0
for k,v in count.items():
    if v >= N-1:
        ans = max(ans,k)
print(ans)


### FIXED CODE ###

"""
問題文を言い換えると、「最大公約数が最大になるように数字を一つ書き換えろ」ということ。
さらに言い換えると、「最大公約数が最大になるように数字を一つ消せ」ということ。
単純な線形探索でできるか？→NO

最大公約数を小さくする要因となっている数字を一つ消す。
どういう数字が最大公約数を小さくする要因になっているか？
他の数字と共通する素因数が少ない、小さい、次数が小さい。ただ、この3つを総合的に判断するのは難しすぎる。
素因数分解はやめて、単純に約数で探せばよいか。
各Aの約数を昇順で列挙しておく。
まずA1は必ず残すと仮定して、A1の約数の大きい方から順番に、ほかのAの約数になるかどうかを調べる。
その約数を含んでいないAが一つだけ存在するとき、そのAを消せば、その約数が最大公約数となる。

次に、A1を消す場合の最大公約数も求めて、A1を含める場合の最大公約数と比較して大きい方を採用する。
"""

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

#A1を必ず入れる場合の最大公約数を求める。
yakusu = []

a = A[0]
d = 1
while d*d < a:
    if a % d == 0:
        yakusu.append(d)
        yakusu.append(a//d)
    d += 1
if d*d == a:
    yakusu.append(d)
yakusu.sort(reverse=True)

ans = 1
for v in yakusu:
    cnt = 0
    for i in range(1,N):
        if A[i]%v != 0:
            cnt += 1
    if cnt <= 1:
        ans = v
        break

#A1を入れない場合の最大公約数を求める。
import math
g = A[1]
for i in range(2,N):
    g = math.gcd(g,A[i])

ans = max(ans,g)

print(ans)
### BUGGY CODE ###

n,W,*wv=map(int,open(0).read().split())
w=wv[::2]
v=wv[1::2]
dp=[[0 for i in range(W+1)]for j in range(2)]
for i in range(n):
  t=i&1
  for j in range(W,-1,-1):
    if j<w[i]:
      dp[t][:j+1]=dp[t-1][:j+1]
      break
    else:
      dp[t][j]=max(dp[t-1][j-w[i]]+v[i],dp[t-1][j])
print(dp[(n-1)&1][-1])

### FIXED CODE ###

import numpy as np
n,W,*wv=map(int,open(0).read().split())
w=np.array(wv[::2])
v=np.array(wv[1::2])
dp=np.zeros([n+1,W+1],dtype=int)
for i in range(1,n+1):
  now=np.zeros(W+1,dtype=int)
  now[w[i-1]:]=dp[i-1][:-w[i-1]]+v[i-1]
  dp[i]=np.maximum(dp[i-1],now)
print(dp[-1][-1])
### BUGGY CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))

E = 0
max = 0
for i in range(N - K + 1):
    work = sum(P[i:i+K])
    if work > max:
        max = work
        max_values = P[i:i+K]

for m in max_values:
    E += (1 + m) * m * (1/2) * (1/m)

print(E)



### FIXED CODE ###

N, K = map(int, input().split())
P = list(map(int, input().split()))
P_E = list(((1 + P[i]) / 2) for i in range(N))

E = 0
prog = sum(P_E[0:K])
max = prog
for i in range(N - K):
    prog += P_E[i + K] - P_E[i]
    if prog > max:
        max = prog
    else:
        pass
    
print(max)
### BUGGY CODE ###

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


X, Y, Z, K = read_ints()
A = list(map(int,input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

from itertools import product

AB = [a + b for a, b in product(A, B)]
AB.sort(reverse=True)

ABC = [ab + c for ab, c in product(AB[:K], C)]
ABC.sort(reverse=True)

ABC = ABC[:K]

print(*ABC, sep='\n')



### FIXED CODE ###

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


X, Y, Z, K = read_ints()
A = read_ints()
B = read_ints()
C = read_ints()

from itertools import product

AB = [a + b for a, b in product(A, B)]
AB.sort(reverse=True)

ABC = [ab + c for ab, c in product(AB[:K], C)]
ABC.sort(reverse=True)

ABC = ABC[:K]

print(*ABC, sep='\n')
### BUGGY CODE ###

# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import sys
# sys.setrecursionlimit(10**4)

def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


MOD = 10**9 + 7
L = list(map(int, list(_s())))
ans = 3**(len(L) - 1)
# for i in range(1, len(L)):
#     ans = (ans + 2**(2*i-1)) % MOD

sumL = sum(L)
ans = (ans + 2**(sumL)) % MOD
count = 0
for i, j in enumerate(L[::-1][:-1]):
    if j == 1:
        count += 1
        ans = (ans + 2 * 2**(sumL-count-1) * 3**i) % MOD

print(int(ans))


### FIXED CODE ###

# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np
# import sys
# sys.setrecursionlimit(10**4)

def _li(): return list(map(int, stdin.readline().split()))
def _li_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def _lf(): return list(map(float, stdin.readline().split()))
def _ls(): return stdin.readline().split()
def _i(): return int(stdin.readline())
def _f(): return float(stdin.readline())
def _s(): return stdin.readline()[:-1]


MOD = 10**9 + 7
L = _s()

ans = 3**(len(L) - 1) % MOD
# for i in range(1, len(L)):
#     ans = (ans + 2**(2*i-1)) % MOD

sumL = L.count('1')
ans = (ans + 2**(sumL) % MOD) % MOD

count = 0
for i in range(len(L)-1):
    if L[-1-i] == '1':
        count += 1
        ans = (ans + 2 * pow(2, sumL-count-1, MOD) * pow(3, i, MOD)) % MOD

print(int(ans))
### BUGGY CODE ###

K,S=map(int,input().split())
ans=0

for i in range(K+1):
    for j in range(K+1):
        for k in range(K+1):
            if i+j+k==S:
                ans+=1

print(ans)

### FIXED CODE ###

import itertools

K,S=map(int,input().split())

ans=0
for i in range(K+1):
  for j in range(K+1):
    if 0<=S-(i+j)<=K:
      ans+=1
print(ans)
### BUGGY CODE ###

n, q = map(int, input().split())
ab = tuple(tuple((map(lambda x: int(x)-1, input().split())) for _ in range(n-1)))
px = tuple(tuple(map(int, input().split())) for _ in range(q))

ad_list = [[] for _ in range(n)]
for a, b in ab:
  ad_list[a] += [b]
  ad_list[b] += [a]

count = [0 for _ in range(n)]
for p, x in px:
  count[p-1] += x


def dfs(v, p, ad_list, count):
  for u in ad_list[v]:
    if u != p:
      count[u] += count[v]
      dfs(u, v, ad_list, count)

dfs(0, -1, ad_list, count)

print(*count)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.buffer.readline
def dfs(G,v,p):
    for nv in G[v]:
        if(nv==p):
            continue
        point[nv]+=point[v]
        dfs(G,nv,v)

N,Q=map(int,input().split())
G=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(lambda x:int(x)-1,input().split())
    G[a].append(b)
    G[b].append(a)
point=[0]*N
for i in range(Q):
    p,x=map(int,input().split())
    point[p-1]+=x
dfs(G,0,-1)
print(*point)
### BUGGY CODE ###

import math
import fractions
def lcm(x, y):
  return (x * y) // fractions.gcd(x, y)
  
def main():
  mod = 10**9 +7
  n = int(input())
  ls = list(map(int,input().split()))
  
  t = ls[0]
  for a in ls:
    t = lcm(t,a)
  
  ans = 0
  for a in ls:
    ans = (ans + t//a) % mod 
  print(ans)
  
if __name__ == '__main__':
  main()

### FIXED CODE ###

import math
import fractions
def lcm(x, y):
  return (x * y) // fractions.gcd(x, y)
  
def main():
  mod = 10**9 +7
  n = int(input())
  ls = list(map(int,input().split()))
  
  t = ls[0]
  for a in ls:
    t = lcm(t,a)
  
  ans = 0
  for a in ls:
    ans += t//a
  print(ans%mod)
  
if __name__ == '__main__':
  main()
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
set_a=set(a)
count=0
for i in set_a:
    if i<a.count(i):
        count+=a.count(i)-i
    elif a.count(i)<i:
        count+=a.count(i)
print(count)


### FIXED CODE ###

from collections import Counter
n=int(input())
counter=Counter(map(int,input().split()))
sum=0
for a,cnt in counter.items():
    if cnt<a:
        sum+=cnt
    else:
        sum+=cnt-a
print(sum)
### BUGGY CODE ###

from heapq import *
n,m=map(int,input().split())
a=list(map(int,input().split()))
q=[]
for i in a:
    heappush(q,[-i,i,0])
for i in range(m):
    s,t,u=heappop(q)
    u+=1
    heappush(q,[-(t//(2**u)),t,u])
print(sum(-q[i][0]for i in range(n)))

### FIXED CODE ###

from heapq import *
n,m=map(int,input().split())
a=list(map(int,input().split()))
q=[]
for i in a:
    heappush(q,-i)
for i in range(m):
    s=heappop(q)
    heappush(q,-(-s//2))
print(sum(int(-q[i])for i in range(n)))
### BUGGY CODE ###

MOD = 10**9 + 7

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    def solve(start, num, x):
        if start == N-1:
            return (num * (x % S[-1])) % MOD
        total = 0
        for i in range(start, N-1):
            total = (total + solve(i+1, num, (x % S[i]))) % MOD
            num = (num * (N-i-1)) % MOD
        total = (total + num * (x % S[N-1])) % MOD
        return total
    total = 0
    num = 1
    for i in range(N-1):
        total += solve(i+1, num, (X % S[i]) % MOD)
        num *= N-i-1
    total = (total + num * (X % S[-1])) % MOD
    print(total)

main()


### FIXED CODE ###

from collections import defaultdict as dd
MOD = 10**9 + 7

def main():
    N, X = list(map(int, input().split()))
    S = list(map(int, input().split()))
    S = sorted(S, reverse=True)

    dp = dd(int)
    dp[X] = 1
    for i in range(N):
        new = dd(int)
        for x in dp:
            xx = x % S[i]
            new[x] += dp[x] * (N-i-1)
            new[x] %= MOD
            new[xx] += dp[x]
            new[xx] %= MOD
        dp = new

    total = 0
    for x in dp:
        total += dp[x] * x
        total %= MOD
    print(total)

main()
### BUGGY CODE ###

S = input()

remove_count = 0
while(True) :
    S = S.replace("10","X").replace("01","X")
    current_count = S.count("X")
    if (current_count == 0) :
        break
    remove_count += current_count
    S = S.replace("X","")

print(remove_count * 2)

### FIXED CODE ###

S = input()

remove_count = 0
# while(True) :
#     S = S.replace("10","X").replace("01","X")
#     current_count = S.count("X")
#     if (current_count == 0) :
#         break
#     remove_count += current_count
#     S = S.replace("X","")
# print(remove_count * 2)
print(min(S.count("1"), S.count("0") )* 2)
### BUGGY CODE ###

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

asum = [0]
bsum = [0]
tmp = 0
for i in range(n):
    tmp += a[i]
    asum.append(tmp)
tmp = 0
for i in range(m):
    tmp += b[i]
    bsum.append(tmp)


ans = 0
for na in range(n+1):
    for nb in range(m+1):
        time = asum[na] + bsum[nb]
        if time > k:
            break
        ans = max(ans, na+nb)
print(ans)

### FIXED CODE ###

import copy

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

asum = [0]
bsum = [0]
tmp = 0
for i in range(n):
    tmp += a[i]
    asum.append(tmp)
tmp = 0
for i in range(m):
    tmp += b[i]
    bsum.append(tmp)


ans = 0
nb = copy.deepcopy(m)
for na in range(n+1):
    res = k - asum[na]
    if res < 0:
        break
    flg = True
    while flg:
        if bsum[nb] > res:
            nb -= 1
            continue
        ans = max(ans, na+nb)
        break
print(ans)
### BUGGY CODE ###

N = int(input())
S = list(map(str,input()))

R = []
G = []
B = []
for i in range(N):
    if S[i] == "R":
        R.append(i)
    elif S[i] == "G":
        G.append(i)
    else:
        B.append(i)
ans = 0
for i in range(N):
    for j in range(1,N):
        if i+2*j < N:
            if S[i] != S[i+j] and S[i] != S[i+2*j] and S[i+j] != S[i+2*j]:
                ans +=1

print(len(R)*len(G)*len(B)-ans)

### FIXED CODE ###

N = int(input())
S = list(map(str,input()))

r = 0
g = 0
b = 0
for i in range(N):
    if S[i] == "R":
        r+=1
    elif S[i] == "G":
        g+=1
    else:
        b+=1
ans = 0
for i in range(N):
    for j in range(1,N//2+2):
        if i+2*j < N:
            if S[i] != S[i+j] and S[i] != S[i+2*j] and S[i+j] != S[i+2*j]:
                ans +=1

print(r*g*b-ans)
### BUGGY CODE ###

n = int(input())
numbers = list(map(int, input().split(' ')))

counters = {}
used_list = []
for i in numbers:
    if i in used_list:
        continue
    counters[i] = numbers.count(i)
    used_list.append(i)
for i in range(n):
    sonuc = 0
    for sayi in counters:
        count = counters[sayi]
        if numbers[i] == sayi:
            count -= 1
        sonuc += count*(count-1)/2
    print(int(sonuc))

### FIXED CODE ###

n = int(input())
numbers = list(map(int, input().split(' ')))

counters = {}
cevap_list = {}
sayilar = set(numbers)
for i in numbers:
    if i in counters:
        counters[i] += 1
    else:
        counters[i] = 1

sonuc = 0
for sayi in counters:
    count = counters[sayi]
    sonuc += int(count * (count - 1) / 2)


for i in range(n):
    print(sonuc-counters[numbers[i]]+1)
### BUGGY CODE ###

n = int(input())
h = tuple(map(int, input().split()))
for i in range(1, n+1):
  print(h.index(i)+1, end =  " ")

### FIXED CODE ###

n = int(input())
h = tuple(map(int, input().split()))
ans = [0]*n
for i in range(n):
  ans[h[i]-1] = i+1
print(*ans)
### BUGGY CODE ###

n = input().split()
N = int(n[0])
K = int(n[1])

lista = []
listb = [1]
C = 1
A = list(map(int, input().split()))
                 
for i in range(2*10**5 + 2):
    C = A[C-1]
    if C not in listb:
        listb.append(C)
    else:
        D = listb.index(C)
        listb.append(C)
        break

L = len(listb) -1

if K <= N:
    print(listb[K])
else:
    if L-D == 1 or L-D == 0:
        print(C)
    else:
        M = (K-D) % (L-D)
        print(listb[D+M])

### FIXED CODE ###

n = input().split()
N = int(n[0])
K = int(n[1])

listb = [1]
listc = [0]*N
C = 1
A = list(map(int, input().split()))
                 
for i in range(N):
    C = A[C-1]
    if listc[C-1] == 0:
        listb.append(C)
        listc[C-1] = 1
    else:
        D = listb.index(C)
        listb.append(C)
        break

L = len(listb) -1

if K <= N:
    print(listb[K])
else:
    if L-D == 1 or L-D == 0:
        print(C)
    else:
        M = (K-D) % (L-D)
        print(listb[(D+M)%L])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N=int(input())
S=input()
dic={}
for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            tmp=S[i]+S[j]+S[k]
            if tmp in dic:
                pass
            else:    
                dic[tmp]=1
print(len(dic))

### FIXED CODE ###

import sys
import copy
input = sys.stdin.readline

N=int(input())

S=input()


T=[[0] * 10 for _ in range(30000)]
for i in range(N):
    if i!=0:
        T[i]=copy.deepcopy(T[i-1])
    T[i][int(S[i])]+=1
#print(T)

dic={}
flag=[False]*10
for i in range(N-2):
    if flag[int(S[i])]==True:
        continue
    flag[int(S[i])]=True
    for j in range(i+1,N-1):
        tmp2=copy.deepcopy(T[N-1])
        tmp3=copy.deepcopy(T[j])
        tmp4=""
        for k in range(10):
            if tmp2[k]-tmp3[k]!=0:
                tmp4=S[i]+S[j]+str(k)
                #print(tmp4)
                if tmp4 in dic:
                    pass
                else:    
                    dic[tmp4]=1
           
print(len(dic))
#print(dic)
### BUGGY CODE ###

import queue
n,Q = map(int,input().split())
ki = [[] for i in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    ki[a-1].append(b-1)
    ki[b-1].append(a-1)
q = queue.Queue()
cnt = [0] * n
for _ in range(Q):
    p,x = map(int,input().split())
    cnt[p-1] += x
vis = [-1] * n
q.put(0)
vis[0] = 1
while True:
    if q.empty():
        break
    a = q.get()
    a_ = ki[a]
    for i in a_:
        if vis[i] == -1:
            q.put(i)
            vis[i] = 1
            cnt[i] += cnt[a]
print(*cnt)

### FIXED CODE ###

from collections import deque
n,Q = map(int,input().split())
ki = [[] for i in range(n)]
for _ in range(n-1):
    a,b = map(int,input().split())
    ki[a-1].append(b-1)
    ki[b-1].append(a-1)
q = deque()
cnt = [0] * n
for _ in range(Q):
    p,x = map(int,input().split())
    cnt[p-1] += x
vis = [-1] * n
q.append(0)
vis[0] = 1
while q:
    a = q.popleft()
    a_ = ki[a]
    for i in a_:
        if vis[i] == -1:
            q.append(i)
            vis[i] = 1
            cnt[i] += cnt[a]
print(*cnt)
### BUGGY CODE ###

n,m = map(int,input().split())
para_val = []
for i in range(m) :
    a,b = map(int,input().split())
    para_val.append([i for i in range(a,b+1)])

ans = [i for i in range(1,n+1)]

for i in range(m):
    ans = list(set(ans) & set(para_val[i]))

print(len(ans))

### FIXED CODE ###

n,m = map(int,input().split())
para_val = []
for i in range(m) :
    a,b = map(int,input().split())
    para_val.append([a,b])

migi = []
hidari = []
for i,j in para_val:
    migi.append(i)
    hidari.append(j)

a = max(migi)
b = min(hidari)

if b - a >= 0 : print("{}".format(b-a+ 1))
else : print("0")
### BUGGY CODE ###

N = int(input())
A = [int(input()) for i in range(N)]
l = []

for ai in A:
  if l == []:
    l.append([ai])
    continue
    
  flag = False
  for m in l:
    if max(m) < ai:
      m.append(ai)
      flag = True
      break
  if flag == False:
    l.append([ai])
    
print(len(l))

### FIXED CODE ###

import sys
input = sys.stdin.readline
from bisect import bisect_right

N = int(input())
l = [-int(input())]

for i in range(N-1):
  ai = -int(input())
# 各色の最大値がlの要素になるようにリストを拡大させていく
  if l[-1] <= ai:
    l.append(ai)
  else:
    b = bisect_right(l, ai)
    l[b] = ai
    
print(len(l))
### BUGGY CODE ###

import math
n, a, b = map(int, input().split())
d = []
for i in range(n):
    d.append(input())
# [d1, d2, d3, ..., dN]
h = [int(m) for m in d]#int型に直す
#print(x)

low = 0
high = math.ceil(max(h)/b)
# t は中央番目の数
t = (low + high) // 2

# 探索の下限のlowが上限のhighになるまで探索
# lowがhighに達すると数は見つからなかっTrueたということ
X = 0
Y = 0
while True:
    for j in range(n):
        X += max(0, math.ceil((h[j] - t * b) / (a - b)))
        Y += max(0, math.ceil((h[j] - (t - 1) * b) / (a - b)))
    if X <= t and Y > t - 1:
        print(t)
        exit()
    elif X <= t and Y <= t - 1:
        high = t - 1
        """for j in range(n):
            Y += max(0, math.ceil((h[j] - (t - 1) * b) / (a - b)))"""
    elif X > t:
        low = t + 1
    t = (low + high) // 2
    X = 0
    Y = 0

### FIXED CODE ###

import math
n, a, b = map(int, input().split())
d = []
for i in range(n):
    d.append(input())
# [d1, d2, d3, ..., dN]
h = [int(m) for m in d]#int型に直す
#print(x)

low = 0
high = math.ceil(max(h)/b)
# t は中央番目の数
t = (low + high) // 2

# 探索の下限のlowが上限のhighになるまで探索
# lowがhighに達すると数は見つからなかっTrueたということ
X = 0
Y = 0
s = 0
while high - low >= 0:
    for j in range(n):
        X += max(0, math.ceil((h[j] - t * b) / (a - b)))
        #Y += max(0, math.ceil((h[j] - (t - 1) * b) / (a - b)))
    if X == t:
        print(t)
        exit()
    elif X < t:
        high = t - 1
        """for j in range(n):
            Y += max(0, math.ceil((h[j] - (t - 1) * b) / (a - b)))"""
    elif X > t:
        low = t + 1
    s = t
    t = (low + high) // 2
    X = 0
    Y = 0
for j in range(n):
  if math.ceil((h[j] - s * b) / (a - b)) > 0:
            Y += math.ceil((h[j] - s * b) / (a - b))
if Y <= s:
  print(s)
else:
  print(s + 1)
### BUGGY CODE ###

s=int(input())
while s%10==0:
    s//=10
s=str(s)
lens=len(s)

dp=[[0,0] for i in range(lens)]

dp[0][0]=int(s[0])
dp[0][1]=11-int(s[0])

for i in range(1,lens):
    dp[i][0]=min(dp[i-1][0]+int(s[i]),dp[i-1][1]+int(s[i]))
    dp[i][1]=min(dp[i-1][0]+11-int(s[i]),dp[i-1][1]+9-int(s[i]))

print(min(dp[-1]))

### FIXED CODE ###

def main():
  s=input()
  #while s%10==0:
  #    s//=10
  #s=9**100
  #s=str(s)
  lens=len(s)

  d=int(s[0])
  p=11-int(s[0])

  for c in s[1:]:
      j=int(c)
      a=min(d+j,p+j)
      b=min(d+11-j,p+9-j)
      d,p=a,b

  print(min(d,p))

if __name__ == '__main__':
    main()
### BUGGY CODE ###

def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = [0]*N
    s = 0
    for i in range(N):
        a = A[i]
        s += a
        S[i] = s

    # print(S)
    ans = 0
    if S[0] == 0:
        ans += 1
    for i in range(1, N):
        if S[i] == 0:
            ans += 1
        for j in range(i):
            d = S[i] - S[j]
            if d == 0:
                ans += 1
    print(ans)

if __name__ == '__main__':
    main()


### FIXED CODE ###

def cmb(n, r):
    ans1 = 1
    for i in range(n - r + 1, n + 1):
        ans1 *= i
    ans2 = 1
    for i in range(1, r + 1):
        ans2 *= i
    return ans1 // ans2

def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = [0]*N
    s = 0
    for i in range(N):
        a = A[i]
        s += a
        S[i] = s

    # print(S)
    ans = 0
    B = {}
    for s in S:
        if s not in B:
            B[s] = 0
        B[s] += 1
    # print(B)
    for b in B:
        if b == 0:
            ans += B[b]
        if B[b] > 1:
            ans += cmb(B[b], 2)
    print(ans)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

s=input()[::-1]

amari=[[(10**i)*x%13 for x in range(10)] for i in range(6)]
dp=[1]+[0]*12
new_dp=[0]*13

mod=10**9+7

for id,char in enumerate(s):
    new_dp=[0]*13
    if char!='?':
        for i in range(13):
            new_i=(i+amari[id%6][int(char)])%13
            new_dp[new_i]+=dp[i]
        dp=new_dp
    else:
        for i in range(13):
            for j in range(10):
                new_i=(i+amari[id%6][j])%13
                new_dp[new_i]+=dp[i]
        dp=[x%mod for x in new_dp]
print(dp[5])
        

### FIXED CODE ###

s=input()[::-1]

amari=[[(10**i)*x%13 for x in range(10)] for i in range(6)]
dp=[1]+[0]*12
new_dp=[0]*13

mod=10**9+7

for id,char in enumerate(s):
    new_dp=[0]*13
    if char!='?':
        for i in range(13):
            new_i=(i+amari[id%6][int(char)])%13
            new_dp[new_i]+=dp[i]
        dp=new_dp
    else:
        for i in range(13):
            for j in range(10):
                new_i=(i+amari[id%6][j])%13
                new_dp[new_i]+=dp[i]
        dp=[x%mod for x in new_dp]
print(dp[5])
### BUGGY CODE ###

N=int(input())
l=[]
for i in range(N):
  l.append(int(input()))
i=l.index(max(l))
if i==0:
  l1=l[1:]
  print(max(l1))
  for a in range(N-1):
    print(max(l))
elif i==N:
  l1=l[:N]
  for a in range(N-1):
    print(max(l))
  print(max(l1))
else:
  l1=l[:i]+l[i+1:]
  for a in range(i):
    print(max(l))
  print(max(l1))
  for a in range(N-i-1):
    print(max(l))

### FIXED CODE ###

N=int(input())
l=[]
for i in range(N):
  l.append(int(input()))
l_max=max(l)
i=l.index(l_max)
if i==0:
  l1=l[1:]
  print(max(l1))
  for a in range(N-1):
    print(l_max)
elif i==N:
  l1=l[:N]
  for a in range(N-1):
    print(l_max)
  print(max(l1))
else:
  l1=l[:i]+l[i+1:]
  for a in range(i):
    print(l_max)
  print(max(l1))
  for a in range(N-i-1):
    print(l_max)
### BUGGY CODE ###

N,M = map(int,input().split())
gra = [[]for i in range(N)]
p = [[0]+[-float("inf")]*(N-1) for i in range(N*2+1)]
p[0][0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    gra[a-1].append([b-1,c])
for i in range(N*2):
    for j in range(N):
        for b,c in gra[j]:
            p[i+1][b] = max(p[i+1][b],p[i][j]+c,p[i][b])
print(["inf",p[N][-1]][p[N][-1] == p[-1][-1]])

### FIXED CODE ###

N,M = map(int,input().split())
gra = [[]for i in range(N)]
p = [[0]+[-float("inf")]*(N-1) for i in range(N*2+1)]
p[0][0] = 0
for i in range(M):
    a,b,c = map(int,input().split())
    gra[a-1].append([b-1,c])
for i in range(N*2):
    for j in range(N):
        for b,c in gra[j]:
            p[i+1][b] = max(p[i+1][b],p[i][j]+c,p[i][b])
print(["inf",p[N][-1]][p[N][-1] == p[-1][-1]])