Harryxun/stf_run huggingface dataset:
n, k = map(int, input().split()) 
a_list = list(map(int, input().split())) 

rireki = [0] * (n+1)
curr_c = 1
rireki[1] = 0

no_loop_count = k
loop_cnt = 0

for i in range(k):
    next_c = a_list[curr_c-1]
    # print(i, curr_c, next_c)
    if next_c == 1:
        no_loop_count = 0
        loop_cnt = i + 1
        break
    elif rireki[next_c] == 0:
        rireki[next_c] = i+1
    else:
        # print(next_c)
        no_loop_count = rireki[next_c] -1
        loop_cnt = i - no_loop_count
        break
    curr_c = next_c


if k <= no_loop_count:
    ans = rireki.index(k)
else:
    # print('k',k)
    # print('no', no_loop_count)
    # print('lo', loop_cnt)
    ind = (k-1-no_loop_count)%loop_cnt +1
    rireki_fix = [i-no_loop_count for i in rireki]
    # print(ind, 'ind')
    ans = rireki_fix.index(ind)
    # print(rireki_fix)

print(ans)

### FIXED CODE ###

def main():
    n, k = map(int, input().split()) 
    al = list(map(int, input().split())) 
    city_l = [1]
    city_s = set([1])
    loop_city_l = []
    curr_city = 1
    cnt = 0
    for i in range(1,k+1):
        next_c = al[curr_city-1]
        if next_c in city_s:
            cnt = i
            first_city = city_l.index(next_c)
            loop_city_l = city_l[first_city:]
            break
        else:
            city_s.add(next_c)
            city_l.append(next_c)
        curr_city = next_c
    else:
        print(curr_city)
        return

    # print(loop_city_l)
    remained = k-cnt
    last_pos = remained%len(loop_city_l)
    print(loop_city_l[last_pos])

if __name__ == "__main__":
    main()
# encoding: utf-8

import sys
from collections import deque


class Solution:
    @staticmethod
    def message_queue():
        # write your code here
        # tasks = deque(map(lambda x: x.split(), sys.stdin.readlines()))
        task_num, task_time = map(int, input().split())

        _input = sys.stdin.readlines()


        # task_deque = deque([{'name': i.split()[0], 'time': int(i.split()[1])} for i in _input])
        task_deque = deque(map(lambda x: dict(name=x.split()[0], time=x.split()[-1]), _input))
        total_time = 0

        while task_deque:
            item = task_deque.popleft()
            if item['time'] <= task_time:
                total_time += item['time']
                print(item['name'], total_time)
            else:
                item['time'] -= task_time
                total_time += task_time
                task_deque.append(item)


if __name__ == '__main__':
    solution = Solution()
    solution.message_queue()

### FIXED CODE ###

# encoding: utf-8

import sys
from collections import deque

input_ = sys.stdin.readlines()


def message_queue():
    # write your code here
    n, shard = map(int, input_[0].split())

    task_deque = deque(map(lambda x: x.split(), input_[1:]))
    t_accu = 0

    while task_deque:
        t_name, t_time = task_deque.popleft()
        t_time = int(t_time)
        if t_time <= shard:
            t_accu += t_time
            print(t_name, t_accu)
        else:
            t_time -= shard
            t_accu += shard
            task_deque.append([t_name, t_time])


message_queue()
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def digit_sum(num:int, base:int) -> int:
    if num < base:
        return num
    else:
        return digit_sum(int(num/base), base) + (num % base)

from collections import deque

k = ni()
que = deque([1])

kmod = [0]*k

while kmod[0] == 0:
    cur = que.popleft()
    if kmod[cur%k] == 0:
        kmod[cur%k] = digit_sum(cur,10)
        
        if kmod[(10*cur)%k] == 0:
            que.appendleft(10*cur)
        if kmod[(cur+1)%k] == 0:
            que.append(cur+1)
            
        
print(kmod[0])

### FIXED CODE ###

from collections import deque

def bfs(K:int) -> list:
    hasChecked = [False]*(K+1)
    cost_list = [0]*(K+1)
    cost_list[1] = 1
    
    que = deque([(cost_list[1], 1)])
    
    while que:
        cost, res = que.popleft()
        if hasChecked[res]:
            continue
        
        hasChecked[res] = True
        cost_list[res] = cost
        
        if not hasChecked[10*res%K]:
            que.appendleft((cost, 10*res%K))
            
        if not hasChecked[(res+1)%K]:
            que.append((cost+1, (res+1)%K))
            
            
    return cost_list


K = int(input())
cost_list = bfs(K)
print(cost_list[0])
a=list(map(int,input().sprit()))
print(min(a[0]*a[1],a[2]))

### FIXED CODE ###

a=list(map(int,input().split()))
print(min(a[0]*a[1],a[2]))
A,B,C = map(int,input.split())

if A == B == C:
  print("No")
else:
  print("Yes")

### FIXED CODE ###

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

if A == B == C:
  print("No")
elif A !=B and A != C and B != C:
  print("No")
else:
  print("Yes")
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N = int(input())

    ans = 0
    for _ in range(N):
        x, u = input().split()
        x = float(x)
        if u == "JPY":
            ans += x
        else:
            ans += x * 380000

    print(f"{ans:6f}")


if __name__ == '__main__':
    main()


### FIXED CODE ###

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N = int(input())

    ans = 0
    for _ in range(N):
        x, u = input().split()
        x = float(x)
        if u == "JPY":
            ans += x
        else:
            ans += x * 380000

    print(ans)


if __name__ == '__main__':
    main()
N = int(input())
card = {}
for i in range(N):
  S = input()
  card[S] = card.get(S, 0) + 1

M = int(input())
for i in range(M):
  S = input()
  card[S] = card.get(S, 0) - 1
  
print(max(max(list(card.values()),0))

### FIXED CODE ###

N = int(input())
card = {}
for i in range(N):
  S = input()
  card[S] = card.get(S, 0) + 1
 
M = int(input())
for i in range(M):
  S = input()
  card[S] = card.get(S, 0) - 1

tmp = list(card.values())
tmp.append(0)
print(max(tmp))
a = [0 for i in range(26)]
cnt = 0
while 1:
    S = input()
    if not S:
        break
    for i in S:
        if 65 <= ord(i) and ord(i) <= 90\
        or 97 <= ord(i) and ord(i) <= 122:
            if 65 <= ord(i) and ord(i) <= 90:
                cnt = ord(i) - ord("A")
            else:
                cnt = ord(i) - ord("a")
            a[cnt] += 1
            cnt = 0
for i in range(26):
    print(chr(i + 97), ":", a[i])

### FIXED CODE ###

import sys
cnts = {}
for line in sys.stdin:
    for c in list(line.lower()): cnts[c] = cnts.get(c,0) + 1
  
for c in range(ord('a'),ord('z')+1): print(chr(c),':',cnts.get(chr(c),0))
N=int(input())
sum=0
for i range(N):

### FIXED CODE ###

n=int(input())
sum=0
for i in range(n):
  l, r =map(int, input().split())
  sum+=(r-l+1)
print(sum)
import numpy as np

n = int(input())
a = []
lcm = 1
for i in range(n):
  t = int(input())
  a.append(t)
  
lc = np.lcm.reduce(a)

res = 0;
for i in range(n):
  res = res + lc / a[i];
  res = res % 1000000007

print(res)


### FIXED CODE ###

import fractions
def LCM(a, b):
    return(a // fractions.gcd(a, b) * b)

n = int(input())
a = list(map(int, input().split()))
lc = 1;

for i in range(n):
    lc = LCM(lc, a[i])

res = 0;
for i in range(n):
    res = res + (lc // a[i])

print(res % 1000000007)
# Acceptance of input

import sys

f_i = sys.stdin

n = int(f_i.readline())

edges = [[None] * n for i in range(n)]

for l_i in f_i:
    s, t, w = map(int, l_i.split())
    edges[s][t] = w
    edges[t][s] = w

# Tree Walk and Output

distance = [0] * n

def tree_walk(u, d, p):
    for v, e in enumerate(edges[u]):
        if e != None and v != p:
            c_d = d + e
            distance[v] = c_d
            tree_walk(v, c_d, u)

tree_walk(0, 0, None)

end_point = distance.index(max(distance))

tree_walk(end_point, 0, None)

print(max(distance))

### FIXED CODE ###

# Acceptance of input

import sys

f_i = sys.stdin

n = int(f_i.readline())

edges = [[] for i in range(n)]

for l_i in f_i:
    s, t, w = map(int, l_i.split())
    edges[s].append((t, w))
    edges[t].append((s, w))

# Tree Walk function

import collections

def tree_walk(start, switch = 0):
    path = collections.deque()
    path.append(start)

    weight = collections.deque()
    weight.append(0)
    
    unvisited = [True] * n
    unvisited[start] = False
    
    rest = n - 1
    end_point = start
    diameter = 0
    
    while True:
        u = path[-1]
        for v, w in edges[u]:
            if unvisited[v]:
                path.append(v)
                unvisited[v] = False
                rest -= 1
                weight.append(w)
                break

        if u == path[-1]:
            distance = sum(weight)
            if diameter < distance:
                diameter = distance
                end_point = u
            if rest == 0:
                break
            path.pop()
            weight.pop()

    if switch:
        print(diameter)
    else:
        return end_point

# Output

tree_walk(tree_walk(0), 1)
n = int(input())
f = []
p = []
for _ in range(n):
  f.append(list(map(int, input().strip().split())))
for _ in range(n):
  p.append(list(map(int, input().strip().split())))

maxt = None
for i in range(1, 2**10):
  total = 0
  for _f, _p in zip(f, p):
    c = 0
    for bi, fi in zip(bin(i)[2::-1], _f):
      if fi==1 and int(bi)==1:
        c += 1
    total += _p[c]
  maxt = max(total, maxt) if maxt is not None else total
print(maxt)


### FIXED CODE ###

n = int(input())
f = []
p = []
for _ in range(n):
  f.append(list(map(int, input().strip().split())))
for _ in range(n):
  p.append(list(map(int, input().strip().split())))

maxt = None
for i in range(1, 2**10):
  total = 0
  for _f, _p in zip(f, p):
    c = 0
    for bi, fi in zip(reversed(bin(i)[2:]), _f):
      if fi==1 and int(bi)==1:
        c += 1
    total += _p[c]
  maxt = max(total, maxt) if maxt is not None else total
print(maxt)
#ABC118D-MatchMatching.py

from sys import stdin
from functools import lru_cache

input = stdin.readline

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

A = list(map(int, input().split()))
A.sort(reverse=True)

B = [2,5,5,4,5,6,3,7,6]

def num(n):
    return B[n-1]

@lru_cache(maxsize=None)
def dp(i):
    if i == 0:
        return 0
    elif i < 0:
        return -float('inf')
    else:
        C = []
        for j in range(M):
            C.append(dp(i-num(A[j]))+1)
        return max(C)

s = ''
n = dp(N)
K = N

for i in range(n):
    for j in range(M):
        if dp(K-num(A[j])) == dp(K)-1:
            s += str(A[j])
            K -= B[A[j]-1]
            break

print(int(s))


### FIXED CODE ###

#ABC118D-MatchMatching.py

import sys
from functools import lru_cache

sys.setrecursionlimit(10000)

input = sys.stdin.readline

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

A = list(map(int, input().split()))
A.sort(reverse=True)

B = [2,5,5,4,5,6,3,7,6]

def num(n):
    return B[n-1]

@lru_cache(maxsize=None)
def dp(i):
    if i == 0:
        return 0
    elif i < 0:
        return -float('inf')
    else:
        C = []
        for j in range(M):
            C.append(dp(i-num(A[j]))+1)
        return max(C)

s = ''
n = dp(N)
K = N

for i in range(n):
    for j in range(M):
        if dp(K-num(A[j])) == dp(K)-1:
            s += str(A[j])
            K -= B[A[j]-1]
            break

print(int(s))
S = list(input())
cnt = 0
for i in range(1 << len(S)-1):
  f = S[0]
  for j, b in enumerate(format(i, 'b').zfill(len(S)-1)):
    if(b == '1'):
      f += '+'
    f += S[j+1]
  cnt += sum(map(int,f.split('+')))
print(cnt)

### FIXED CODE ###

S = list(input())
"""
if(len(S) == 1):
  print(S[0])
  exit()
cnt = 0
for i in range(1 << len(S)-1):
  f = S[0]
  for j, b in enumerate(format(i, 'b').zfill(len(S)-1)):
    if(b == '1'):
      f += '+'
    f += S[j+1]
  cnt += sum(map(int,f.split('+')))
print(cnt)

"""

cnt = 0
for i in range(1 << len(S)-1):
  f = S[0]
  for j in range(len(S)-1):
    if(((i >> j) & 1)== 1):
      f += '+'
    f += S[j+1]
  cnt += sum(map(int, f.split('+')))
print(cnt)
l,s = [],[]
for i in range(5):
    num = int(input())
    l.append(num)
    s.append(num%10)
res = 0
s = list(set(s))
if 0 in s:
    s.remove(0)
for i in l:
    if i%10!=0:
        res += (i+10)//10*10
    else:
        res += i
else:
    print(res-(10-min(s)))

### FIXED CODE ###

l,s = [],[]
for i in range(5):
    num = int(input())
    l.append(num)
    if num%10!=0:
        s.append(num%10)

res = 0

for i in l:
    if i%10!=0:
        res += (i+10)//10*10
    else:
        res += i
else:
    if len(s) != 0:
        tmp = min(s)
    else:
        tmp = 10
    print(res-(10-tmp))
K,T=map(int,input().split())
A=list(map(int,input().split()))
from heapq import heappush,heappop
from collections import deque

A.sort(reverse=True)
d=deque(A)
d2=deque(A)
ans=A[0]
mod=1
while d:
  if len(d)==1:
    print(d[0]-1)
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  if not b==1:
    d.appendleft(b-1)

    
  


### FIXED CODE ###

K,T=map(int,input().split())
A=list(map(int,input().split()))
from heapq import heappush,heappop
from collections import deque

A.sort(reverse=True)
d=deque(A)
d2=deque(A)
ans=A[0]
mod=1
while d:
  if len(d)==1:
    print(d[0]-1)
    exit()
  a=d.pop()
  b=d.popleft()
  if not a==1:
    d.append(a-1)
  if not b==1:
    d.appendleft(b-1)
print(0)
a,b,c = map(int,input().split())
print(c+' '+a+' '+b)


### FIXED CODE ###

a,b,c = map(str,input().split())
print(c+' '+a+' '+b)
def main():
  divisor = 10 * 9 + 7
  h, w = map(int, input().split())
  a = [input() for _ in range(h)]
  dp = [[0] * w for _ in range(h)]
  dp[0][0] = 1
  for i in range(h):
    ai = a[i]
    ai1 = a[i + 1]
    dpi = dp[i]
    dpi1 = dp[i + 1]
    for j in range(w):
      if i + 1 < h and ai1[j] != '#':
        dpi1[j] = (dpi1[j] + dpi[j]) % divisor
      if j + 1 < w and ai[j + 1] != '#':
        dpi[j + 1] = (dpi[j + 1] + dpi[j]) % divisor
  print(dp[h - 1][w - 1])
main()

### FIXED CODE ###

divisor = 10 ** 9 + 7
h, w = map(int, input().split())
a = [input() for _ in range(h)]
dp = [[0] * w for _ in range(h)]
dp[0][0] = 1
dp0 = dp[0]
a0 = a[0]
for j in range(1, w):
  if a0[j] != '#':
    dp0[j] = dp0[j - 1]
for i in range(1, h):
  dpi = dp[i]
  dpi1 = dp[i - 1]
  ai = a[i]
  if ai[0] != '#':
    dpi[0] = dpi1[0]
  for j in range(1, w):
    if ai[j] != '#':
      dpi[j] = (dpi1[j] + dpi[j - 1]) % divisor
print(dp[h - 1][w - 1])
import sys

l = [int(i) for i in sys.stdin]

N = l[0]

a = l[1:]

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)


### FIXED CODE ###

N = int(input())

a = []

for i in range(N):
    a.append(int(input()) - 1)

done = [False] * N

i = 0

ans = 0

while True:
    if done[i] == False:
        done[i] = True
        ans += 1
        if a[i] == 1:
            break
        i = a[i]
    else:
        ans = -1
        break

print(ans)
from __future__ import division, print_function
from sys import stdin

for line in stdin:
    a,b = map(int, line.split())
    if (a == 0 and b == 0):
        break
    if a > b:
       print b, a
    else:
       print a, b

### FIXED CODE ###

while True:
    line = map(int, raw_input().split())
    if line[0] == 0 and line[1] == 0:
        break
    line.sort()
    print line[0],line[1]
a,b=map(str,input().split())
n=int(a+b)
m==n**0.5
if int(m)-m==0:
  print('Yes')
else:
  print('No')


### FIXED CODE ###

a,b=map(str,input().split())
n=int(a+b)
m=n**0.5
if int(m)-m==0:
  print('Yes')
else:
  print('No')
N,M = map(int,input().split())

from1 = [False]*N
toN = [False]*N
for i in range(M):
  a,b = map(int,input().split())
  if a ==1 or b ==1:
    from1[max(a,b)]=True
  if a == N or b == N:
    toN[a if a!= N else b]=True
cnt =0 
for i in range(M):
  cnt+=from1[i]&toN[i]
print("POSSIBLE" if cnt else "IMPOSSIBLE")

### FIXED CODE ###

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

from1 = [False]*max(N,M)
toN = [False]*max(N,M)
for i in range(M):
  a,b = map(int,input().split())
  if a ==1 or b ==1:
    from1[a-1 if a!= 1 else b-1]=True
  if a == N or b == N:
    toN[a-1 if a!= N else b-1]=True
cnt =0 
for i in range(M):
  cnt+=from1[i]&toN[i]
print("POSSIBLE" if cnt else "IMPOSSIBLE")
N=int(input())
 
X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])
 
 
def ceil(x):
  if int(x)==x:
    return int(x)
  else:
    return int(x//1+1)
 
 
a=X[0][0]
b=X[0][1]
 
for i in range(1,N):
  c=X[i][0]
  d=X[i][1]
 
 
  #print("i = "+str(i)+" a = "+str(a)+" b = "+str(b)+" c = "+str(c)+" d = "+str(d))
  x=ceil(a/c)
  y=ceil(b/d)
  
 
  if d*x-b>=0:
    a=c*x
    b=d*x

    t=a+b

 
  
  if c*y-a>=0:

    if c*y+d*y<t:
      a=c*y
      b=d*y
 
 
  
print(a+b)

### FIXED CODE ###

N=int(input())
 
X=[]
for i in range(N):
  a=input().split()
  X.append([int(a[0]),int(a[1])])
 
 
a=X[0][0]
b=X[0][1]
 
for i in range(1,N):
  c=X[i][0]
  d=X[i][1]
 

  x= (a + (c - 1)) // c
  y= (b + (d -1)) // d
  
  a1=1000000000000000000
  b1=1000000000000000000
  a2=1000000000000000000
  b2=1000000000000000000
  if d*x-b>=0:
    a1=c*x
    b1=d*x
 
  
  if c*y-a>=0:
    a2=c*y
    b2=d*y
  if (a1+b1)<(a2+b2):
    a=a1
    b=b1
  else:
    a=a2
    b=b2
 
  #print("i = "+str(i)+" a = "+str(a)+" b = "+str(b)+" a1 = "+str(a1)+" b1 = "+str(b1)+" a2 = "+str(a2)+" b2 = "+str(b2)+" c = "+str(c)+" d = "+str(d))
  
 
 
 
 
  
print(a+b)
a = int(input())
b = int(input())
if(a>8 or b>8):
    print (":(")
else:
    print("Yay!")
        

### FIXED CODE ###

a,b = map(int,input().split())
if(a>8 or b>8):
    print (":(")
else:
    print("Yay!")
s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
n = int(input())
input_s = input()
print(''.join([s[s.index(c)+n % 26] for c in input_s])

### FIXED CODE ###

s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
n = int(input())
input_s = input()
print(''.join([s[(s.index(c)+n) % 26] for c in input_s]))
N = int(input())
odd = 0
if i%2==1 for i in range (1, N+1):
  odd += 1
print(odd//N)

### FIXED CODE ###

N = int(input())
odd = 0
for i in range (1, N+1):
    if i%2==1:
        odd += 1

print(odd/N)
a=int(input())
b=int(input())
print(a*b," ",2*(a+b))


### FIXED CODE ###

a,b=[int(i) for i in input().split()]
print(a*b,2*(a+b))
while True:
    n = input()
    if n == 0: break
    print ans(input() for i in range(n/4))

### FIXED CODE ###

while True:
    n = input()
    if n == 0: break
    print sum(input() for i in range(n/4))
n,m,t =int(input())

z = (n+m-1)/m

ans = z * t

print(ans)

### FIXED CODE ###

n, m, t = input().split()
z = ((int(n)+int(m))-1)//int(m)
ans = z * int(t) 
print(ans)
s = input()
t = input()
d = {i:[] for i in "abcdefghijklmnopqrstuvwxyz"}
for i in range(len(s)):
    d[s[i]].append(i)
for i in t:
    if len(d[i]) == 0:
        print(-1)

def binary_search(arr, k):
    l, r = 0, len(arr)-1
    while l <= r:
        m = l+r >> 1
        if arr[m] < k:
            l = m+1
        else:
            r = m-1
    return l

a, b = 0, 0
for i in t:
    j = binary_search(d[i], a)
    if len(d[i]) <= j:
        a = 0
        b += 1
        j = binary_search(d[i], 0)
    a = d[i][j] + 1
print(b * len(s) + a)


### FIXED CODE ###

# -*- coding: utf-8 -*-
"""
Created on Mon Feb 17 09:13:00 2020

@author: msaleh1
"""

def upper_bound(nums, target):
    l, r = 0, len(nums) - 1
    while l <= r:
        mid = int(l + (r - l) / 2)
        if nums[mid] > target:
            r = mid - 1
        else:
            l = mid + 1
    return l


mx = pow(10,100)
#pypy3
s = input()
t = input()

    
cnt_s = dict()
cnt_t = dict()

for c in t:
    if c in cnt_t:
        cnt_t[c]+=1
    else:
        cnt_t[c] = 1


for idx in range(len(s)):
    if s[idx] in cnt_s:
        cnt_s[s[idx]].append(idx)  
    else:
       cnt_s[s[idx]] = [idx]

#cannot
for x in cnt_t:
    if x not in cnt_s:
        print(-1)
        exit(0)
    else:
        if(cnt_t[x] > mx*len(cnt_s[x])):
            print(-1)
            exit(0)
            
cur_indx =-1
skp =0
ans = 0

for x in t:
    temp_idx = upper_bound(cnt_s[x],cur_indx)
    if(temp_idx == len(cnt_s[x])):
        skp+=1
        cur_indx = cnt_s[x][0]
    else:
        cur_indx = cnt_s[x][temp_idx];
if(skp >= mx):
    print(-1)
else:
    print(skp*len(s)+cur_indx+1)
N = int(input())
s = [int(input()) for i in range(N)]
M = int(input())
t = [int(input()) for i in range(M)]

s1 = list(set(s))
a = []
for i in range (len(s1)):
    for j in range (N):
        if s1[i] == s[j]:
            a[i]+=1
for i in range (len(s1)):
    for j in range (M):
        if s1[i] == t[j]:
            a[i]-=1
print(max(a))

### FIXED CODE ###

N = int(input())
s = [input() for i in range(N)]
M = int(input())
t = [input() for i in range(M)]

s1 = list(set(s))
a = [0 for i in range(101)]
for i in range (len(s1)):
    for j in range (N):
        if s1[i] == s[j]:
            a[i]+=1
for i in range (len(s1)):
    for j in range (M):
        if s1[i] == t[j]:
            a[i]-=1
print(max(a))
MOD = 10**9 + 7

def make_fac_tables_mod_p(n, p):
  fac = [0] * (n+1)
  fac_inv = [0] * (n+1)
  mod_inv = [0] * (n+1)
  fac[0] = fac[1] = 1
  fac_inv[0] = fac_inv[1] = 1
  mod_inv[1] = 1
  for i in range(2, n+1):
    fac[i] = (fac[i-1] * i) % p
    mod_inv[i] = p - mod_inv[p % i] * (p // i) % p
    fac_inv[i] = fac_inv[i-1] * mod_inv[i] % p
  return fac, fac_inv

def nCk_mod_p(fac, fac_inv, n, k, p):
  return ((fac[n] * fac_inv[n-k]) % p) * fac_inv[k] % p

X, Y = map(int, input().split())

ans = 0
q, r = divmod(X+Y, 3)
if r == 0:
  n = q
  k = X - q
  fac, fac_inv = make_fac_tables_mod_p(n, MOD)
  ans = nCk_mod_p(fac, fac_inv, n, k, MOD)

print(ans)


### FIXED CODE ###

MOD = 10**9 + 7

def make_fac_tables_mod_p(n, p):
  fac = [0] * (n+1)
  fac_inv = [0] * (n+1)
  mod_inv = [0] * (n+1)
  fac[0] = fac[1] = 1
  fac_inv[0] = fac_inv[1] = 1
  mod_inv[1] = 1
  for i in range(2, n+1):
    fac[i] = (fac[i-1] * i) % p
    mod_inv[i] = p - mod_inv[p % i] * (p // i) % p
    fac_inv[i] = fac_inv[i-1] * mod_inv[i] % p
  return fac, fac_inv

def nCk_mod_p(fac, fac_inv, n, k, p):
  return ((fac[n] * fac_inv[n-k]) % p) * fac_inv[k] % p

X, Y = map(int, input().split())

ans = 0
q, r = divmod(X+Y, 3)
if r == 0 and (q <= X <= 2*q) and (q <= Y <= 2*q):
  n = q
  k = X - q
  fac, fac_inv = make_fac_tables_mod_p(n, MOD)
  ans = nCk_mod_p(fac, fac_inv, n, k, MOD)

print(ans)
A,B=map(int,input().split())

K=abs(A+B)/2

if abs(A+B) % 2 == 0;
	print(K)
else;
	print("IMPOSSIBLE")

### FIXED CODE ###

A,B=map(int,input().split())

K=abs(A+B)/2

if abs(A+B) % 2 == 0:
  print(int(K))
else:
  print("IMPOSSIBLE")