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

K=int(input())
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

answer=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            answer=answer+gcd(i,j,k)
print(answer)


### FIXED CODE ###

K=int(input())
import math
from functools import reduce

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

answer=0
for i in range(1,K+1):
    for j in range(1,K+1):
        for k in range(1,K+1):
            answer=answer+gcd(i,j,k)
print(answer)
### BUGGY CODE ###

n = int(input())
a_list = [int(num) for num in input().split(" ")]

total_comb = 0
ball_num_count = {}
for i in range(1,n+1):
    i_count = a_list.count(i)
    total_comb += int(i_count*(i_count-1)/2)
    ball_num_count[i] = int(i_count*(i_count-1)/2 - (i_count-1)*(i_count-2)/2)
for i in range(n):
    except_num = a_list[i]
    print(total_comb - ball_num_count[except_num])


### FIXED CODE ###

import collections

n = int(input())
a_list = [int(num) for num in input().split(" ")]
c_dict = collections.Counter(a_list)

total_comb = 0
for i in c_dict:
    i_count = c_dict[i]
    total_comb += int(i_count*(i_count-1)/2)
for k in range(n):
    except_num = a_list[k]
    print(total_comb - (c_dict[except_num]-1))
### BUGGY CODE ###

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

maxlist = sum(numlist[0:K])
max_num = 0
for i in range(N-K+1):
  a = sum(numlist[i:i+K])
  if a >= maxlist:
    maxlist = a
    max_num = i

definelist = numlist[max_num:max_num+K]
expected = 0
for j in definelist:
  dice = list(range(1, j+1))
  p = 1/len(dice)
  expected += sum(p*x for x in dice)

print(expected)

### FIXED CODE ###

import itertools

n, k = map(int, input().split())
lis = list(map(int,input().split()))

exlis = []


for i in lis:
    a = (1+i)/2
    exlis.append(a)


nlis = list(itertools.accumulate(exlis))


ans  = nlis[k-1]
for j in range(n-k):
    tmp = nlis[j+k] - nlis[j]
    ans = max(ans, tmp)

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

import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
S = rs()

def get_max(i):
	ok, ng = i, len(S)
	while ng-ok > 1:
		c = (ok+ng)//2
		if S[i:c] in S[c:]:
			ok = c
		else:
			ng = c
	return ok-i

ans = 0
for i in range(N):
	l = get_max(i)
	ans = max(ans, l)
print(ans)


### FIXED CODE ###

import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
S = rs()

dp = [[0] * (N+1) for _ in range(N+1)]

ans = 0
for i in range(N):
	for j in range(i+1,N):
		if S[i] == S[j]:
			if i+1 < j+1-(dp[i][j]+1)+1:
				dp[i+1][j+1] = dp[i][j]+1
			else:
				dp[i+1][j+1] = 0
			ans = max(ans, dp[i+1][j+1])
print(ans)
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
q = int(input())
dic = {}
s = sum(a)
for i in range(1,10**5+1):
    dic[i] = a.count(i)
for j in range(q):
    b,c = map(int,input().split())
    s += (c-b)*dic[b]
    dic[c] += dic[b]
    dic[b] = 0
    print(s)

### FIXED CODE ###

from collections import Counter
n = int(input())
a = list(map(int,input().split()))
q = int(input())
num = Counter(a)
s = sum(a)
for _ in range(q):
    b,c = map(int,input().split())
    s += (c-b)*num[b]
    num[c] += num[b]
    num[b] = 0
    print(s)
### BUGGY CODE ###

n=int(input())
s=list(input())
q=int(input())

for _ in range(q):
    m,l,r=input().split()
    
    if m=='1':
        s[int(l)-1]=r
    else:
        print(len(set(s[int(l)-1:int(r)])))


### FIXED CODE ###

import bisect
n = int(input())
s = list(input())
alp = 'abcdefghijklmnopqrstuvwxyz'
cnt = {c: [] for c in alp}

for i, c in enumerate(s):
    cnt[c].append(i)

q = int(input())
for _ in range(q):
    t, x, y = input().split()
    ans = 0
    if t == '1':
        i, c = int(x) - 1, y
        if s[i] == c:
            continue
        a = bisect.bisect_left(cnt[s[i]], i)
        cnt[s[i]].pop(a)
        s[i] = c
        b = bisect.bisect_left(cnt[c], i)
        cnt[c].insert(b, i)
    else:
        l, r = int(x) - 1, int(y) - 1
        for c in alp:
            a = bisect.bisect_left(cnt[c], l)
            if a < len(cnt[c]) and cnt[c][a] <= r:
                ans += 1

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

mod=10**9+7
S=input()
dp=[0]*13
dp[0]=1

for s in S:
    prev=[x for x in dp]
    dp=[0]*13
    if s=='?':
        nx=range(10)
    else:
        nx=[int(s)]
    for i in range(13):
        for j in nx:
            dp[(10*i+j)%13]+=prev[i]
    dp=[x%mod for x in dp]
print(dp[5])

### FIXED CODE ###

mod=10**9+7
S=input()
dp=[0]*13
dp[0]=1

for s in S:
    prev=[x for x in dp]
    dp=[0]*13
    if s=='?':
        nx=range(10)
    else:
        nx=[int(s)]
    for i in range(13):
        for j in nx:
            dp[(10*i+j)%13]+=prev[i]
    dp=[x%mod for x in dp]
print(dp[5])
### BUGGY CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
saisyo = min(A)
saidai = max(A)
cb = []
count = 0
for _ in range(M):
    b, c = map(int, input().split())
    if c > saisyo and count < N: #枝刈り
        A += [c] * b
    if c > saidai:
        count += b
        cb += [c] * b
ans = 0
if count < N:
    A.sort(reverse = True)
    for i in range(N):
        ans += A[i]

if count >= N:
    cb.sort(reverse = True)
    for i in range(N):
        ans += cb[i]

print (ans)

### FIXED CODE ###

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

cb = []
for i in A:
    cb += [[i,1]]
for _ in range(M):
    b, c = map(int, input().split())
    cb += [[c, b]]
cb.sort(reverse = True)
#print (cb)

ans = 0
i = 0
j = 0
while i <= N:
    a = min(N-i, cb[j][1])
    ans += cb[j][0] * a
    j += 1
    i += a
    #print (ans)
    if i == N:
        print (ans)
        i += 1
### BUGGY CODE ###

def xxx(l,n,a,b,c):
  X=10000
  for i in range(n):
    for j in range(n):
      if j==i: continue
      for k in range(n):
        if k==j or k==i: continue
        x=abs(a-l[i])+abs(b-l[j])+abs(c-l[k])
        if x<X:
          X=x
  return X
import copy
def xx(l,n,a,b,c):
  X=10000
  if n<3:
    return 10000
  else:
    for p in range(n):
      for q in range(n):
        if p!=q:
          ll=copy.deepcopy(l) 
          ll[p]+=ll[q]
          ll.pop(q)
          x=10+xxx(ll,n-1,a,b,c)
          x2=10+xx(ll,n-1,a,b,c)
          if x<X:
            X=x
          if x2<X:
            X=x2
  return X

n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
  l.append(int(input()))
X=xxx(l,n,a,b,c)
if xx(l,n,a,b,c)<X:
  X=xx(l,n,a,b,c)
print(X)  

### FIXED CODE ###

N,A,B,C=map(int, input().split())
L=[int(input()) for i in range(N)]
from itertools import product
ans=10**9
for p in product(range(4),repeat=N):
    l=[0]*3
    cost=0
    for i,t in enumerate(p):
        if t<3:
            if l[t]>0:
                cost+=10
            l[t]+=L[i]
    if min(l)>0:
        ans=min(ans,cost+abs(A-l[0])+abs(B-l[1])+abs(C-l[2]))
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
S = str(input())
T = str(input())
#print(S)
 
# 最大公約数
def gcd(a, b):
  while b > 0:
    a, b = b, a%b
  return a
 
# 最小公倍数
def lcm(a, b):
  return a*b/gcd(a, b)
 
L = int(lcm(N, M))
 
#文字列の作成
#SをもとにX
X1 = r''
n = 0
m = 0
 
#list内包にしたらはやいのかな？
for i in range(0,L):
  if i == n * (L//N):
    X1 += S[n]
    n += 1
  else:
    X1 += r'*'
#print(X1)  
#X1とTを比べる
 
  if i == m * (L//M) :
    #print(i,m,X1[i],T[m])
    if X1[i] == T[m] or X1[i] == "*":
      m += 1
    else:
      print(-1)
      exit()
 
print(int(L)) 

### FIXED CODE ###

from fractions import gcd
from bisect import bisect_left
 
N, M = map(int, input().split())
S = input()
T = input()
 
if N > M:
    N, M = M, N
    S, T = T, S

G = N*M // gcd(N, M)
 
a = G//N
b = G//M
 
X = [a*i for i in range(N)]
Y = [b*j for j in range(M)]
 
for i, x in enumerate(X):
    j = bisect_left(Y, x)
    if j < M and Y[j] == x:
        if S[i] != T[j]:
            print(-1)
            break
else:
    print(G)
### BUGGY CODE ###

import bisect
n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

cnt = 0
for a in A:
    index_b = bisect.bisect_right(B, a)
    for b in B[index_b:]:
        index_c = bisect.bisect_right(C, b)
        cnt += n-index_c

print(cnt)

### FIXED CODE ###

import bisect
n = int(input())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
C = sorted(list(map(int, input().split())))

cnt = 0
for b in B:
    index_a = bisect.bisect_left(A, b)
    index_c = bisect.bisect_right(C, b)
    cnt += index_a * (n - index_c)

print(cnt)
### BUGGY CODE ###

n = int(input())
a =[int(x) for x in input().split()]
ans2 = 0
mod = 10**9 + 7
for index,i in enumerate(a):
    for j in a[index+1:]:
        ans2 = (ans2 + (i * j)%mod) % mod
print(ans2)

### FIXED CODE ###

n = int(input())
a_list =[int(x) for x in input().split()]
mod = 10**9 + 7

s_list = [0]*(n+1)
for i,a in enumerate(a_list):
    s_list[i+1] = s_list[i]+a


ans = 0
for i,a in enumerate(a_list):
    ans += a * (s_list[n]-s_list[i+1])
    
print(ans%mod)
### BUGGY CODE ###

from collections import deque
import copy

def main():
  H, W = list(map(int, input().split()))
  field = [[i=='#' for i in input()] for _ in range(H)]
  count = 0
  q = deque([(i, j) for i in range(H) for j in range(W) if field[i][j]])
  while True:
    nq = deque()
    while q:
      y, x = q.popleft()
      for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
        ny, nx = y + dy, x + dx
        if 0<=ny<H and 0<=nx<W and not field[ny][nx]:
          field[ny][nx] = True
          nq.append((ny, nx))
    if nq:
      q = copy.copy(nq)
    else:
      break
    count += 1
  print(count)
      
if __name__ == "__main__":
  main()

### FIXED CODE ###

from collections import deque
import copy

def main():
  H, W = map(int, input().split())
  maps = [[j=="#" for j in input()] for i in range(H)]
  q = deque([(i, j) for i in range(H) for j in range(W) if maps[i][j]])
  count = 0
  while True:
    nq = deque()
    while q:
      x, y = q.popleft()
      if x > 0 and not maps[x-1][y]:
        maps[x-1][y] = True
        nq.append((x-1,y))
      if y > 0 and not maps[x][y-1]:
        maps[x][y-1] = True
        nq.append((x,y-1))
      if x < H-1 and not maps[x+1][y]:
        maps[x+1][y] = True
        nq.append((x+1,y))
      if y < W-1 and not maps[x][y+1]:
        maps[x][y+1] = True
        nq.append((x,y+1))
    if nq:
      q = copy.copy(nq)
    else:
        break
    count += 1
  print(count)

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

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

count = 1
L = []
for i in range(N):
    if i != N-1:
        if S[i] != S[i+1]:
            L.append(count)
            count = 1
        else:
            count += 1
    else:
        L.append(count)
        count += 1



if S[0] == '1':
    width = K * 2 + 1
    flag = True
    index = 0
    ans = 0
    while flag:
        if index + width <= len(L):
            tmp = sum(L[index:index+width])
            ans = max(ans, tmp)
        else:
            tmp = sum(L[index:index+width])
            ans = max(ans, tmp)
            flag = False
        index += 2
    print(ans)

if S[0] == '0':
    width = K * 2 + 1
    flag = True
    index = 0
    ans = 0
    while flag:
        if index != 0:
            if index + width <= len(L):
                tmp = sum(L[index:index+width])
                ans = max(ans, tmp)
            else:
                tmp = sum(L[index:index+width])
                ans = max(ans, tmp)
                flag = False
            index += 2

        else:
            else_width = width - 1
            if index + width <= len(L):
                tmp = sum(L[index:index+else_width])
                ans = max(ans, tmp)
            else:
                tmp = sum(L[index:index+width])
                ans = max(ans, tmp)
                flag = False
            index += 1

    print(ans)

### FIXED CODE ###

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

count = 1
L = []
L.append(0)
for i in range(N):
    if i < N-1:
        if S[i] == S[i+1]:
            count += 1
        else:
            L.append(count)
            count = 1
    else:
        L.append(count)
        count = 1
L.append(0)

if S[0] == '1':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 1:
            if i - dist >= 0:
                tmp -= L[i-dist]
                tmp -= L[i-dist+1]
            ans = max(ans, tmp)
        else:
            if i == N - 1:
                ans = max(ans, tmp)
    print(ans)

if S[0] == '0':
    ans = 0
    tmp = 0
    dist = 2 * (K + 1)
    for i in range(len(L)):
        tmp += L[i]
        if i % 2 == 0:
            if i - dist + 1 >= 0:
                tmp -= L[i-dist+1]
            if i - dist >= 0:
                tmp -= L[i-dist]
            ans = max(ans, tmp)
        else:
            if i == N - 1:
                ans = max(ans, tmp)
    print(ans)
### BUGGY CODE ###

n = int(input())
s = input()
passwords = []

firsts = []
firsts_seconds = []

for i in range(n-2):
    first =  s[i]
    if first in firsts:
        continue
    firsts.append(first)
    for j in range(i+1,n-1):
        second = s[j]
        first_second = first + second
        if first_second in firsts_seconds:
            continue
        firsts_seconds.append(first_second)
        for k in range(j+1, n):
            password = first_second + s[k]
            if password not in passwords:
                passwords.append(password)

print(len(passwords))

### FIXED CODE ###

n = int(input())
s = input()
passwords = []

firsts = []
firsts_seconds = [[]*10 for _ in range(10)]
firsts_seconds_thirds = [[]*10 for _ in range(100)]

for i in range(n-2):
    if len(firsts) == 10:
        break
    first =  s[i]
    if first in firsts:
        continue
    firsts.append(first)
    for j in range(i+1,n-1):
        if len(firsts_seconds[int(first)]) == 10:
            break
        second = s[j]
        first_second = first + second
        if first_second in firsts_seconds[int(first)]:
            continue
        firsts_seconds[int(first)].append(first_second)
        for k in range(j+1, n):
            password = first_second + s[k]
            if password not in firsts_seconds_thirds[int(first_second)]:
                firsts_seconds_thirds[int(first_second)].append(password)
                passwords.append(password)

print(len(passwords))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

MOD = 1000000007
n, m = map(int, input().split())
a = [int(input()) for _ in range(m)]

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

for i in range(1, n+1):
    if i in a:
        dp[i] = 0
    else:
        dp[i] += dp[i-1] + dp[i-2]
        dp[i] %= MOD

print(dp[n])

### FIXED CODE ###

import sys
input = sys.stdin.readline

MOD = 1000000007
n, m = map(int, input().split())
hole = [0]*(n+1)

for i in range(m): hole[int(input())] = 1

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

for i in range(1, n+1):
    if hole[i]: dp[i] = 0
    else:
        dp[i] += dp[i-1] + dp[i-2]
        dp[i] %= MOD

print(dp[n])
### BUGGY CODE ###

from fractions import gcd

from functools import reduce
K=int(input())

def gcd_list(a,b,c):
  return gcd(gcd(a,b),gcd(b,c))


count=0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      
      if i==j and j==k and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd_list(i,j,k)

print(count)

### FIXED CODE ###

import math

from functools import reduce
K=int(input())

def gcd_list(numbers):
  return reduce(math.gcd, numbers)


count=0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if i==j and j==k and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd_list([i,j,k])

print(count)
### BUGGY CODE ###

#import numpy as np
import math
import collections
import bisect


a, b, c, d = map(int, input().split())

dp0 = [[1 for i in range(d + 1)] for j in range(c + 1)]
dp1 = [[0 for i in range(d + 1)] for j in range(c + 1)]
for i in range(c + 1):
    for j in range(d + 1):
        if j < b or i < a:
            dp0[i][j] = 0
            dp1[i][j] = 0
for i in range(a, c + 1):
    for j in range(b, d + 1):
        # print(i, j, dp[i][j])
        if a == i and b == j:
            continue
        dp0[i][j] = ((i * dp0[i][j - 1]) % 998244353 + dp1[i][j - 1]) % 998244353
        dp1[i][j] = (j * (dp0[i - 1][j] + dp1[i - 1][j]) % 998244353) % 998244353

print((dp0[c][d] + dp1[c][d]) % 998244353)


### FIXED CODE ###

#import numpy as np
import math
import collections
import bisect


def main():
    a, b, c, d = map(int, input().split())

    dp0 = [[1 for i in range(d + 1)] for j in range(c + 1)]
    dp1 = [[0 for i in range(d + 1)] for j in range(c + 1)]
    for i in range(c + 1):
        for j in range(d + 1):
            if j < b or i < a:
                dp0[i][j] = 0
                dp1[i][j] = 0
    for i in range(a, c + 1):
        for j in range(b, d + 1):
            # print(i, j, dp[i][j])
            if a == i and b == j:
                continue
            dp0[i][j] = ((i * dp0[i][j - 1]) % 998244353 + dp1[i][j - 1]) % 998244353
            dp1[i][j] = (j * (dp0[i - 1][j] + dp1[i - 1][j]) % 998244353) % 998244353

    print((dp0[c][d] + dp1[c][d]) % 998244353)


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

def main():
    n, p = map(int, input().split())
    s = input()

    TF = 2, 5
    if p in TF:
        answer = 0
        for i, char in enumerate(s):
            if int(char) % p == 0:
                answer += i + 1
        print(answer)
        return

    u = [int(s[i:]) for i in range(n)]
    u.append(0)
    # The numbers to consider are int(s[i:j]) (0 <= i < j <= n)
    # u[i] := int(s[i:]) (u[n] = 0 for convenience)
    # int(s[i:j]) and u[i] - u[j] are congruent (unless p == 2 or 5)
    remainders = [number % p for number in u]
    d = [0] * p
    for remainder in remainders:
        d[remainder] += 1
    answer = 0
    for count in d:
        if count > 1:
            answer += count * (count - 1) // 2
    print(answer)

main()


### FIXED CODE ###

def main():
    n, p = map(int, input().split())
    s = input()

    TF = 2, 5
    if p in TF:
        answer = 0
        for i, char in enumerate(s):
            if int(char) % p == 0:
                answer += i + 1
        print(answer)
        return

    # u = [int(s[i:]) for i in range(n)]
    remainders = []
    t = 0
    for i, char in enumerate(s[::-1]):
        t += pow(10, i, p) * int(char)
        remainders.append(t % p)
    remainders = remainders[::-1]
    remainders.append(0)
    # The numbers to consider are int(s[i:j]) (0 <= i < j <= n)
    # u[i] := int(s[i:]) (u[n] = 0 for convenience)
    # int(s[i:j]) and u[i] - u[j] are congruent (unless p == 2 or 5)
    d = {}
    for remainder in remainders:
        if remainder not in d:
            d[remainder] = 1
        else:
            d[remainder] += 1
    answer = 0
    for count in d.values():
        if count > 1:
            answer += count * (count - 1) // 2
    print(answer)

main()
### BUGGY CODE ###

q = int(input())

l = []
x = ""
y = ""

for i in range(q):
    x = input()
    y = input()
 
    yLen = len(y)
    xLen = len(x)
    l = [[0] * (xLen + 1) for j in range(yLen + 1)]

    for j in range(1, yLen + 1):
        for k in range(1, xLen + 1):
            if x[k - 1] == y[j - 1]:
                l[j][k] = 1 + l[j - 1][k - 1]
            else:
                if l[j][k - 1] > l[j - 1][k]:
                    l[j][k] = l[j][k - 1]
                else:
                    l[j][k] = l[j - 1][k]
 
    print(l[yLen][xLen])



### FIXED CODE ###

q = int(input())

for i in range(q):
    x = input()
    y = input()
 
    l = []
    for j, s in enumerate(x):
        l.append([])
        l[j].append(-1)
        if j == 0:
            endPos = y.find(s)
            if endPos != -1: 
                l[j].append(endPos)
        else:
            for k, t in enumerate(l[j - 1][:-1]):
                endPos = y.find(s, t + 1)
                if endPos != -1:
                    l[j].append(min(endPos, l[j - 1][k + 1]))
                else:
                    l[j].append(l[j - 1][k + 1])
            endPos = y.find(s, l[j - 1][-1] + 1)
            if endPos != -1:
                l[j].append(endPos)

    print(len(l[-1]) - 1)
### BUGGY CODE ###

from itertools import permutations

n, m, r = map(int, input().split())
A = list(map(int, input().split()))
d = [[1e18]*n for i in range(n)]
for i in range(m):
        a, b, c = map(int, input().split())
        d[a-1][b-1] = d[b-1][a-1] = c

for k in range(n):
        for i in range(n):
                for j in range(n):
                        d[i][j] = min(d[i][j], d[i][k]+d[k][j])


ans = 1e18
for x in permutations(A):
        l = 0
        for i in range(r-1):
                l += d[x[i]-1][x[i+1]-1]
        ans = min(ans, l)
print(ans)

### FIXED CODE ###

from itertools import permutations

n, m, r = map(int, input().split())
A = list(map(int, input().split()))
d = [[1e18]*n for i in range(n)]
for i in range(m):
        a, b, c = map(int, input().split())
        d[a-1][b-1] = d[b-1][a-1] = c

for k in range(n):
        for i in range(n):
                for j in range(n):
                        d[i][j] = min(d[i][j], d[i][k]+d[k][j])


ans = 1e18
for x in permutations(A):
        l = 0
        for i in range(r-1):
                l += d[x[i]-1][x[i+1]-1]
        ans = min(ans, l)
print(ans)