Buggy python submission (verdict: Runtime Error):
import sys
from collections import defaultdict
import bisect
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
T = sr()
len_S = len(S)
indexes_S = defaultdict(list)
for i, s in enumerate(S):
    indexes_S[s].append(i)

x = -1 # 今いるところ、次はx+1から探索
for t in T:
    if indexes_S[t] == 0:
        print(-1); exit()
    arr = indexes_S[t]
    y = (x+1) % len_S
    next_i = bisect.bisect_left(arr, y) # tの同じ文字がある次のindex
    if next_i < len(arr):
        x = (x+1) + arr[next_i] - y
    else:
        x = (x+1) + arr[0] + len_S - y # 1周して次のarr

print(x+1)


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict
from bisect import bisect_left

S = input().rstrip()
T = input().rstrip()

LS = len(S)

index = defaultdict(list)

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

x = -1
for t in T:
    arr = index[t]
    if len(arr) == 0:
        x = -2
        break
    y = (x+1) % LS
    i = bisect_left(arr,y)
    if i < len(arr):
        next_x = (x+1) + arr[i] - y
    else:
        next_x = (x+1) + arr[0] - y + LS
    x = next_x

print(x + 1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
import bisect
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
len_S = len(S)
T = sr()
dict_S = defaultdict(list)
for i, s in enumerate(S):
    dict_S[s].append(i)

answer = 0
cur = -1
for t in T:
    turn = 0 # 1周した時 len_S - cur
    if not dict_S[t]:
        print(-1)
        exit()
    indexes = dict_S[t] + [len_S]
    i = bisect.bisect_right(indexes, cur)
    next = indexes[i]
    if next == len_S:
        turn = len_S - cur - 1
        cur = -1
        answer += indexes[0] - cur + turn
        cur = indexes[0]
    else:
        answer += next - cur
        cur = next

print(answer)


Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict
from bisect import bisect_left

S = input().rstrip()
T = input().rstrip()

LS = len(S)

index = defaultdict(list)

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

x = -1
for t in T:
    arr = index[t]
    if len(arr) == 0:
        x = -2
        break
    y = (x+1) % LS
    i = bisect_left(arr,y)
    if i < len(arr):
        next_x = (x+1) + arr[i] - y
    else:
        next_x = (x+1) + arr[0] - y + LS
    x = next_x

print(x + 1)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import defaultdict
import bisect
sys.setrecursionlimit(10 ** 7)

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

S = sr()
len_S = len(S)
T = sr()
dict_S = defaultdict(list)
dict_T = defaultdict(list)
for i, s in enumerate(S):
    dict_S[s].append(i)

for i, t in enumerate(T):
    dict_T[t].append(i)

answer = 0
cur = -1
for t in T:
    if not dict_S[t]:
        print(-1)
        exit()
    indexes = dict_S[t] + [len_S]
    i = bisect.bisect_right(indexes, cur)
    next = indexes[i]
    if next == len_S:
        answer = (-(-answer//len_S)) * len_S
        cur = -1
        answer += indexes[0] - cur
        cur = indexes[0]
    else:
        answer += next - cur
        cur = next

print(answer)
# 32

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from collections import defaultdict
from bisect import bisect_left

S = input().rstrip()
T = input().rstrip()

LS = len(S)

index = defaultdict(list)

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

x = -1
for t in T:
    arr = index[t]
    if len(arr) == 0:
        x = -2
        break
    y = (x+1) % LS
    i = bisect_left(arr,y)
    if i < len(arr):
        next_x = (x+1) + arr[i] - y
    else:
        next_x = (x+1) + arr[0] - y + LS
    x = next_x

print(x + 1)