Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    d={chr(i+a): i for i in range(26)}
    s=list(map(lambda x:d[x],input()))
    t=list(map(lambda x:d[x],input()))
    l=[[-1]*26 for _ in range(len(s))]
    ap=[0]*26
    for i,a in enumerate(s*2):
        for j in range(ap[a],len(s)):
            l[j][a]=i-j
        ap[a]=i
    ans=0
    index=len(s)-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        ans+=n
        index+=n
        index%=len(s)
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    s=list(map(lambda x:ord(x)-a,input()))
    N=len(s)
    t=list(map(lambda x:ord(x)-a,input()))
    l=[[-1]*26 for _ in range(len(s))]
    for i in range(N-1,-N,-1):
        for j in range(26):
            if s[i]==j:
                l[i-1][j]=i%N
            else:
                l[i-1][j]=l[i][j]
    ans=0
    index=N-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        if n>index:
            ans+=n-index
        else:
            ans+=n+N-index
        index=n
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    s=list(map(lambda x:ord(x)-a,list(input())))
    t=list(map(lambda x:ord(x)-a,list(input())))
    l=[[-1]*len(s) for _ in range(26)]
    ap=[0]*26
    for i,a in enumerate(s*2):
        for j in range(ap[a],len(s)):
            l[a][j]=i-j
        ap[a]=i
    ans=0
    index=len(s)-1
    for a in t:
        n=l[a][index]
        if n==-1:
            print(-1)
            exit()
        ans+=n
        index+=n
        index%=len(s)
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    s=list(map(lambda x:ord(x)-a,input()))
    N=len(s)
    t=list(map(lambda x:ord(x)-a,input()))
    l=[[-1]*26 for _ in range(len(s))]
    for i in range(N-1,-N,-1):
        for j in range(26):
            if s[i]==j:
                l[i-1][j]=i%N
            else:
                l[i-1][j]=l[i][j]
    ans=0
    index=N-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        if n>index:
            ans+=n-index
        else:
            ans+=n+N-index
        index=n
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    s=list(map(lambda x:ord(x)-a,list(input())))
    t=list(map(lambda x:ord(x)-a,list(input())))
    l=[[-1]*26 for _ in range(len(s))]
    S=s*2
    ap=[0]*26
    for i,a in enumerate(S):
        for j in range(ap[a],len(s)):
            l[j][a]=i-j
        ap[a]=i
    ans=0
    index=len(s)-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        ans+=n
        index+=n
        index%=len(s)
    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    a=ord('a')
    s=list(map(lambda x:ord(x)-a,input()))
    N=len(s)
    t=list(map(lambda x:ord(x)-a,input()))
    l=[[-1]*26 for _ in range(len(s))]
    for i in range(N-1,-N,-1):
        for j in range(26):
            if s[i]==j:
                l[i-1][j]=i%N
            else:
                l[i-1][j]=l[i][j]
    ans=0
    index=N-1
    for a in t:
        n=l[index][a]
        if n==-1:
            print(-1)
            exit()
        if n>index:
            ans+=n-index
        else:
            ans+=n+N-index
        index=n
    print(ans)

if __name__ == '__main__':
    main()