Harryxun/stf_run huggingface dataset:
import bisect
INF = 10 ** 10
s = input()
t = input()
moji = [[] for _ in range(26)]

for i, m in enumerate(s):
    moji[ord(m) - ord("a")].append(i)

for i in range(26):
    moji[i].sort()

moji_o = [0 for _ in range(len(t))]

cnt = 0
ans = 1
# print(moji)
for i, m in enumerate(t):
    if len(moji[ord(m) - ord("a")]) == 0:
        ans = -1
        break
    if i == 0:
        moji_o[i] = moji[ord(m) - ord("a")][0]
    else:
        if moji_o[i - 1] > moji[ord(m) - ord("a")][-1]:
            moji_o[i] =  moji[ord(m) - ord("a")][0]
        else:
            tmp = bisect.bisect_right(moji[ord(m) - ord("a")], moji_o[i - 1])
            if tmp == 0:
                moji_o[i] = moji[ord(m) - ord("a")][0]
            else:
                moji_o[i] = moji[ord(m) - ord("a")][tmp]
# print(moji_o)
for i in range(1, len(moji_o)):
    if ans == -1:
        break
    if moji_o[i - 1] >= moji_o[i]:
        cnt += 1

if ans == -1:
    print(-1)
else:
    print(cnt * len(s) + moji_o[-1] + 1)

### FIXED CODE ###

import bisect
INF = 10 ** 10
s = input()
t = input()
moji = [[] for _ in range(26)]

for i, m in enumerate(s):
    moji[ord(m) - ord("a")].append(i)

for i in range(26):
    moji[i].sort()

moji_o = [0 for _ in range(len(t))]

cnt = 0
ans = 1
# print(moji)
for i, m in enumerate(t):
    if len(moji[ord(m) - ord("a")]) == 0:
        ans = -1
        break
    if i == 0:
        moji_o[i] = moji[ord(m) - ord("a")][0]
    else:
        if moji_o[i - 1] >= moji[ord(m) - ord("a")][-1]:
            moji_o[i] =  moji[ord(m) - ord("a")][0]
        else:
            tmp = bisect.bisect_right(moji[ord(m) - ord("a")], moji_o[i - 1])
            if tmp == 0:
                moji_o[i] = moji[ord(m) - ord("a")][0]
            else:
                moji_o[i] = moji[ord(m) - ord("a")][tmp]
# print(moji_o)
for i in range(1, len(moji_o)):
    if ans == -1:
        break
    if moji_o[i - 1] >= moji_o[i]:
        cnt += 1

if ans == -1:
    print(-1)
else:
    print(cnt * len(s) + moji_o[-1] + 1)
a,b = map(int,input(),sprit())
print(a*b,2*(a+b))


### FIXED CODE ###

a,b = map(int,input().split())
print(a*b,2*(a+b))
n = int(input())
count = 0
for i in range(n):
    x = int(input())
    if (x % i == 0):
        break
    else:
        count += 1
print(count)

### FIXED CODE ###

n = int(input())
count = 0
for i in range(n):
    x = int(input())
    if (x == 2):
        count += 1
    elif (x % 2 == 0):
        continue
    else:
        if (pow(2, x - 1, x) == 1):
            count += 1
print(count)
s = input()
ans = 'WA'
if s[0] == 'A' and s[2:-1].count('C') == 1\
   s.replace('A', '').replace('C', '').islower():
  ans = 'AC'
print(ans)nn

### FIXED CODE ###

s = input()
ans = 'WA'
if s[0] == 'A' and s[2:-1].count('C') == 1\
   and s.replace('A', '').replace('C', '').islower():
  ans = 'AC'
print(ans)
n,q=map(int,input().split())
s=input()
count=[]*n
for i in range(1,n):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":
        count[i]+=1

for i in range(q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])


### FIXED CODE ###

N,Q=map(int,input().split())
S=input()
count=[0]*N
for i in range(1,N):
    count[i]+=count[i-1]
    if S[i-1:i+1]=="AC":count[i]+=1
for i in range(Q):
    l,r=map(int,input().split())
    print(count[r-1]-count[l-1])
# this is cumulative
n = int(input())
s = input()

point = 0
cum_sum = []

for i in s:
    if i == "I":
        point += 1
    else:
        point -= 1
    cum_sum.append(point)
else:
    print(max(cum_sum,0))
    


### FIXED CODE ###

n = int(input())
s = str(input())

num = 0
res = 0

for i in range(len(s)):
  if s[i] == 'I':
    num += 1
  else:
    num -= 1
  if num > res:
    res = num

print(res)
N,M = map(int,input().split())
lst = list(map(int,input().split()))
plus = 0
for a in range(lst):
    plus += a
if plus <= N:
    print(N-plus)
else:
    print('-1')

### FIXED CODE ###

N,M = map(int,input().split())
lst = list(map(int,input().split()))
plus = 0
for a in lst:
    plus += a
if plus <= N:
    print(N-plus)
else:
    print('-1')
N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
cnt = 0

ai = A[0]
ai2 = A[1]

for i in range(N):

  bi = B[i]
  
  if ai <= bi:
    cnt += ai
    
    if ai2 <= (bi - ai):
      cnt += ai2
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
    else:
      cnt += (bi - ai)
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
      
  else:
    cnt += bi
    ai = A[i+1]
    ai2 = A[i+2]
  
print(int(cnt))

### FIXED CODE ###

N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
cnt = 0

A.append(0)
A.append(0)

ai = A[0]
ai2 = A[1]

for i in range(N):

  bi = B[i]
  
  if ai <= bi:
    cnt += ai
    
    if ai2 <= (bi - ai):
      cnt += ai2
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
    else:
      cnt += (bi - ai)
      ai = max(0, ai2-(bi - ai))
      ai2 = A[i+2]
      
  else:
    cnt += bi
    ai = A[i+1]
    ai2 = A[i+2]
  
print(int(cnt))
int(x)*3

### FIXED CODE ###

x=int(input())
print(x**3)
import fractinons
K=int(input())
ans=0
for a in range(1,K+1):
	for b in range(1,K+1):
		for c in range(1,K+1):
			ans+=fractions.gcd(fractions.gcd(a,b),c)
print(ans)

### FIXED CODE ###

import math
K=int(input())
ans=0
for a in range(1,K+1):
	for b in range(a,K+1):
		for c in range(b,K+1):
			A=math.gcd(math.gcd(a,b),c)
			if a==b and b==c:
				ans+=A
			elif a==b or b==c:
				ans+=(A*3)
			else:
				ans+=(A*6)
print(ans)
import sys
sys.setrecursionlimit(10 ** 6)

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

for _ in range(m):
    a, b = map(int, input().split())
    e[a-1].append(b-1)

c = [0] * n
ans = []
def search_path(p, temp):
    c[p] = 1
    for x in e[p]:
        if x in temp:
            return temp[temp.index(x):]
        else:
            ret = search_path(x, temp + [x])
            if len(ret) != 0:
                return ret
    return []

t = 1
for i in range(n):
    if c[i] == 0:
        ans = search_path(0, [0])
        if len(ans) != 0:
            break

if len(ans) == 0:
    print(-1)
    exit()

check = 1
while check == 1:
    check = 0
    for i in range(len(ans)-1):
        for x in e[ans[i]]:
            #print(x, ans[:i])
            if x in ans[:i]:
                ans = [] + ans[:i+1]
                check = 1
    for x in e[ans[-1]]:
        if x in ans[1:len(ans)]:
            ans = [] + ans[ans.index(x):]
            check = 1

print(len(ans))
for x in ans:
    print(x+1)




### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 6)

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

for _ in range(m):
    a, b = map(int, input().split())
    e[a-1].append(b-1)

c = [0] * n
ans = []
def search_path(p, temp):
    c[p] = 1
    for x in e[p]:
        if x in temp:
            print(-99999)
            exit()
        else:
            search_path(x, temp + [x])


t = 1
for i in range(n):
    if c[i] == 0:
        ans = search_path(i, [i])

print(-1)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys

def read_data():
    lines = [line.rstrip() for line in sys.stdin]
    ns = [int(n) for n in lines[1].split()]
    ms = [(int(line.split()[0]), int(line.split()[1])) for line in lines[3:]]
    return ns, ms

def main():
    ns, ms = read_data()
    for (drink_num, drink_sec) in ms:
        result = sum(ns) - ns[drink_num-1] + drink_sec
        print result


if __name__ == "__main__":
    main()

### FIXED CODE ###

#!/usr/bin/env python
# -*- coding:utf-8 -*-

def main():
    a, op, b = raw_input().split()
    if op == '+':
        result = int(a) + int(b)
    else:
        result = int(a) - int(b)
    print result


if __name__ == "__main__":
    main()
l = [str(input()) for i in range(5)]
dct = {}
total = 0
a = []
b = []
c = []
for n in l:
    dct[n] = n[-1]
for k, v in sorted(dct.items(), key=lambda x: x[1], reverse=True):
    if int(v) == 0:
        b.append([int(k), int(v)])
    else:
        a.append([int(k), int(v)])
c.append(a[-1]) 
a = a[:-1]
for i in a:
    total += i[0] + 10 - i[1]
for i in b:
    total += i[0]
total += c[0][0]
print(total)

### FIXED CODE ###

l = [int(input()) for i in range(5)]
a = []
b = []
c = []
total = 0
for i in l:
    if i % 10 == 0:
        a.append(i)
    else:
        b.append(i)
minNum = 10
ans = 0
for i in b:
    if i % 10 < minNum:
        minNum = i % 10
        ans = i
if b:
    c.append(ans)
    b.remove(ans)
if a:
    for i in a:
        total += i
if b:
    for i in b:
            total += i + (10 - i % 10)
if c:
    total += c[0]
print(total)
S=int(input())
l=[]
while(S not in l):
    l.append(S)
    if S%2==0:S/=2
    else:S=3*S+1
print(len(l)+1)

### FIXED CODE ###

S={c:list(input()) for c in "abc"}
s="a"
while S[s]:s=S[s].pop(0)
print(s.upper())
S = input()
for i in range(len(S)):
    if S[i] == "A" or "C" or "G" or "T":
        for j in range(i+1,len(S)):
            if S[j] == "A" or "C" or "G" or "T":
                continue
            else:
                tmp = j-i
                break
print(tmp)

### FIXED CODE ###

S = input()
ans = 0
acgt = ["A","C","G","T"]
for i in range(len(S)):
    tmp = 0
    if S[i] in acgt:
        tmp += 1
        for j in range(i+1,len(S)):
            if S[j] in acgt:
                tmp += 1
                continue
            else:
                ans = max(ans,tmp)
                break
        ans = max(ans,tmp)
print(ans)
for i in range(1, 10):
  for k in range(1,10):
    if N == i * k:
      print("Yes")
      sys.exit()
print("No")

### FIXED CODE ###

import sys
N = int(input())

for i in range(1, 10):
  for k in range(1,10):
    if N == i * k:
      print("Yes")
      sys.exit()

print("No")
s = [list(input()) for i in range(3)]
turn = s[0]
for i in range(300):
    if "a" == turn.pop(0):
        turn = s[0]
    elif "b" == turn.pop(0):
        turn = s[1]
    else:
        turn = s[2]
    
    if not s[0]:
        print("A")
        exit()
    if not s[1]:
        print("B")
        exit()
    if not s[2]:
        print("C")
        exit()

### FIXED CODE ###

ABC = "ABC"
s = [input()+ABC[i] for i in range(3)]
turn = s[0]
card = 0
count = [1, 0, 0]

for i in range(305):
    if turn[card] == "a":
        turn = s[0]
        card = count[0]
        count[0] += 1
    elif turn[card] == "b":
        turn = s[1]
        card = count[1]
        count[1] += 1
    else:
        turn = s[2]
        card = count[2]
        count[2] += 1
    
    if turn[card] == "A":
        print("A")
        exit()
    if turn[card] == "B":
        print("B")
        exit()
    if turn[card] == "C":
        print("C")
        exit()
import copy
import numpy as np

N = int(input())
A = np.array([input() for i in range(N)])

B = np.array([])
for i in range(N):
    B = copy.copy(A)
    np.delete(B, i)
    print(max(B)

### FIXED CODE ###

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

a = max(A)
b = sorted(A)[-2]
for i in A:
    print(b if i == a else a)
A,B,C = list(map(int,input().split()))

ans = 0

for i in range(1,B+1):
  if A*i % B == C:
    ans = 1

if ans == 1:
  print(YES)
else :
  print(NO)

### FIXED CODE ###

A,B,C = list(map(int,input().split()))
 
ans = 0
 
for i in range(1,B+1):
  if A*i % B == C:
    ans = 1
 
if ans == 1:
  print("YES")
else :
  print("NO")
import sys
li = [list(map(int,input().split())) for i in range(3)]
n = int(input())
cnt = 0
for i in range(2):
    for j in range(2):
        for k in int(input()):
            if li[i][j] == k:
                li[i][j] == -1
            else:
                pass

for a in range(2):
    if li[a][0] == li[a][1] == li[a][2] == -1:
        print("Yes")
        sys.exit()
    else:
        pass
for b in range(2):
    if li[0][[b] == li[1][[b] == li[2][[b] == -1:
        print("Yes")
        sys.exit()
    else:
        pass
if li[0][[0] == li[1][[1] == li[2][[2] == -1:
    print("Yes")
    sys.exit()
else:
    pass
if li[0][[2] == li[1][[1] == li[2][[0] == -1:
    print("Yes")
    sys.exit()
else:
    print("No")

### FIXED CODE ###

import sys
li = [list(map(int,input().split())) for i in range(3)]
n = int(input())
k = [int(input()) for i in range(n)]
cnt = 0
for i in range(3):
    for j in range(3):
        if li[i][j] in k:
            li[i][j] = -1
        else:
            pass
for a in range(3):
    if li[a][0] == li[a][1] == li[a][2] == -1:
        print("Yes")
        exit()
    else:
        pass
for b in range(3):
    if li[0][b] == li[1][b] == li[2][b] == -1:
        print("Yes")
        exit()
    else:
        pass
if li[0][0] == li[1][1] == li[2][2] == -1:
    print("Yes")
    exit()

elif li[0][2] == li[1][1] == li[2][0] == -1:
    print("Yes")
    exit()
else:
    print("No")
X = map(int, input())

num_500 = X // 500
num_5 = (X - (X // 500)*500 ) % 5

print(1000*num_500 + 5 * num_5)

### FIXED CODE ###

X = int(input())
 
num_500 = X // 500
num_5 = (X - (X // 500) * 500 ) // 5
 
print(1000*num_500 + 5 * num_5)
n = int(input())

max_index = 0
max_price = 0
price_list = []

for i in range(0, n + 1):
    p = int(input())
    price_list.append(p)
    if max_price < p:
        max_index = i

sum = 0
for i, p in enumerate(price_list):
    if i == max_index:
        sum += p / 2
    else:
        sum += p

print(sum)


### FIXED CODE ###

n = int(input())

max_index = 0
max_price = 0
price_list = []

for i in range(0, n):
    p = int(input())
    price_list.append(p)
    if max_price < p:
        max_price = p
        max_index = i

p_sum = 0
for i, p in enumerate(price_list):
    if i == max_index:
        p_sum += p / 2
    else:
        p_sum += p

print(int(p_sum))
import math 

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

A,B,C,D= map(int,input().split())
lcm = lcm(C,D)
E = (A-1)//C
F = (A-1)//D
G = (A-1)//lcm
H = B//C
I = B//D
J = B//lcm

count = (B-A+1)-((H-E)+(I-F)-(J-G))

print(count)

### FIXED CODE ###

import fractions

def lcm(x,y):
	return (x*y)//fractions.gcd(x,y)
count = 0
A,B,C,D= map(int,input().split())
lcm = lcm(C,D)
E = (A-1)//C
F = (A-1)//D
G = (A-1)//lcm
H = B//C
I = B//D
J = B//lcm

count = (B-A+1)-((H-E)+(I-F)-(J-G))

print(count)
N = int(input())
# t, x, y = 0, 0, 0
for __ in range(N):
    t, x, y = map(int, input().split())
    if (x + y) > t or (x + y + t) % 2 != 0:
        print("No")
        sys.exit()

print("Yes")

### FIXED CODE ###

t, x, y = 0, 0, 0
for i in range(int(input())):
    next_t, next_x, next_y = map(int, input().split())
    diff = abs(x - next_x) + abs(y - next_y)
    if diff > (next_t - t) or (diff - (next_t - t)) % 2 == 1:
        print("No")
        exit(0)
    t, x, y, = next_t, next_x, next_y
print("Yes")
K,N=int(map(int,input().split()))
A=list(map(int,input().split()))
m=K
for j in range(len(A)):
  m=min(m,min(K-abs(A[j]-A[j-1]),K-abs(A[j]-A[j+1])))
print(m)
              
                                              


### FIXED CODE ###

K,N=map(int,input().split())
A=list(map(int,input().split()))
m=K
for i in range(N):
  if i==0:
    m=min(m,A[N-1]-A[0])
  else:
    b=A[N-1]-A[i]+K-A[N-1]+A[i-1]
    m=min(m,b)
print(m)
N,X=map(int,input().split())
L=[int(a) for a in input().split()]

temp=0
cnt=0
for i in range(N+1):
    if temp>X:
        break
    else:
        temp+=L[i]
        cnt+=1
   
print(cnt)

### FIXED CODE ###

N,X=map(int,input().split())
L=[int(a) for a in input().split()]

d=0
cnt=0
for i in range(N+1):
    if d>X:
        break
    elif i==N:
        cnt+=1
    else:
        d+=L[i]#next
        cnt+=1
   
print(cnt)
a, b = int(_) for _ in input().split()
if a - 2 > b:
  print(a*2)
elif b - 2 > a:
  print(b*2)
else:
  print(a+b)

### FIXED CODE ###

a, b = (int(_) for _ in input().split())
if a - 2 >= b:
  print(a*2 -1)
elif b - 2 >= a:
  print(b*2 -1)
else:
  print(a+b)
= map(int, input().split())

ans = 1
while(True):
    n = n//k
    if(n == 0):
        break
    ans += 1

print(ans)


### FIXED CODE ###

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

ans = 1
while(True):
    n = n//k
    if(n == 0):
        break
    ans += 1

print(ans)
from itertools import accumulate
from sys import stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
P = [int(_) for _ in stdin.readline().rstrip().split()]
evs = [(1+p)/2 for p in P]
evs = list(accumulate(evs))
results = [evs[i+K] - evs[i] for i, e in enumerate(evs[:N-K])]
print(sorted(results)[-1])

### FIXED CODE ###

from itertools import accumulate
from sys import stdin
N, K = [int(_) for _ in stdin.readline().rstrip().split()]
P = [int(_) for _ in stdin.readline().rstrip().split()]
evs = [(1+p)/2 for p in P]
evs = [0] + list(accumulate(evs))
ans = 0
for i in range(K, N+1):
    ans = max(ans, evs[i]-evs[i-K])
print(ans)
import sys
def 解():
    input = sys.stdin.readline
    H,W,N = [int(_) for _ in input().split()]
    dT={}
    if 0 < N  :
        for  _ in range(N):
            x,y = [int(_) - 1 for _ in input().split()]
            if x >= y:
                系列 = x-y
                if 系列 in dT:
                    if x + y < dT[系列][2] :
                        dT[系列] = (x,y,x+y)
                else:
                    dT[系列] = (x,y,x+y)
        aB = sorted(dT.items())
        iL = len(aB)
        if iL == 1:
            高橋,青木,距離 = aB[0][1]
        else:
            #入力例2
            #[(0, (1, 1, 2)), (1, (3, 2, 5)), (2, (5, 3, 8)), (4, (6, 2, 8)), (7, (7, 0, 7))]
            aG= [aB[0][1]]
            for i in range(1,iL):
                if aB[i][0]-1 == aB[i-1][0]:
                    if aB[i][1][2] < aB[i-1][1][2] :
                        aG.append(aB[i][1])
                else:
                    aG.append(aB[i][1])
            if 0 < len(aG) and  aG[0][0] - aG[0][1] == 0 :
                del aG[0]
            if 0 < len(aG) :
                高橋,青木,距離 = sorted(aG,key=lambda x:x[0])[0]
            else:
                高橋 = H
        print(高橋)
    else:
        print(H)
解()


### FIXED CODE ###

import sys
def 解():
    input = sys.stdin.readline
    H,W,N = [int(_) for _ in input().split()]
    dT={}
    高橋 = H
    if 0 < N  :
        for  _ in range(N):
            x,y = [int(_) - 1 for _ in input().split()]
            if x >= y:
                系列 = x-y
                if 系列 in dT:
                    if x + y < dT[系列][2] :
                        dT[系列] = (x,y,x+y)
                else:
                    dT[系列] = (x,y,x+y)
        aB = sorted(dT.items())
        iL = len(aB)
        if iL == 1:
            高橋,青木,距離 = aB[0][1]
        else:
            #入力例2
            #[(0, (1, 1, 2)), (1, (3, 2, 5)), (2, (5, 3, 8)), (4, (6, 2, 8)), (7, (7, 0, 7))]
            if 0 < len(aB) :
                aG= [aB[0][1]]
                for i in range(1,iL):
                    if aB[i][0]-1 == aB[i-1][0]:
                        if aB[i][1][2] < aB[i-1][1][2] :
                            aG.append(aB[i][1])
                    else:
                        aG.append(aB[i][1])
                if 0 < len(aG) and  aG[0][0] - aG[0][1] == 0 :
                    del aG[0]
                if 0 < len(aG) :
                    高橋,青木,距離 = sorted(aG,key=lambda x:x[0])[0]
    print(高橋)
解()
a,b = map(int, input().split())
if a > b:
  print(2a - 1)
elif a == b:
  print(2a)
else:
  print(2b -1)

### FIXED CODE ###

a, b = map(int, input().split())
if a > b :
  print(a + a -1)
elif a == b:
  print(a + b)
else:
  print(b + b - 1)
N = int(input())
A = [0] * 5
A[0] = int(input())
A[1] = int(input())
A[2] = int(input())
A[3] = int(input())
A[4] = int(input())
print(math.ceil(N / min(A)) * (mi + 1) + 4)

### FIXED CODE ###

import math
N = int(input())
A = [0] * 5
A[0] = int(input())
A[1] = int(input())
A[2] = int(input())
A[3] = int(input())
A[4] = int(input())
print(math.ceil(N / min(A)) + 4)
w, h = x,y = map(int,input().split())

if w * h % 2 == 0:
  print(w * h // 2)
else:
  print(w * h // 2 + 1)

### FIXED CODE ###

w, h = map(int,input().split())
 
if w == 1 or h == 1:
  print(1)
  exit()
  
if (w * h) % 2 == 0:
  print(w * h // 2)
else:    
  a = w // 2 + 1
  s = a * (h // 2 + 1)
  b = w // 2
  s2 = b * (h //2)
  print(s + s2)
#https://qiita.com/vain0x/items/fbde8b66927b84080811
#http://kmjp.hatenablog.jp/entry/2019/09/01/1000
#2人の選手が互いに相手を次の対戦相手に指定しているとき以外組めない
#したがって貪欲で一意
from collections import deque
import sys

n = int(input())
a = []

for i in range(n):
    ai = list(map(int, input().split( )))
    ai.reverse()
    for j in range(n-1):
        ai[j]-=1
    a.append(ai)

Q = deque()
for i in range(n):
    Q.append(i)

#nは番兵
Q.append(n)
Done = set()
cnt = 1
while len(Q)>1:
    i = Q.popleft()
    if i==n:
        cnt += 1
        Q.append(n)
        Done=set()###
    if i<n and a[i]:
        p = a[i].pop()
        if p not in Done:##この判定しないと1日に複数回試合することになる
            if a[p][-1]==i and i not in Done:###
                Q.append(i)
                Q.append(p)
                a[p].pop()
                Done.add(p)###
                Done.add(i)
            else:#戻す
                a[i].append(p)
        # else:
        #     Done.remove(p)
        
for i in range(n):
    if len(a[i])>0:
        print(-1)
        sys.exit()
        
print(cnt)
        
        
        


### FIXED CODE ###

#https://qiita.com/vain0x/items/fbde8b66927b84080811
#http://kmjp.hatenablog.jp/entry/2019/09/01/1000
#2人の選手が互いに相手を次の対戦相手に指定しているとき以外組めない
#したがって貪欲で一意

n = int(input())
a = []

for i in range(n):
    ai = list(map(int, input().split( )))
    ai.reverse()
    for j in range(n-1):
        ai[j]-=1
    a.append(ai)

##キューの管理のどこがおかしいかわからなかったがあきらめる

can={i for i in range(n)}
nxt=set()
cnt = 0
rem=n*(n-1)//2
while rem:
    cnt+=1
    for i in can:
        if i not in nxt and a[i]:
            p = a[i][-1]
            if p not in nxt:##この判定しないと1日に複数回試合することになる
                if a[p][-1]==i:
                    a[p].pop();a[i].pop()
                    nxt.add(i);nxt.add(p)
    rem-=len(nxt)//2

    can=nxt.copy()###copy
    nxt=set()
    if not can:
        break
       
if rem:
    print(-1)
else:       
    print(cnt)
S = list(input())
s = len(S)
K = int(input())
A = []
j = 1

for i in range(0,s-1,1):
  if S[i] == S[i+1]:
    j += 1
  else:
    A.append(j)
    j = 1
A.append(j)

a = len(A)

b = 0
for i in range(0,a,1):
  b += A[i]//2
  
x = b*K

if S[s-1] == S[0] and A[a-1] % 2 == 1 and A[1] % 2 == 1:
  x += K-1

print(x)

### FIXED CODE ###

S = list(input())
s = len(S)
K = int(input())
A = []
j = 1

for i in range(0,s-1,1):
  if S[i] == S[i+1]:
    j += 1
  else:
    A.append(j)
    j = 1
A.append(j)

a = len(A)

b = 0
for i in range(0,a,1):
  b += A[i]//2
  
x = b*K

if a == 1:
  x = (s*K)//2
elif S[s-1] == S[0] and A[a-1] % 2 == 1 and A[0] % 2 == 1:
  x += K-1

print(x)