qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    S = str(input().strip())
    T = str(input().strip())

    if not set(T) <= set(S):
        return -1

    n = len(S)
    d = defaultdict(list)
    for i, s in enumerate(S, start=1):
        d[s].append(i)

    cnt = 0
    pos = 0
    for t in T:
        if pos >= d[-1]:
            cnt += 1
            pos = d[t][0]
        else:
            left = 0
            right = len(d[t])
            while left < right:
                mid = (left + right) // 2
                np = d[t][mid]
                if np <= pos:
                    left = mid + 1
                else:
                    right = mid
            pos = d[t][left]

    return n * cnt + pos


if __name__ == '__main__':
    print(main())


Fixed code:
import sys
from collections import defaultdict

def main():
    input = sys.stdin.readline
    S = str(input().strip())
    T = str(input().strip())

    if not set(T) <= set(S):
        return -1

    n = len(S)
    d = defaultdict(list)
    for i, s in enumerate(S, start=1):
        d[s].append(i)

    cnt = 0
    pos = 0
    for t in T:
        if pos >= d[t][-1]:
            cnt += 1
            pos = d[t][0]
        else:
            left = 0
            right = len(d[t])
            while left < right:
                mid = (left + right) // 2
                np = d[t][mid]
                if np <= pos:
                    left = mid + 1
                else:
                    right = mid
            pos = d[t][left]

    return n * cnt + pos


if __name__ == '__main__':
    print(main())
Buggy code:
s=input()
t=input()
leng=len(s)
lent=len(t)
count=0
tmp=0
i=0
while i<lent:
   ans=s[tmp:].find(t[i])
   if ans!=-1:
       i += 1
       tmp += ans+1
       if i ==lent:
           count += tmp
           break
       continue
   if tmp==0:
       conut=-1
       break
   tmp=0
   count+=leng
if count>leng*10**100:
    count=-1
print(count)

Fixed code:
s=input()
t=input()
leng=len(s)
lent=len(t)
count=0
tmp=0
i=0
while True:
   ans=s[tmp:].find(t[i])
   if ans!=-1:
       i += 1
       tmp += ans+1
       if i ==lent:
           count += tmp
           break
       continue
   if tmp==0:
       count=-1
       break
   tmp=0
   count+=leng
if count>leng*10**100:
    count=-1
print(count)
Buggy code:
s = input()
t = input()

dic = {}
for i in range(len(s)):
    c = s[i]
    if c not in dic:
        dic[c] = []
    dic[c].append(i)

repeat = 0
focus = 0
ans = 0
for i in range(len(t)):
    c = t[i]
    if c not in dic:
        ans = -1
        break
    if dic[c][-1] <= focus:
        repeat+=1
        focus = dic[c][0]
    else:
        l = 0
        r = len(dic[c])
        mid = (r+l)//2
        while r == l:
            if(dic[c][mid] > focus):
                l = mid
            else:
                r = mid
        focus = dic[c][mid-1]
print(repeat*len(s)+focus+1)


Fixed code:
s = input()
t = input()

dic = {}
for i in range(len(s)):
    c = s[i]
    if c not in dic:
        dic[c] = []
    dic[c].append(i)

repeat = 0
focus = 0
ans = 0
for i in range(len(t)):
    c = t[i]
    if c not in dic:
        ans = -1
        break
    if dic[c][-1] < focus:
        repeat += 1
        focus = dic[c][0]+1
    else:
        l = 0
        r = len(dic[c])
        while l < r:
            mid = (r+l)//2
            if(dic[c][mid] < focus):
                l = mid + 1
            else:
                r = mid
        focus = dic[c][l]+1
print(ans if ans == -1 else repeat*len(s)+focus)
Buggy code:
import bisect
s = input()
t = input()
N = len(s)

next_map = sorted(zip(s*2,range(N*2)))
br = bisect.bisect_right
ans = 0
i = 0
for c in t:
    map_idx = br(next_map,(c,i))
    if map_idx == N*2:
        ans = -2
        break
    a,j = next_map[map_idx]
    if a != c :
        ans = -2/0
        break
    ans += j - i
    i = j%N
ans += 1
print(ans)

Fixed code:
import bisect
s = input()
t = input()
N = len(s)

next_map = sorted(zip(s*2,range(N*2)))
br = bisect.bisect_right
ans = 0
i = -1
for c in t:
    map_idx = br(next_map,(c,i))
    if map_idx == N*2:
        ans = -1
        break
    a,j = next_map[map_idx]
    if a != c :
        ans = -1
        break
    ans += j - i
    i = j%N
print(ans)
Buggy code:
import bisect
s = input()
t = input()
lens=len(s)
lent=len(t)
d = {}

for i in range(lens):
    if s[i] in d:
        d[s[i]].append(i)
    else:
        d[s[i]] = [i]

tmp=0
r=0
snum=0
for i in range(lent):
    if d.get(t[i],None)==None:
        print("-1")
        exit()
    else:
        lis=d[t[i]]
        lis.sort()
        r = bisect.bisect_left(lis,tmp)
        if(len(lis) <= r):
            snum+=lens
            tmp=lis[0]
        else:
            tmp=lis[r]+1
print(snum+tmp+1)




Fixed code:
import bisect
s = input()
t = input()
lens=len(s)
lent=len(t)
d = {}
s2=s+s
lens2=len(s2)

for i in range(lens2):
    if s2[i] in d:
        d[s2[i]].append(i)
    else:
        d[s2[i]] = [i]

tmp=0
r=0
snum=0
for i in range(lent):
    if(lens<tmp):
        snum+=lens
        tmp-=lens
        
    if d.get(t[i],None)==None:
        print("-1")
        exit()
    else:
        lis=d[t[i]]
        r = bisect.bisect_left(lis,tmp)
        tmp=lis[r]+1
print(snum+tmp)
Buggy code:
from collections import defaultdict as dd
from bisect import bisect_left as bl
s = input()
t = input()
ind = dd(list)
res = 0
for i in range(len(s)):
  ind[s[i]].append(i)
#print(ind)
for i in range(len(t)):
  if len(ind[t[i]]) == 0:
    print(-1)
    exit(0)
  temp = bl(ind[t[i]], res % len(s))
  #print(temp, res, t[i])
  if res % len(s) == ind[t[i]][temp]:
    temp += 1
  if temp == len(ind[t[i]]):
    res += (len(s) - res % len(s)) + ind[t[i]][0]
  else:
    res += ind[t[i]][temp] - res % len(s)
print(res + 1)

Fixed code:
from collections import defaultdict as dd
from bisect import bisect_left as bl
s = input()
t = input()
ind = dd(list)
res = 0
first = 1
for i in range(len(s)):
  ind[s[i]].append(i)
#print(ind)
for i in range(len(t)):
  if len(ind[t[i]]) == 0:
    print(-1)
    exit(0)
  temp = bl(ind[t[i]], res % len(s))
  #print(temp, res, t[i])
  if first == 0:
    if res % len(s) == ind[t[i]][min(temp, len(ind[t[i]]) - 1)]:
      temp += 1
  first = 0
  if temp == len(ind[t[i]]):
    res += (len(s) - res % len(s)) + ind[t[i]][0]
  else:
    res += ind[t[i]][temp] - res % len(s)
print(res + 1)
Buggy code:
import numpy as np
import math
import sys
import bisect
sys.setrecursionlimit(10000000)

def main():
    s = input()
    t = input()
    alphabets = dict.fromkeys(list("abcdefghijklmnopqrstuvwxyz"), None)
    # alphabets = alphabet.copy()
    for key_ in alphabets:
        lists = []
        for i in range(len(s)):
            if key_ == s[i]:
                lists.append(i)
        alphabets[key_] = lists
    # print(alphabets)
    now = -1
    count = 0
    for alpha in t:
        if alphabets[alpha] is None:
            print(-1)
            sys.exit()
        else:
            lists = alphabets[alpha]
            index = bisect.bisect_left(lists, now)
            if index >= len(lists):
                now = -1
                count += len(s)
                index = bisect.bisect_left(lists, now)
                now = lists[index]
            else:
                now = lists[index]
            # print(now, count)
    print(count + now + 1)
            







if __name__ == '__main__':
    main()

Fixed code:
import numpy as np
import math
import sys
import bisect
sys.setrecursionlimit(10000000)

def main():
    s = input()
    t = input()
    alphabets = dict.fromkeys(list("abcdefghijklmnopqrstuvwxyz"), None)
    # alphabets = alphabet.copy()
    for key_ in alphabets:
        lists = []
        for i in range(len(s)):
            if key_ == s[i]:
                lists.append(i)
        alphabets[key_] = lists
    now = -1
    count = 0
    for alpha in t:
        if len(alphabets[alpha]) == 0:
            print(-1)
            sys.exit()
        else:
            lists = alphabets[alpha]
            index = bisect.bisect_right(lists, now)
            if index >= len(lists):
                now = -1
                count += len(s)
                now = lists[0]
            else:
                now = lists[index]
            # print(now, count)
    print(count + now + 1)
            







if __name__ == '__main__':
    main()
Buggy code:
import bisect

s = input()
t = input()

if set(list(t)).issubset(set(list(s))):
    index_list = [ [] for i in range(26) ]
    for i, v in enumerate(s,1):
        index_list[ord(v)-97].append(i)
    ls = len(s)
    for i in range(26):
        index_list[i].append(ls+1)

    index = 0
    turn = 0 
    for v in t:
        new_index = bisect.bisect_left(index_list[ord(v)-97],index)
        index = index_list[ord(v)-97][new_index]
        if index == ls+1:
            new_index = bisect.bisect_left(index_list[ord(v)-97],0)
            index = index_list[ord(v)-97][new_index]
            turn += 1

    print(index+turn*ls)

else:
    ans = -1



Fixed code:
import bisect

s = input()
t = input()

if set(list(t)).issubset(set(list(s))):
    index_list = [ [] for i in range(26) ]
    for i, v in enumerate(s,1):
        index_list[ord(v)-97].append(i)
    ls = len(s)
    for i in range(26):
        index_list[i].append(ls+1)

    old_index = 0
    index = 0
    turn = 0 
    for i, v in enumerate(t):

        new_index = bisect.bisect_left(index_list[ord(v)-97],index+1)
        index = index_list[ord(v)-97][new_index]

        

        if index == ls+1:
            new_index = bisect.bisect_left(index_list[ord(v)-97],0)
            index = index_list[ord(v)-97][new_index]
            turn += 1


    print(index+turn*ls)

else:
    print(-1)
Buggy code:
def main():
    from collections import defaultdict
    import sys
    
    s = input()
    t = input()
    ds = defaultdict(int)
    di = {}

    for i in range(len(s)):
        di[i] = defaultdict(int)
    
    for a in s:
        ds[a] = 1
        
    flag = 0
    for b in t:
        if ds[b] == 0:
            flag = 1
    if flag == 1:
        print(-1)
        sys.exit()

    s2 = s[::-1]
    for i in range(2*len(s)):
        for j in di[len(s) - (i-1)%len(s)-1]:
            di[len(s) - i%len(s)-1][j] = di[len(s) - (i-1)%len(s)-1][j] + 1
        di[len(s) - i%len(s) - 1][s2[(i-1)%len(s)]] = 1
    tmp = t[0]
    print(di)

    for k in range(len(s)):
        if tmp == s[k]:
            answer = k+1
            break
    for k in range(len(t)-1):
        print(answer)
        l = k+1
        answer += di[(answer-1)%len(s)][t[l]]
        tmp = t[l]
    print(answer)
      

if __name__ == '__main__':
    main()

Fixed code:
def main():
    from collections import defaultdict
    import sys
    
    s = input()
    t = input()
    ds = defaultdict(int)
    di = {}

    for i in range(len(s)):
        di[i] = defaultdict(int)
    
    for a in s:
        ds[a] = 1
        
    flag = 0
    for b in t:
        if ds[b] == 0:
            flag = 1
    if flag == 1:
        print(-1)
        sys.exit()

    s2 = s[::-1]
    for i in range(2*len(s)):
        for j in di[len(s) - (i-1)%len(s)-1]:
            di[len(s) - i%len(s)-1][j] = di[len(s) - (i-1)%len(s)-1][j] + 1
        di[len(s) - i%len(s) - 1][s2[(i-1)%len(s)]] = 1
    tmp = t[0]

    for k in range(len(s)):
        if tmp == s[k]:
            answer = k+1
            break
    for k in range(len(t)-1):
        l = k+1
        answer += di[(answer-1)%len(s)][t[l]]
        tmp = t[l]
    print(answer)
      

if __name__ == '__main__':
    main()
Buggy code:

import sys
input = sys.stdin.readline

S = list(input())
T = list(input())

alpha2num = lambda c: ord(c) - ord('a') 

alpha = [[] for _ in range(26)]
for i,s in enumerate(S):
    alpha[alpha2num(s)].append(i)

import bisect
tmp = 0
N = len(S)
base = 0
for t in T:
    a = alpha2num(t)
    if len(alpha[a]) == 0:
        print(-1)
        sys.exit()
    else:
        L = len(alpha[a])        
        num = bisect.bisect_right(alpha[a],tmp)
        
        if num == L:
            base += 1
            tmp = alpha[a][0]
        else:
            tmp = alpha[a][num]
            
            
ans = N * base + tmp + 1
print(ans)
        
    


Fixed code:

import sys
#input = sys.stdin.readline

S = list(input())
T = list(input())

alpha2num = lambda c: ord(c) - ord('a') 

alpha = [[] for _ in range(26)]
for i,s in enumerate(S):
    alpha[alpha2num(s)].append(i)

import bisect
tmp = -1
N = len(S)
base = 0
for t in T:
    a = alpha2num(t)
    if len(alpha[a]) == 0:
        print(-1)
        sys.exit()
    else:
        L = len(alpha[a])        
        num = bisect.bisect_right(alpha[a],tmp)
        
        if num == L:
            base += 1
            tmp = alpha[a][0]
        else:
            tmp = alpha[a][num]
ans = N * base + tmp + 1
print(ans)
Buggy code:
import bisect
s = input()
t = input()

s_len = len(s)
t_len = len(t)

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

def get_ord(c):
    return ord(c)-97

for i in range(s_len):
    ord_c = get_ord(s[i])
    n[ord_c].append(i)

#print(n)

#print("s_len",s_len)
#print("t_len",t_len)

def get_ans():
    res = 0
    for i in range(t_len):
        rr = res % s_len
        t_ord = get_ord(t[i])
        words = n[t_ord]
        if(len(words)==0):
            return -1
        if(words[len(words)-1]<=rr):
            res += s_len - rr 
            #print("add",s_len-rr)
            rr = res % s_len
        
        bis = bisect.bisect_right(words,rr)
        w = words[bis]
        res += w - rr
        #print(t[i],words,rr,res)
    
    return res+1

print(get_ans())

Fixed code:
import bisect
s = input()
t = input()

s_len = len(s)
t_len = len(t)

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

def get_ord(c):
    return ord(c)-97

for i in range(s_len):
    ord_c = get_ord(s[i])
    n[ord_c].append(i)

#print(n)

#print("s_len",s_len)
#print("t_len",t_len)

def get_ans():
    res = 0
    for i in range(t_len):
        
        rr = res % s_len
        t_ord = get_ord(t[i])
        words = n[t_ord]
        
        if(len(words)==0):
            return -1
        if(i==0 and words[0] == 0):
            res = 0
            continue
    
        if(words[len(words)-1]<=rr):
            res += s_len - rr 
            #print("add",s_len-rr)
            rr = -1
        
        bis = bisect.bisect_right(words,rr)
        #print(bis,len(words),rr,words)
        w = words[bis]


        if(rr!=-1):
            res += w - rr
        else:
            res += w
        #print(t[i],words,rr,res)
    
    return res+1

print(get_ans())
Buggy code:
# ABC138E - Strings of Impurity
import sys
input = sys.stdin.readline

def main():
    S = input()
    T = input()
    if set(T) <= set(S):
        lim_s, lim_t = len(S) - 1, len(T)
        ans, cur, idx = 1, 0, -1
        while cur < lim_t:
            x = S[idx + 1 :].find(T[cur])
            if x == -1:
                ans += 1
                idx = -1
            else:
                idx += x + 1
                cur += 1
        ans = (ans - 1) * (lim_s + 1) + idx + 1
        print(ans)
    else:  # corner case
        print(-1)


if __name__ == "__main__":
    main()

Fixed code:
# ABC138E - Strings of Impurity
def main():
    S = input()
    T = input()
    if set(T) <= set(S):
        lim_s, lim_t = len(S) - 1, len(T)
        ans, cur, idx = 1, 0, -1
        while cur < lim_t:
            x = S[idx + 1 :].find(T[cur])
            if x == -1:
                ans += 1
                idx = -1
            else:
                idx += x + 1
                cur += 1
        ans = (ans - 1) * (lim_s + 1) + idx + 1
        print(ans)
    else:  # corner case
        print(-1)


if __name__ == "__main__":
    main()
Buggy code:

from bisect import bisect_right as br

def main():
    S = [s for s in input()]
    T = [t for t in input()]
    slen = len(S)
    tlen = len(T)

    dic = {}
    for i in range(slen):
        if S[i] in dic:
            dic[S[i]].append(i)
        else:
            dic[S[i]] = [i]

    ans = 0
    pre = 0
    if T[0] in dic:
        if T[0] != S[0]:
            ans = dic[T[0]][0]
    else:
        print(-1)
        return 
    
    for j in range(1,tlen):
        t = T[j]
        if t in dic:
            point = br(dic[t],pre)
            if point == len(dic[t]):
                ans += slen - pre + dic[t][0]
                pre = dic[t][0]
            else:
                if pre == dic[t] == point:
                    ans += slen
                else:
                    ans += dic[t][point] - pre
                    pre = dic[t][point]
        else:
            print(-1)
            return
    print(ans + 1)
    return 
main()

Fixed code:

from bisect import bisect_right as br

def main():
    S = [s for s in input()]
    T = [t for t in input()]
    slen = len(S)
    tlen = len(T)

    dic = {}
    for i in range(slen):
        if S[i] in dic:
            dic[S[i]].append(i)
        else:
            dic[S[i]] = [i]

    ans = 0
    pre = 0
    if T[0] in dic:
        if T[0] != S[0]:
            ans = dic[T[0]][0]
            pre = dic[T[0]][0]
    else:
        print(-1)
        return 

    for j in range(1,tlen):
        t = T[j]
        if t in dic:
            point = br(dic[t],pre)
            if point == len(dic[t]):
                ans += slen - pre + dic[t][0]
                pre = dic[t][0]
            else:
                if pre == dic[t] == point:
                    ans += slen
                else:
                    ans += dic[t][point] - pre
                    pre = dic[t][point]
        else:
            print(-1)
            return
    print(ans + 1)
    return 
main()
Buggy code:
from collections import defaultdict
from bisect import bisect_left

def sol():
    S = input()
    T = input()


    print('-1')
    return

    setS = set(S)
    setT = set(T)
    for t in setT:
        if not t in setS:
            print('-1')
            return

    posS = defaultdict(list)

    for i, s in enumerate(S):
        posS[s].append(i)

    for s in enumerate(S):
        posS[s].sort()

    prev = -1
    ans = 0
    N = len(S)

    for t in T:
        next = bisect_left(posS[t], prev)
        if next >= len(posS[t]):
            ans += N - prev
            ans += posS[t][0]
            prev = posS[t][0]
        else:
            ans += posS[t][next] - prev
            prev = posS[t][next]

    print(ans)

sol()


Fixed code:
from collections import defaultdict
from bisect import bisect_right

def sol():
    S = input()
    T = input()

    setS = set(S)
    setT = set(T)
    for t in setT:
        if not t in setS:
            print('-1')
            return

    posS = defaultdict(list)

    for i, s in enumerate(S):
        posS[s].append(i)

    for s in enumerate(S):
        posS[s].sort()

    prev = -1
    ans = 0
    N = len(S)

    for t in T:
        next = bisect_right(posS[t], prev)
        if next >= len(posS[t]):
            ans += N - prev
            ans += posS[t][0]
            prev = posS[t][0]
        else:
            ans += posS[t][next] - prev
            prev = posS[t][next]

    print(ans)

sol()
Buggy code:
s = input()
t = input()
ls,lt = len(s),len(t)
i = j = 0
si = lp = 0
while True:
    tc = t[i]
    si = s.find(tc)
    if si == -1:
        print(-1)
        exit(0)
    else:
        if i == lt-1:
            print(lp*ls + si+1)
            exit(0)
        else:
            while True:
                if t[i+1] in s[si+1:]:
                    si = s.find(t[i+1])
                    if i+1 == lt-1:
                        #print(lp*ls + si+1)
                        exit(0)
                    else:
                        i += 1
                else:
                    i+=1
                    break
    lp += 1
    if i >= lt:
        print(-1)
        break

Fixed code:
s = input()
t = input()
ls,lt = len(s),len(t)
i = j = 0
si = lp = 0
while True:
    tc = t[i]
    si = s.find(tc)
    if si == -1:
        print(-1)
        exit(0)
    else:
        if i == lt-1:
            print(lp*ls + si+1)
            exit(0)
        else:
            while True:
                if t[i+1] in s[si+1:]:
                    si = s[si+1:].find(t[i+1]) + si +1
                    if i+1 == lt-1:
                        print(lp*ls + si+1)
                        exit(0)
                    else:
                        i+=1
                else:
                    i+=1
                    break
    lp += 1
    if i >= lt:
        print(-1)
        break
Buggy code:
s=input()
t=input()

x=list(s)
y=list(t)
import sys
if len(set(y)-set(x))!=0:
	print(-1)
	sys.exit()

a=[[] for _ in range(26)]
for i in range(len(s)):
	a[ord(x[i])-97].append(i)



import bisect
#t=bisect.bisect_right(res[p-1],y)

repeat=0
last=0
for i in range(len(t)):
	temp=a[ord(y[i])-97]
	if last>=temp[-1]:
		repeat+=1
		last=temp[0]
	else:
		last=temp[bisect.bisect_right(temp,last)]
	print(temp,repeat,last)
print(repeat*len(s)+last+1)

Fixed code:
s=input()
t=input()
x=list(s)
y=list(t)
import sys
if len(set(y)-set(x))!=0:
	print(-1)
	sys.exit()
a=[[] for _ in range(26)]
for i in range(len(s)):
	a[ord(x[i])-97].append(i)
import bisect

repeat=0
last=-1
for i in range(len(t)):
	temp=a[ord(y[i])-97]
	if last>=temp[-1]:
		repeat+=1
		last=temp[0]
	else:
		last=temp[bisect.bisect_right(temp,last)]
	#print(temp,repeat,last)
print(repeat*len(s)+last+1)
Buggy code:
from bisect import bisect_left

s = input()
t = input()
ls, lt = len(s), len(t)

exsist = [[] for _ in range(26)]
for i in range(ls) :
    exsist[ord(s[i])-97].append(i)

print(exsist)
ret = 0
now = 0
for i in range(lt) :
    ot = ord(t[i])-97
    if len(exsist[ot]) == 0 :
        print('-1')
        break
    
    a = bisect_left(exsist[ot], now)
    if a == len(exsist[ot]) :
        ret += ls
        now = exsist[ot][bisect_left(exsist[ot], 0)]
    else :
        now = exsist[ot][a]
        
else :
    print(ret+now+1)

Fixed code:
from bisect import bisect_left

s = input()
t = input()
ls, lt = len(s), len(t)

exsist = [[] for _ in range(26)]
for i in range(ls) :
    exsist[ord(s[i])-97].append(i)

ret = 0
now = -1
for i in range(lt) :
    ot = ord(t[i])-97
    if len(exsist[ot]) == 0 :
        print('-1')
        break
    
    a = bisect_left(exsist[ot], now + 1)
    if a == len(exsist[ot]) :
        ret += ls
        now = exsist[ot][bisect_left(exsist[ot], -1)]
    else :
        now = exsist[ot][a]
        
else :
    print(ret+now+1)
Buggy code:
from bisect import bisect_left

s,t = input(),input()
s = [ord(v)-ord('a') for v in s]
t = [ord(v)-ord('a') for v in t]
indices = [list() for _ in range(26)]
for i,v in enumerate(s):
    indices[v].append(i)

loop = 0
cur = 0
for c in t:
    l = indices[c]

    if not l:
        cnt = -1
        break
    if l[-1] < cur:
        cur = l[0]
        loop += 1
    else:
        cur = l[bisect_left(l, cur)]+1
        if cur == len(s):
            cur = 0
print(loop*len(s)+cur)

Fixed code:
from bisect import bisect_left

def solve(s,t):
    indices = [list() for _ in range(26)]
    for i,v in enumerate(s):
        indices[v].append(i)

    loop = 0
    cur = 0
    for c in t:
        l = indices[c]

        if not l:
            loop = 0
            cur = -1
            break
        if l[-1] < cur:
            cur = l[0]+1
            loop += 1
        else:
            cur = l[bisect_left(l, cur)]+1
            if cur == len(s):
                loop += 1
                cur = 0
    return loop*len(s)+cur

s,t = input(),input()
s = [ord(v)-ord('a') for v in s]
t = [ord(v)-ord('a') for v in t]
print(solve(s,t))