Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD

def factorize(num: int) -> dict:
    """ 素因数分解 """
    from math import sqrt
    from collections import Counter

    d = Counter()
    # 終点はルート切り捨て+1
    for i in range(2, int(sqrt(num))+1):
        # 素因数分解：小さい方から割れるだけ割って素数をカウント
        while num % i == 0:
            num //= i
            d[i] += 1
        # 1まで分解したら終了
        if num == 1:
            break
    # 最後に残ったnumは素数(ただし1^1は1^0なので数に入れない)
    if num != 1:
        d[num] += 1
    return d

def mul(C1, C2):
    return C1 + C2

def div(C1, C2):
    return C1 - C2

def gcd(C1, C2):
    return C1 & C2

def lcm(C1, C2):
    return div(mul(C1, C2), gcd(C1, C2))

N = INT()
A = LIST()

# 素因数の状態で全体のLCMを出す
A2 = [factorize(a) for a in A]
l2 = Counter()
for a in A2:
    l2 = lcm(l2, a)

# 通常の値に戻す
l = 1
for k, v in l2.items():
    l *= pow(k, v, MOD)
    l %= MOD

ans = 0
for a in A:
    ans += fermat(l, a, MOD)
    ans %= MOD
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-

import sys
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

N = INT()
A = LIST()

l = 1
for a in A:
    l = lcm(l, a)

ans = 0
for i, a in enumerate(A):
    ans += l // a
print(ans%MOD)
### BUGGY CODE ###

INF = 1e9
n = int(input())
s = list(input())
ew = [0]*n
ans = INF

for i in range(n):
    a = s[:i].count("W")
    b = s[i+1:n].count("E")
    ans = min(ans, a+b)
print(ans)

### FIXED CODE ###

INF = 1e9
n = int(input())
s = list(input())
sum_1 = [0]*(n+1)
for i in range(n):
    if s[i]=="E":
        sum_1[i+1] = sum_1[i]+1
    else:
        sum_1[i+1] = sum_1[i]

ans = INF
for i in range(n):
    a = i-sum_1[i] + sum_1[n]-sum_1[i+1]
    ans = min(ans, a)
print(ans)
### BUGGY CODE ###

import numpy as np
n, k = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]

sum = 0
for i in range(n-k+1):
  s = np.sum(p[i:i+k])
  if s > sum:
    l =p[i:i+k]
    sum = s

sum = 0
for i in range(k):
  sum += (l[i]/2)+0.5
  
print(sum)

### FIXED CODE ###

n, k = [int(i) for i in input().split()]
p = [int(i) for i in input().split()]

for i in range(n):
  p[i] = (p[i] + 1)/2
  
a = sum(p[:k])
s = a

for i in range(n-k):
  a = a - p[i] + p[i+k]
  if s < a:
    s = a
  
print(s)
### BUGGY CODE ###

R, G, B, N = list(map(int, input().split()))

sum = 0
count = 0

len_R = N // R + 1
len_G = N // G + 1
len_B = N // B + 1

for ix in range(N // R + 2):
    for iy in range(N // G + 2):
        sum = N - (R * ix + G * iy)
        if sum % B == 0:
            if sum / B >= 0:
                count += 1
        sum = R * ix + G + iy
        if sum > N:
            break
    sum = R * ix
    if sum > N:
        break

print(count)

### FIXED CODE ###

R, G, B, N = list(map(int, input().split()))

count = 0

len_r = N // R + 2
for ix in range(len_r):
    len_g = (N - (R * ix)) // G + 2
    for iy in range(len_g):
        t = N - (R * ix + G * iy)
        if t >= 0 and t % B == 0:
            count += 1
print(count)
### BUGGY CODE ###


N,M=map(int,input().split())
A=[int(x) for x in input().split()]
A.sort(reverse=True)

for i in range(0,M):
    A[0]=int(A[0]/2)
    A.sort(reverse=True)

print(sum(A))

### FIXED CODE ###

import heapq
N,M=map(int,input().split())
A=list(map(lambda x:int(x)*(-1),input().split()))
heapq.heapify(A)
    
for i in range(0,M):
    tmp=heapq.heappop(A)
    heapq.heappush(A,-int(-tmp/2))
print(-sum(A))
### BUGGY CODE ###

N=int(input())
a=list(map(int,input().split()))
A=[0]*N
B=[]
count=0
for i in range(N//2,N):
    if a[i]==1:
        A[i]=1
        count=count+1
        B.append(i+1)
for i in range(N//2)[::-1]:
    asum=0
    x=i+1
    for j in range(i,N,x):
        asum=asum+A[j]
    if asum%2==a[i]:
        A[i]=0
    else:
        A[i]=1
        B.insert(0,i+1)
        count=count+1
print(count)
if count!=0:
    print(*B)

### FIXED CODE ###

N=int(input())
a=list(map(int,input().split()))
A=[0]*N
B=[]
for i in range(N//2,N):
    if a[i]==1:
        A[i]=1
        B.append(i+1)
for i in range(N//2)[::-1]:
    asum=0 
    x=i+1
    for j in range(i,N,x):
        asum=asum+A[j]
    if asum%2!=a[i]:
        A[i]=1
        B.append(i+1)
count=sum(A)
print(count)
if count!=0:
    print(*B)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

#A1 ~ Aiまでの和 O(logN)
#A1 ~ Aiまでの和 O(logN)
def BIT_query(BIT,idx):
    res_sum = 0
    if idx == 0:
        return 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx&(-idx)
    return res_sum

#Ai += x O(logN)
def BIT_update(BIT,idx,x,n):
    while idx <= n:
        BIT[idx] += x
        idx += idx&(-idx)
    return

def main():
    N, Q = map( int, input().split())
    C = list( map( int, input().split()))
    LR = [ tuple( map( int, input().split())) for _ in range(Q)]
    LRI = []
    for i, x in enumerate(LR):
        LRI.append((x[1], x[0], i))
    LRI.sort()
    lastAppend = [-1]*(N+1)
    BIT = [0]*(N+1)
    ANS = [0]*Q
    now = 1
    for r, l, i in LRI:
        while now <= r:
            c = C[now-1]
            if lastAppend[c] == -1:
                BIT_update(BIT, now, 1,N)
            else:
                BIT_update(BIT, now, 1,N)
                BIT_update(BIT, lastAppend[c],-1,N)
            lastAppend[c] = now                
            now += 1
        ANS[i] = BIT_query(BIT,r) - BIT_query(BIT,l-1)
    print( "\n".join( map( str, ANS)))
    
    

if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    N, Q = map( int, input().split())
    C = list( map( int, input().split()))
    LRI = []
    for i in range(Q):
        l, r = map( int, input().split())
        LRI.append((r,l,i))
    LRI.sort(key=lambda x:x[0])
    lastAppend = [-1]*(N+1)
    BIT = [0]*(N+1)
    #A1 ~ Aiまでの和 O(logN)
    def BIT_query(idx):
        res_sum = 0
        if idx == 0:
            return 0
        while idx > 0:
            res_sum += BIT[idx]
            idx -= idx&(-idx)
        return res_sum

    #Ai += x O(logN)
    def BIT_update(idx,x):
        while idx <= N:
            BIT[idx] += x
            idx += idx&(-idx)
        return

    ANS = [0]*Q
    now = 1
    for r, l, i in LRI:
        while now <= r:
            c = C[now-1]
            if lastAppend[c] == -1:
                BIT_update(now, 1)
            else:
                BIT_update( now, 1)
                BIT_update(lastAppend[c],-1)
            lastAppend[c] = now                
            now += 1
        ANS[i] = BIT_query(r) - BIT_query(l-1)
    print( "\n".join( map( str, ANS)))
    
    

if __name__ == '__main__':
    main()
### BUGGY CODE ###

a, b = map(int, input().split())
ans = a
for i in range(a + 1, b + 1):
  ans ^= i
print(ans)

### FIXED CODE ###

def func(x):
  mod = (x + 1) % 4
  if (mod == 0): return 0
  elif (mod == 1): return x
  elif (mod == 2): return 1
  else: return x ^ 1
a, b = map(int, input().split())
print(func(a - 1) ^ func(b))
### BUGGY CODE ###

n = int(input())
a = list(input().split())
b = [int(i) for i in a]
c=0
print(a)
ans=0
def cal2(k):
    return k /2

while True:
    for i in range(len(a)):
        c+=int(a[i][-1])
        if  c% 2 == 0:
            b = list(map(cal2,b))
            ans+=1
        else:
            print(ans)
print(ans)

### FIXED CODE ###

import numpy as np
N=int(input())
A=list(map(int,input().split()))
A=np.array(A)
ans=2**9
for i in A:
  cnt=0
  while i%2==0:
    i=i/2
    cnt+=1
  if ans > cnt:
    ans=cnt
    
print(ans)
### BUGGY CODE ###

def main():
    N = int(input())
    L = list(map(int, input().split()))

    L.sort(reverse=True)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            min = L[i] - L[j] + 1
            max = L[j] - 1
            if min > max:
                break
            tmp = len([k for k in L if (min<=k)&(k<=max)])
            ans += tmp

    print(ans)

    return 0

if __name__ == '__main__':
    main()

### FIXED CODE ###

import bisect

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L = sorted(L)

    ans = 0

    for i in range(N):
        for j in range(i+1, N):
            X = L[i] + L[j]
            l = bisect.bisect_left(L, X, lo=j+1)
            ans += (l-(j+1))
            #print(L[j + 1:l])
    print(ans)
    return 0

if __name__ == '__main__':
    main()
### BUGGY CODE ###

# coding: utf-8
import sys
from math import log


def change_into_int(lists):
    tmps = lists.split(' ')
    num_list = []
    for n in tmps:
        num_list.append(int(n))
    return num_list


def create_result(one_list):
    ans = 0
    for n in one_list:
        ans = ans + log(n)
    return ans


def create_lists(data, point):
    num_lists = []
    flag = 0
    count = 0
    while(flag == 0):
        tmp_list = []
        for i in range(point):
            tmp_list.append(data[i+count])
            # print(i+count)
            if i+count == len(data)-1:
                flag = 1
        count = count + 1
        ans = create_result(tmp_list)
        num_lists.append(ans)
    return num_lists


if __name__ == "__main__":
    lines = []
    result_list = []
    # 入力
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    # int
    num1 = change_into_int(lines[0])
    data = change_into_int(lines[1])
    # k個分のlistを作成
    # [2,3,4,5,6]
    # [[2,3],[3,4], ...]
    result_list = create_lists(data, num1[1])
    # # 取り出して積つくる
    # for one_list in lists:
    #     result_list.append(create_result(one_list))
    # 判断
    for n in range(len(result_list)-1):
        if result_list[n] < result_list[n+1]:
            print("Yes")
        else:
            print("No")


### FIXED CODE ###

# coding: utf-8
import sys
from math import log


def change_into_int(lists):
    tmps = lists.split(' ')
    num_list = []
    for n in tmps:
        num_list.append(int(n))
    return num_list


if __name__ == "__main__":
    lines = []
    result_list = []
    # 入力
    for l in sys.stdin:
        lines.append(l.rstrip('\r\n'))
    # int
    num1 = change_into_int(lines[0])
    data = change_into_int(lines[1])
    i = 0
    while(True):
        ans = data[i] - data[i+num1[1]]
        if ans < 0:
            print("Yes")
        else:
            print("No") 
        i = i + 1
        if i+num1[1] == len(data):
            break
### BUGGY CODE ###

count = 0
N = int(input())
string = list(map(int, [input() for i in range(N)]))
for i in set(string):
    if string.count(i) % 2 != 0:
        count += 1
print(count)


### FIXED CODE ###

N = int(input())
L = set()
for i in range(N):
    L ^= {input()}
print(len(L))
### BUGGY CODE ###

import sys
import numpy as np
n,k = map(int,input().split())
h = list(map(int,sys.stdin.readline().split()))

dp = np.zeros(n, dtype=int)
dp[0] = 0
for i in range(1,n):
    x = max(i-k, 0)
    dp[i] = min(d + abs(h[i]-hd) for d, hd in zip(dp[x:i], h[x:i]))

print(dp[-1])


### FIXED CODE ###

import sys
import numpy as np
n,k = map(int,input().split())
h = np.array(list(map(int,sys.stdin.readline().split())))

dp = np.zeros(n, dtype=int)
dp[0] = 0
for i in range(1,n):
    x = max(i-k, 0)
    #dp[i] = min(d + abs(h[i]-hd) for d, hd in zip(dp[x:i], h[x:i]))
    dp[i] = (dp[x:i] + abs(h[i] - h[x:i])).min()
print(dp[-1])
### BUGGY CODE ###

n,m = map(int, input().split())
p = [0]*m 
for i in range(m):
    p[i] = input().split()
for i in range(m):
    p[i][0]=int(p[i][0])
AC=[]
WA=0
for i in range(m):
    if p[i][1]=="AC" and p[i][0] not in AC:
        AC.append(p[i][0])
    elif p[i][1]=="WA" and p[i][0] not in AC and [p[i][0],"AC"] in p:
        WA=WA+1
print(len(AC),WA)


### FIXED CODE ###

n,m = map(int, input().split())
p = [0]*m 

for i in range(m):
    p[i] = input().split()
AC=[[0,-1] for j in range(n)]
k=0
WA=0
for i in range(m):
    if p[i][1]=="AC" and AC[int(p[i][0])-1][0]==0 :
        AC[int(p[i][0])-1][0]=1
        AC[int(p[i][0])-1][1]=i
        k=k+1
for i in range(m):
    if p[i][1]=="WA" and AC[int(p[i][0])-1][0]==1 and AC[int(p[i][0])-1][1]>i:
        WA=WA+1

print(k,WA)
### BUGGY CODE ###

s=input()
a=[]
b=0
w=0
for i in s:
  if i =='B':
    a.append(0)
    b+=1
  else:
    a.append(1)
    w+=1
A=len(a)
cnt=0
for i in range(A-1):
  for j in range(A-1-i):
    if a[j]==0 and a[j+1]==1:
      a[j],a[j+1]=a[j+1],a[j]
      cnt+=1
print(cnt)

### FIXED CODE ###

s=input()
a=[]
b=0
w=0
for i in s:
  if i =='B':
    a.append(0)
    b+=1
  else:
    a.append(1)
    w+=1
A=len(a)
cnt=0
for i in range(A):
  if a[i]==1:
    cnt+=i
for i in range(w):
  cnt-=i
print(cnt)
### BUGGY CODE ###

import numpy as np
N=int(input())
L=list(map(int,input().split()))
L.sort()
L=np.array(L)
#print(L)

#b-a < c < a+b
#a<=b<=c
ans=0
for a in range(N-2):
    for b in range(a+1,N-1):
        ans+=sum((L[b]-L[a] < L[b+1:]) & (L[b+1:] < L[a]+L[b]))
        
print(ans)

### FIXED CODE ###

import bisect
import collections
N=int(input())
L=list(map(int,input().split()))
L.sort()
c = collections.Counter(L)
#print(L)
 
#b-a < c < a+b

#a<=b<=c
ans=0
L_a=L[:N-2]
for i,a in enumerate(L_a):
    L_b=L[i+1:N-1]
    for j,b in enumerate(L_b,start=i+1):
        x=bisect.bisect(L[j+1:],a+b)
        ans+=x
        if a+b==L[j+x]:
            #print(a,b,L[j+x])
            ans-=c[a+b]
        #print(i,a,j,b,x,N-j-1,L[j+1:])
print(ans)
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
word = list(s)
li = [list(input().split()) for _ in range(q)]


def dfs(m):
    for i in range(q):
        if word[m] == li[i][0]:
            if li[i][1] == 'R':
                m += 1
            else:
                m -= 1
        if m < 0:
            return 'Left'
        elif m > n-1:
            return  'Right'
    return True

def left(a,b):
    m = int((a+b)/2)
    if a == m:
        if dfs(a) != 'Left':
            return 0
        elif dfs(b) == 'Left':
            return n
        else:
            return b
    if dfs(m) == 'Left':
        return left(m,b)
    else:
        return left(a,m)

def right(a,b):
    m = int((a+b)/2)
    if a == m:
        if dfs(b) != 'Right':
            return n-1
        elif dfs(a) == 'Right':
            return -1
        else:
            return a
    if dfs(m) == 'Right':
        return right(a,m)
    else:
        return right(m,b)

print(right(0,n-1) - left(0,n-1) + 1)


### FIXED CODE ###

n,q = map(int,input().split())
s = input()
word = list(s)
li = [list(input().split()) for _ in range(q)]
memo = {}

def dfs(m):
    k = m
    if m in memo:
        return memo[m]
    for x in li:
        if word[m] == x[0]:
            if x[1] == 'R':
                m += 1
            else:
                m -= 1
        if m < 0:
            memo[k] = 'Left'
            return 'Left'
        elif m > n-1:
            memo[k] = 'Right'
            return  'Right'
    memo[k] = True
    return True

def left(a,b):
    m = int((a+b)/2)
    if a == m:
        return b
    elif dfs(m) == 'Left':
        return left(m,b)
    else:
        return left(a,m)

def right(a,b):
    m = int((a+b)/2)
    if a == m:
        return a
    elif dfs(m) == 'Right':
        return right(a,m)
    else:
        return right(m,b)

if dfs(0) == 'Right':
    print(0)
elif dfs(0) == 'Left':
    if dfs(n-1) == 'Right':
        print(right(0,n-1) - left(0,n-1) + 1)
    elif dfs(n-1) == 'Left':
        print(0)
    else:
        print(n - left(0,n-1))
else:
    if dfs(b) == 'Right':
        print(right(0,n-1) + 1)
    elif dfs(b) == 'Left':
        print(0)
    else:
        print(n)
### BUGGY CODE ###

n=int(input())
A=list(map(int,input("").split()))
ans=1

for i in A:
    ans*=i
if ans>(10**18):
    print(-1)
else: 
    print(ans)

### FIXED CODE ###

n=int(input())
A=list(map(int,input("").split()))
ans=1
A.sort()
for i in A:
    ans*=i
    if ans==0:
        break
    if ans>(10**18):
        ans="-1"
        break

print(ans)
### BUGGY CODE ###

n = int(input())
p = list(map(int,input().split()))

ans = 0
for i in range(n):
    p_list = list(sorted(p[:i+1])) 
    
    if p_list[0] == p[i]:
        ans += 1
    
print(ans)

### FIXED CODE ###

n = int(input())
ppp = list(map(int, input().split()))
curr = 10 ** 6
ans = 0
for p in ppp:
    if p < curr:
        ans += 1
        curr = p
print(ans)
### BUGGY CODE ###


import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

import math
from fractions import gcd

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

# 最大公約数
g = gcd(A, B)

if g == 1:
    print(1)
    exit()

def get_sieve_of_eratosthenes(n):
    if not isinstance(n, int):
        raise TypeError('n is int type.')
    if n < 2:
        raise ValueError('n is more than 2')
    prime = []
    limit = math.sqrt(n)
    data = [i + 1 for i in range(1, n)]
    while True:
        p = data[0]
        if limit <= p:
            return prime + data
        prime.append(p)
        data = [e for e in data if e % p != 0]

prime = get_sieve_of_eratosthenes(g)
phash = {}
for p in prime:
    phash[p] = True

#print(prime)
#print(phash)

ggg = set()
for i in range(1, g):
    for j in range(i+1, g+1):
        if A%i == 0 and B%i == 0 and A%j == 0 and B%j == 0:
            if i in phash:
                ggg.add(i)
            if j in phash:
                ggg.add(j)

# 1 の分を +1
print(len(ggg)+1)


### FIXED CODE ###


import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

from fractions import gcd

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

# 最大公約数
g = gcd(A, B)

if g == 1:
    print(1)
    exit()

def prime_factorize(n):
    a = set()
    while n % 2 == 0:
        a.add(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.add(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.add(n)
    return a

print(len(prime_factorize(g)) +1)
### BUGGY CODE ###

import re


def resolve():
    n = int(input())
    s = input()
    for i in range(n//2, 0, -1):
      for j in range(n-i):
        if re.search(s[j:j+i]+r".*"+s[j:j+i],s)!=None:
          print(i)
          return
    print(0)


if __name__ == '__main__':
    resolve()

### FIXED CODE ###

def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1
    j = 0
    A[0] = l = len(S)
    while i < l:
        while i + j < l and S[j] == S[i + j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i + k] = A[k]
            k += 1
        i += k
        j -= k
    return A


def resolve():
    n = int(input())
    s = input()
    ans = 0
    for i in range(n):
        a = z_algo(s[i:])
        for index, j in enumerate(a):
            if j <= index:
                ans = max(ans, j)
    print(ans)


if __name__ == '__main__':
    resolve()
### BUGGY CODE ###

s = input()
t = input()

len_s = len(s)
dp = [[-1 for _ in range(len_s)] for _ in range(26)]
# dp[i][j] = いまsのj文字目にいる。次に探しているのはアルファベットのi文字目であるとき、次までの必要文字数。
# print(len(dp))
# print(len(dp[0]))

for idx in range(len_s):

	ch = s[idx]
	i = ord(ch)-ord("a")
#	for j in range(len_s):
	if dp[i][0] == -1:
		dp[i] = [idx] * len_s
	else:
		temp = dp[i][idx]
		dp[i][temp+1:idx] = [idx] * (idx - temp + 1)

ans = 0
j = 0
first = True
for ch in t:

	i = ord(ch)-ord("a")

	if dp[i][j] < 0:
		print(-1)
		exit()

	distance = dp[i][j] - j
	if (not first )and distance <= 0 :
		distance += len_s
	ans += distance
	j = dp[i][j]

	first = False

print(ans + 1)


### FIXED CODE ###

# https://img.atcoder.jp/abc138/editorial.pdf
# https://www.youtube.com/watch?v=lWETOlGiuaI
# このコードは公式解説動画をまんまpythonにしたものです。

# 解説動画
# s = abacaababc (10文字)の場合は
# a: [0, 2, 4, 5, 7, 10, 12, 14, 15, 17]
# b: [1, 6, 8, 11, 16, 18]
# c: [3, 9, 13, 19]
# という二重配列をまず作る。

# 例えばt = 'bbcca'だと
# 現在位置0、bを探す→1
# 現在位置1、bを探す→6
# 現在位置6、cを探す→9
# 現在位置9、cを探す→13(3に戻す)
# 現在位置3、aを探す→4
# で答えは14。


# 解説pdf曰く：
# next(ch, i):=「s'のi文字目以降で文字種chが最初に現れる位置」を
# をO(文字種数×|S|)かけて事前に全て求める

# 文字の先頭に戻る可能性があるので、2回連結しておくと処理が楽になる

from bisect import bisect_left

s = input()
t = input()
n = len(s)
m = len(t)

index_list = [[] for _ in range(26)]

for i in range(n):
    index_list[ord(s[i]) - ord('a')].append(i)
for i in range(n):
    index_list[ord(s[i]) - ord('a')].append(i+n)

p = 0  # p以降
ans = 0
for j in range(m):
    ch = ord(t[j]) - ord('a')
    if len(index_list[ch]) == 0:
        # t内に出現する文字が、sに全く登場しないので、不可
        print(-1)
        exit()
    next_pos = bisect_left(index_list[ch], p)
    p = index_list[ch][next_pos] + 1
    if p >= n:
        p -= n
        ans += n
    # print(p)

ans += p
print(ans)
### BUGGY CODE ###

n, k = [int(i) for i in input().split()]
al = [int(i) for i in input().split()]
al.sort()
mod = 10 ** 9 + 7


kai_dp={}
def kai(x, p=mod):
    if x in kai_dp:
        return kai_dp[x]
    a = 1
    for i in range(1, x + 1):
        a *= i
        a %= p
    kai_dp[x]=a
    return a

def comb(a, b, p=mod):
    if a < 0 or b < 0:
        return 0
    elif a < b:
        return 0
    c = 1
    c *= kai(a, p)
    c *= pow(kai(b, p), p - 2, p)
    c *= pow(kai(a - b, p), p - 2, p)
    return c % p


ans = 0
for i in range(k - 1, n):
    ans += al[i] * comb(i,k-1)
    ans %= mod
al = list(reversed(al))
for i in range(k - 1, n):
    ans -= al[i] * comb(i, k-1)
    ans %= mod
print(ans)


### FIXED CODE ###

n, k = [int(i) for i in input().split()]
al = [int(i) for i in input().split()]
al.sort()
mod = 10 ** 9 + 7


kai = [1]
for i in range(1, n + 1):
    kai.append(kai[-1]*i%mod)




def comb(a, b, p=mod):
    if a < 0 or b < 0:
        return 0
    elif a < b:
        return 0
    c = 1
    c *= kai[a]
    c *= pow(kai[b], p - 2, p)
    c *= pow(kai[a - b], p - 2, p)
    return c % p


ans = 0
for i in range(k - 1, n):
    ans += al[i] * comb(i,k-1)
    ans %= mod
al = list(reversed(al))
for i in range(k - 1, n):
    ans -= al[i] * comb(i, k-1)
    ans %= mod
print(ans)
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
a = list(set(A))
a.sort(reverse = True)
B = []
for i in a:
    if A.count(i) >= 2:
        B.append(i)
if len(B) <= 1:
    print(0)
else:
    if A.count(B[0]) >= 4:
        print(B[0]*B[0])
    else:
        print(B[0]*B[1])

### FIXED CODE ###

import sys
input = sys.stdin.readline
import collections

n = int(input())
A = list(map(int, input().split()))
c = collections.Counter(A)
l = c.most_common()
L = [ l[i] for i in range(len(l)) if l[i][1] >= 2 ]
b = sorted(L,reverse = True)
if len(b) == 0:
    ans = 0
elif len(b) == 1 and b[0][1] < 4:
    ans = 0
else:
    if b[0][1] >= 4:
        ans = b[0][0]**2
    else:
        ans = b[0][0]*b[1][0]
print(ans)