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

N=int(input())
A = [int(x) for x in input().split()]

ans=1
if 0 in A:
    ans = 0
else:
    for a in A:
        A.reverse()
        if a > 10**18:
            ans = -1
        else:
            ans *= a
            if ans > 10**18:
                ans = -1
print(ans)

                


### FIXED CODE ###

N=int(input())
A = [int(x) for x in input().split()]

ans=1
if 0 in A:
    ans = 0
else:
    A.sort(reverse=True)
    for a in A:
        if a > 10**18:
            ans = -1
            break
        else:
            ans *= a
            if ans > 10**18:
                ans = -1
                break
print(ans)
### BUGGY CODE ###

import heapq
N = int(input())
a = list(map(int,input().split()))
fa = a[:N:]
ba = list(map(lambda x: x*(-1), a[2*N::]))
heapq.heapify(fa)
heapq.heapify(ba)
suml = [sum(fa)]
k = -2

for i in range(N, 2*N):
    heapq.heappush(fa, a[i])
    heapq.heappop(fa)
    suml.append(sum(fa))

suml[-1] += sum(ba)

for j in range(2*N-1, N-1, -1):
        heapq.heappush(ba, -a[j])
        heapq.heappop(ba)
        suml[k] += sum(ba)
        k -= 1
    


print(max(suml))


    


### FIXED CODE ###

import heapq
N = int(input())
a = list(map(int,input().split()))
fa = a[:N:]
ba = list(map(lambda x: x*(-1), a[2*N::]))
heapq.heapify(fa)
heapq.heapify(ba)
sumf = sum(fa)
sumb = sum(ba)
suml = [sumf]

k = -2

for i in range(N, 2*N):
    heapq.heappush(fa, a[i])
    sumf += a[i]
    sumf -= heapq.heappop(fa)
    suml.append(sumf)

suml[-1] += sumb

for j in range(2*N-1, N-1, -1):
        heapq.heappush(ba, -a[j])
        sumb -= a[j]
        sumb -= heapq.heappop(ba)
        suml[k] += sumb
        k -= 1
    


print(max(suml))
### BUGGY CODE ###

N, K = map(int,input().split())
H = list(map(int,input().split()))

if N < K:
    K = N
for i in range(K):
    max1 = max(H)
    H.remove(max1)

sum = sum(H)

print(sum)


### FIXED CODE ###

N, K = map(int,input().split())
H = list(map(int,input().split()))

H.sort(reverse=True)
del H[:K]
H_sum = sum(H)

print(H_sum)
### BUGGY CODE ###


N = int(input())
A = [int(input()) for _ in range(N)]

maxims = [A[0]]
ans = 1
for i in range(1,N):
    for j,m in enumerate(maxims):
        if A[i] > m:
            maxims[j] = A[i]
            break
    else:
        maxims.append(A[i])
        ans += 1
    
print(ans)
    
    
    

### FIXED CODE ###

import bisect
import sys
input = sys.stdin.readline

N = int(input())
A = [-1*int(input()) for _ in range(N)]

maxims = [A[0]]
ans = 1
for a in A[1:]:
    if maxims[-1] > a:
        idx = bisect.bisect(maxims,a)
        maxims[idx] = a
    else:
        maxims.append(a)
        ans += 1
    
print(ans)
### BUGGY CODE ###

S = input()

def f (S):
    for i, s in enumerate(S):
        cnt = dict()
        cnt = {s:1}
        for j, e in enumerate(S[i+1:]):
            if e in cnt.keys():
                cnt[e] += 1
            else:
                cnt[e] = 1
            if max(cnt.values()) / sum(cnt.values()) > 0.5:
                return "{} {}".format(i+1, (i+1)+j+1)
    return "-1 -1"
print(f(S))

### FIXED CODE ###

S = input()
def f(S):
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            return "{} {}".format(i+1, i+2)
        elif i < len(S)-2 and S[i] == S[i+2]:
            return "{} {}".format(i+1, i+3)
        else:
            pass
    return "-1 -1"
print(f(S))
### BUGGY CODE ###

N, Q = map(int, input().split())
S = input()

for i in range(Q):
    l, r = map(int, input().split())
    c = 0
    for j in range(l - 1, r - 1):
        if S[j:j + 2] == "AC":
            c += 1
    print(c)

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()
SC = [0] * N

for i in range(N - 1):
    if S[i:i + 2] == "AC":
        SC[i + 1] = SC[i] + 1
    else:
        SC[i + 1] = SC[i]

for i in range(Q):
    l, r = map(int, input().split())
    print(SC[r - 1] - SC[l - 1])
### BUGGY CODE ###

N=int(input())
A=[int(x) for x in input().split()]
AA=sorted(set(A),reverse=True)
li=[]
for aa in AA:
  if A.count(aa)>=4:
    li.append(aa)
    break
  if A.count(aa)>=2:
    li.append(aa)
  if len(li)==2:
    break
if len(li)==0:
  print(0)
if len(li)==1:
  print(li[0]**2)
if len(li)==2:
  print(li[0]*li[1])

### FIXED CODE ###

import collections
N=int(input())
A=[int(x) for x in input().split()]
C=collections.Counter(A)
C=sorted(list(C.items()),reverse=True)
li=[]
for i,j in C:
  if j>=4:
    li.append(i)
    break
  if j>=2:
    li.append(i)
  if len(li)==2:
    break
if len(li)==0:
  print(0)
if len(li)==1:
  print(li[0]**2)
if len(li)==2:
  print(li[0]*li[1])
### BUGGY CODE ###

n = int(input())
ans = 0
ls = []
for _ in range(n):
    s = input()
    ls2 = []
    for i in range(10):
        ls2.append(s[i])
    ls3 = sorted(ls2)
    for j in ls:
        if j == ls3:
            ans +=1
    ls.append(ls3)
print(ans)

### FIXED CODE ###

n = int(input())
ans = 0
ls = {}
for _ in range(n):
    s = ''.join(sorted(input()))
    if s in ls:
        ans += ls[s]
        ls[s] += 1
    else:
        ls[s] = 1
print(ans)
### BUGGY CODE ###

import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math

Q = int(input())
q = []
for i in range(Q):
	q.append(list(map(int, input().split())))

a_list = []
b_sum = 0
a_sum = 0
a_sum_half = 0
for i in range(Q):
	if len(q[i]) == 3:
		loc = bisect.bisect(a_list, q[i][1])
		bisect.insort(a_list, q[i][1])
		if len(a_list) % 2 == 0 and loc < len(a_list)//2:
			a_sum_half += q[i][1]
		elif len(a_list) % 2 == 0 and loc >= len(a_list)//2:
			a_sum_half += a_list[len(a_list)//2 - 1]
		elif len(a_list) % 2 == 1 and loc < len(a_list) // 2:
			a_sum_half += q[i][1]
			a_sum_half -= a_list[len(a_list)//2]
		a_sum += q[i][1]
		b_sum += q[i][2]
	else:
		index = (len(a_list) - 1)//2
		argmin_x = a_list[index]
		min_x = argmin_x*(len(a_list)//2) - a_sum_half
		min_x += (a_sum - a_sum_half) - (argmin_x*(len(a_list) - len(a_list)//2))
		min_x += b_sum
		print("{0} {1}".format(argmin_x, min_x))

### FIXED CODE ###


import sys
from collections import deque, defaultdict
import copy
import bisect
input=sys.stdin.readline
#sys.setrecursionlimit(10 ** 9)
import math
import heapq

Q = int(input())
#Q = 100000
q = []
for i in range(Q):
	q.append(list(map(int, input().split())))
	#q.append([1, (i*17) % 10000, i])

a_list = []
a_list2 = []
b_sum = 0
a_sum = 0
a_sum_half = 0
length = 0
for i in range(Q):
	if len(q[i]) == 3:
		if len(a_list2) == 0:
			D = True
		else:
			x = heapq.heappop(a_list2)
			heapq.heappush(a_list2,x)
			D = (x <= q[i][1])
		length += 1
		if length % 2 == 0 and not D:
			a_sum_half += q[i][1]
			heapq.heappush(a_list, -q[i][1])
		elif length % 2 == 0 and D:
			heapq.heappush(a_list2, q[i][1])
			x = heapq.heappop(a_list2)
			heapq.heappush(a_list, -x)
			a_sum_half += x
		elif length % 2 == 1 and not D:
			a_sum_half += q[i][1]
			heapq.heappush(a_list, -q[i][1])
			x = heapq.heappop(a_list)
			a_sum_half += x
			heapq.heappush(a_list2, -x)
		else:
			heapq.heappush(a_list2, q[i][1])
		a_sum += q[i][1]
		b_sum += q[i][2]
	else:
		if length % 2 == 0:
			argmin_x = -heapq.heappop(a_list)
			heapq.heappush(a_list, -argmin_x)
		else:
			argmin_x = heapq.heappop(a_list2)
			heapq.heappush(a_list2, argmin_x)
		min_x = argmin_x*(length//2) - a_sum_half
		min_x += (a_sum - a_sum_half) - (argmin_x*(length - length//2))
		min_x += b_sum
		print("{0} {1}".format(argmin_x, min_x))
### BUGGY CODE ###

from random import choice
from random import random
from time import time


class Optimizer:
    def __init__(self, C, S):
        self.C = C
        self.S = S
        X = []
        L = [-1] * 26
        for i in range(365):
            max_diff = -10**10
            best_j = 0
            for j in range(26):
                memo = L[j]
                L[j] = i
                diff = self.S[i][j] - \
                    sum([self.C[jj] * (i - L[jj]) for jj in range(26)])
                if diff > max_diff:
                    max_diff = diff
                    best_j = j
                L[j] = memo
            L[best_j] = i
            X.append(best_j)
        self.X = X

    def calc_score(self):
        score = 0
        L = [-1 for j in range(26)]
        A = [0 for j in range(26)]
        for i in range(365):
            score += self.S[i][self.X[i]]
            A[self.X[i]] += (i - L[self.X[i]]) * \
                (i - L[self.X[i]] - 1) // 2
            L[self.X[i]] = i
        for j in range(26):
            A[j] += (365 - L[j]) * (365 - L[j] - 1) // 2
            score -= self.C[j] * A[j]
        return score

    def optimize(self, temp, threp, interval):
        max_score = self.calc_score()
        start = time()
        while time() - start < 1.85:
            t = (time() - start) / 2
            if random() < threp:
                i = choice(range(365))
                j = choice(range(26))
                memo = self.X[i]
                self.X[i] = j
            else:
                a = choice(range(interval))
                i0 = choice(range(365 - a))
                i1 = i0 + a
                self.X[i0], self.X[i1] = self.X[i1], self.X[i0]
            new_score = self.calc_score()
            if new_score > max_score:
                max_score = new_score
            elif random() < pow(2.7, (new_score - max_score) / pow(2000, 1 - t)):
                max_score = new_score
            else:
                try:
                    self.X[i0], self.X[i1] = self.X[i1], self.X[i0]
                except BaseException:
                    self.X[i] = memo
        return max(max_score + 10**6, 0)

    def output(self):
        for x in self.X:
            print(x + 1)


d = int(input())
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
opt = Optimizer(C, S)
opt.optimize(400, 0.8, 10)
opt.output()


### FIXED CODE ###

import random

d = int(input())
dd = d * (d + 1) // 2
*C, = map(int, input().split())
S = [list(map(int, input().split())) for i in range(d)]
T = [random.choice(range(26)) for i in range(d)]
score = 0

for _ in range(10**5):
    p = random.choice(range(d))
    q = random.choice(range(26))
    memo = T[p]
    T[p] = q
    L = [-1 for j in range(26)]
    X = [0 for j in range(26)]
    tmp = 0
    for i in range(d):
        tmp += S[i][T[i]]
        X[T[i]] += (d - i) * (i - L[T[i]])
        L[T[i]] = i
    for j in range(26):
        tmp -= C[j] * (dd - X[j])
    if tmp > score:
        score = tmp
    else:
        T[p] = memo

for t in T:
    print(t + 1)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
n, k = map(int, input().split())
a = list(map(int, input().split()))
for j in range(k):
  b = [0] * (n + 1)
  for i in range(n):
    left_edge = max(0, i-a[i])
    right_edge = min(i + a[i] + 1, n)
    b[left_edge] += 1
    b[right_edge] -= 1
  for i in range(1, n+1):
    b[i] = b[i] + b[i-1]
  if a == b:
    break
  a = b.copy()
print(' '.join(str(b[i]) for i in range(n)))

### FIXED CODE ###

# -*- coding: utf-8 -*-
n, k = map(int, input().split())
a = list(map(int, input().split()))
for j in range(k):
  b = [0] * (n + 1)
  for i in range(n):
    left_edge = max(0, i-a[i])
    right_edge = min(i + a[i] + 1, n)
    b[left_edge] += 1
    b[right_edge] -= 1
  for i in range(1, n+1):
    b[i] = b[i] + b[i-1]
  b.pop(n)
  if a == b:
    break
  a = b.copy()
print(' '.join(str(b[i]) for i in range(n)))
### BUGGY CODE ###

import sys
import copy
import math
import itertools
#import numpy as np
import re

N = int(input())
A = [int(input()) for c in range(N)]
ans = []

for i in A:
    if i in ans:
        ans.remove(i)
    else:
        ans.append(i)
print(len(ans))

### FIXED CODE ###

N = int(input())
A = [int(input()) for c in range(N)]
A = sorted(A)

tmp = 1
cnt = 0
for i in range(N):    
    if i+1<N:
        if A[i] == A[i+1]:
            tmp += 1
        else:
            cnt += tmp%2
            tmp = 1
    else:
        cnt += tmp%2

print(cnt)
### BUGGY CODE ###

def search(keys,dic,now,sum_):
    if sum_ < 0:
        return -1
    elif now==len(keys):
        if sum_==0:
            return True
        else:
            return 0
    k=keys[now]
    for i in range(dic[k]+1):
        res=search(keys,dic,now+1,sum_-2*i*k)
        if res == 0:
            continue
        elif res==-1:
            break
        else:
            return True
            
    return 0

s=input()+'T'
x,y=list(map(int,input().split()))
xlist=[0 for _ in range(8000)]
ylist=[0 for _ in range(8000)]
flag='x+'
ini=0
tmp=0
xsum=0
ysum=0
for c in s:
    if c =='F':
        tmp+=1
    else:
        if flag=='x':
            xlist[tmp]+=1
            xsum+=tmp
        elif flag=='y':
            ylist[tmp]+=1
            ysum+=tmp
        else:
            ini=tmp
        tmp=0
        if flag=='y':
            flag='x'
        else:
            flag='y'
xlist[0]=0
ylist[0]=0
xkey=[]
ykey=[]
for i in range(8000):
    if xlist[i]>0:
        xkey.append(i)
    if ylist[i]>0:
        ykey.append(i)
xkey.reverse()
ykey.reverse()

if search(xkey,xlist,0,xsum-x+ini)==1 and search(ykey,ylist,0,ysum-y)==1:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

s=input()+'TE'
x,y=list(map(int,input().split()))

ini = s.find('T')
s=s[ini+1:]

is_y=1
point=[set([ini]),set([0])]

while len(s)>1:
    tmp=s.find('T')
    if tmp != 0:
        set_=set()
        for z in point[is_y]:
            set_.add(z+tmp)
            set_.add(z-tmp)
        point[is_y]=set_
    is_y+=1
    is_y%=2
    s=s[tmp+1:]

if x in point[0] and y in point[1]:
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

X = int(input())

max_ = 0

for i in range(X):
  power=1
  while i**(power+1)<=X:
    power+=1
  if i**power>max:
    max=i**power
    
print(max_)

### FIXED CODE ###

X = int(input())

max_ = 1

for i in range(2,X):
  power=1
  while i**(power+1)<=X:
    power+=1
    
  if power==1:
    break
    
  if i**power>max_:
    #print(i)
    #print(power)
    max_=i**power
    
print(max_)
### BUGGY CODE ###

n,q = map(int, input().split())
s = input()
for _ in range(q):
    l,r = map(int, input().split())
    print(s[l-1:r].count("AC"))

### FIXED CODE ###

n,q = map(int, input().split())
s = input()
count = [0]*n
for i in range(1,n):
    count[i] += count[i-1]
    if s[i-1:i+1]=="AC": count[i]+=1
for i in range(q):
    l,r = map(int, input().split())
    print(count[r-1]-count[l-1])
### BUGGY CODE ###

n = int(input())
t = [0 for _ in range(n+1)]
x = [0 for _ in range(n+1)]
y = [0 for _ in range(n+1)]
for i in range(n):
    t[i+1], x[i+1], y[i+1] = [int(z) for z in input().split()]
    # print(t,x,y)
dt = [t[i+1] - t[i] for i in range(n)]
dist = [abs(x[i+1] - x[i]) + abs(y[i+1] - y[i]) for i in range(n)]
# print(dt, dist)
result = True

while result:
    for i in range(n):
        if not dt > dist and (t[i] % 2 != (abs(x[i])+abs(y[i])) % 2):
            result = False

if result:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

n = int(input())
t = [0 for _ in range(n+1)]
x = [0 for _ in range(n+1)]
y = [0 for _ in range(n+1)]
for i in range(n):
    t[i+1], x[i+1], y[i+1] = [int(z) for z in input().split()]
    if t[i+1] - t[i] < abs(x[i + 1] - x[i]) + abs(y[i + 1] - y[i]) or (t[i+1] % 2 != (abs(x[i+1]) + abs(y[i+1])) % 2):
        print('No')
        exit()
print('Yes')
### BUGGY CODE ###

import sys

def input():
    return sys.stdin.readline().rstrip()

if __name__ == "__main__":
    n = int(input())
    sl = [''.join(sorted(input())) for i in range(n)]

    sset = set(sl)

    ans = 0
    for s in sset:
        scount = sl.count(s)
        ans += sum([i for i in range(scount)])

    print(ans)


### FIXED CODE ###

import sys
import collections

def input():
    return sys.stdin.readline().rstrip()

if __name__ == "__main__":
    n = int(input())
    sl = [''.join(sorted(input())) for i in range(n)]
    c = collections.Counter(sl)

    dict = collections.defaultdict(int)
    for v in c.values():
        if v == 1:
            continue

        dict[v] += 1

    ans = 0
    for k, v in dict.items():
        ans += sum([i for i in range(k)]) * v

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

N=int(input())
A=list(map(int,input().split()))
count=0
ans=1
if(N%2==0):
  flag=True
  for i in range(N//2):
    try:
      A.remove(2*i+1)
      try:
        A.remove(2*i+1)
        count+=1
        ans=(ans*2)%(10**9+7)
      except:
        flag=False
        break
    except:
      flag=False
      break
else:
  flag=True
  for i in range((N+1)//2):
    if(i==0):
      try:
        A.remove(0)
      except:
        flag=False
        break
    else:
      try:
        A.remove(2*i)
        try:
          A.remove(2*i)
          count+=1
          ans=(ans*2)%(10**9+7)
        except:
          flag=False
          break
      except:
        flag=False
        break
if(flag==True):
  print(ans)
else:
  print(0)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
count=0
ans=1
A.sort()
if(N%2==0):
  flag=True
  for i in range(N):
    if(i%2==0):
      if(A[i]!=i+1):
        flag=False
        break
    else:
      if(A[i]!=i):
        flag=False
        break
      else:
        ans=(ans*2)%(10**9+7)
else:
  flag=True
  if(A[0]!=0):
    flag=False
  for i in range(N):
    if(i%2==1):
      if(A[i]!=i+1):
        flag=False
        break
    else:
      if(A[i]!=i):
        flag=False
        break
      else:
        if(i>0):
          ans=(ans*2)%(10**9+7)
if(flag==True):
  print(ans)
else:
  print(0)
### BUGGY CODE ###

import numpy as np
n,k=map(int,input().split())
h=np.array(list(map(int,input().split())))
c=np.zeros(n,dtype=int)
for i in range(1,n):
  c[i]=np.min(np.array([abs(h[i]-h[i-j])+c[i-j] for j in range(1,k+1) if i-j>=0]))
print(c[n-1])

### FIXED CODE ###

n, k = map(int, input().split())
hs = list(map(int, input().split()))
dp=[0]*n
dp[0]=0
for i, hi in enumerate(hs):
    if i == 0:
        continue
    s = max(0, i-k)
    dp[i] = min([ml + abs(hl - hi) for ml, hl in zip(dp[s:i], hs[s:i])])
print(dp[-1])
### BUGGY CODE ###

k,s = map(int,input().split())
ans = 0
for x in range(k+1):
    for y in range(k+1):
        z = s-x-y
        if 0 <= z <= k and x+y+z == s :
            ans += 1
print(ans)

### FIXED CODE ###

k,s = map(int,input().split())
ans = 0
for x in range(k+1):
    for y in range(k+1):
        z = s-x-y
        if 0 <= z <= k:
            ans += 1
print(ans)
### BUGGY CODE ###

N, M = map(int, input().split())
al = []
for i in range(M):
  al.append(tuple(input().split()))
al_a = []
for a in al:
  if a[1] == 'AC':
    al_a.append(a)
r1 = len(set(al_a))
al_a2 = [i[0] for i in al_a]
r2 = 0
for j in al:
  if j[1] == 'AC':
    al_a2 = [i for i in al_a2 if i != j[0]]
  elif j[1] == 'WA' and j[0] in al_a2:
    r2 += 1
  
  
print(int(r1) ,int(r2))

### FIXED CODE ###

def main():
    n, m = map(int, input().split())
    ac = [0] * n
    wanum = [0] * n
    for _ in range(m):
        p, s = input().split()
        p = int(p) - 1
        if ac[p]:
            continue
        else:
            if s == 'AC':
                ac[p] = 1
                wanum[p] *= -1
            else:
                wanum[p] -= 1
    wanum2 = [i for i in wanum if i > 0]
    print(sum(ac), sum(wanum2))

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

import sys
I = lambda:map(int,sys.stdin.readline().split())
pr = lambda x:sys.stdout.write(f'{x}\n')
sys.setrecursionlimit(2000000001)
ab = abs
n = k = ar = dp = 0

def solve(x):
	global n,k,ar,dp
	if x>=n-2: return dp[x]
	if dp[x] + 1: return dp[x]
	m = 1e9
	for i in range(x+1,min(n,x+k+1)):
		t = ab(ar[x]-ar[i])+solve(i)
		m = t if t<m else m
	dp[x] = m
	# pr(f'{x} => {dp[x]}')
	return dp[x]

def main():
	global n,ar,k,dp
	(n,k), ar = [[*map(int,s.split())] for s in [*open(0)]]
	dp = [-1]*(n-2) + [ab(ar[-2]-ar[-1]),0]
	# pr((n,k,ar,dp))
	pr(solve(0))

main()

### FIXED CODE ###

import sys
from functools import reduce
sys.setrecursionlimit(2000000001)

def solve(n,k,ar,dp,x):
	ab = abs; mn = min
	if x>=n-2: return dp[x]
	if not dp[x] is -1: return dp[x]
	# t = [ for i in range(x+1,mn(n,x+k+1))]
	t = map(lambda i:ab(ar[x]-ar[i])+solve(n,k,ar,dp,i), range(x+1,mn(n,x+k+1)))
	dp[x] = reduce(lambda a,b : a if a < b else b,t)
	return dp[x]

def main():
	I = lambda:map(int,sys.stdin.readline().split())
	pr = lambda x:sys.stdout.write(f'{x}\n')
	n,k, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	pr(solve(n,k,ar,dp,0))

main()
### BUGGY CODE ###

from collections import deque
from collections import Counter

s=input()
t=input()


ds=Counter(s)
dt=Counter(t)

for k in dt.keys():
    if not k in ds.keys():
        print(-1)
        exit()

Ls=len(s)
Lt=len(t)


T=deque(list(t))

cnt=0
idx=0
while T:
    t1=T.popleft()
    temp=""
    for i in range(Ls):
        if t1==s[i]:
            temp+=t1
            idx+=1
            if idx==Lt:
                print(cnt*Ls+i+1)
                exit()
            else:
                t1=T.popleft()
    cnt+=1
    T.appendleft(t1)



### FIXED CODE ###

s=input()
t=input()

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

len_s=len(s)
len_t=len(t)


for i in range(len_s):
    idx=ord(s[i])-97
    pos[idx].append(i)

ans=[]
tmp=-1
cnt=0
for i in range(len_t):
    idx=ord(t[i])-97
    if len(pos[idx])==0:
        print(-1)
        exit()

    if tmp<pos[idx][-1]:
        l=-1
        r = len(pos[idx]) - 1
        while l + 1 < r:
            mid = (l + r) // 2
            if pos[idx][mid] <= tmp:
                l = mid
            else:
                r=mid
        ans.append(pos[idx][r])
        tmp=pos[idx][r]
    else:
        cnt+=1
        ans.append(pos[idx][0])
        tmp=pos[idx][0]


print(cnt*len_s+ans[-1]+1)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
rain = [0]*n
sign = [-1, 1]
for i in range(n):
    ex = a[:i] + a[i+1:]
    even = [ex[i] for i in range(n-1) if i%2 == 0]
    odd = [ex[i] for i in range(n-1) if i%2 == 1]
    rain[i] += a[i] + sign[i%2]*sum(even) + sign[(i+1)%2]*sum(odd)
print(*rain)


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
rain = [0]*n
rain[0] += sum([a[i] for i in range(n) if i%2 == 0]) - sum([a[i] for i in range(n) if i%2 == 1])
for i in range(n-1):
    rain[i+1] = 2 * (a[i] - rain[i]//2)
print(*rain)
### BUGGY CODE ###

from collections import Counter
from functools import reduce
from operator import mul

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    b = Counter(a)
    return b

N = int(input())
A = list(map(int, input().split()))
if N == 1:
    print('1')
else:
    fac = prime_factorize(A[0]) | prime_factorize(A[1])
    for x in A[2:]:
        fac |= prime_factorize(x)
    ans = 0
    f = [pow(x[0],x[1],1000000007) for x in fac.items()]
    if len(f) > 0:
        lcm = reduce(mul,f)
    else:
        lcm = 1
    for x in A:
        ans = (ans + (lcm // x)) % 1000000007
    print(ans)

### FIXED CODE ###

from fractions import gcd

N = int(input())
A = list(map(int, input().split()))
if N == 1:
    print('1')
else:
    g = gcd(A[0],A[1])
    lcm = (A[0]*A[1]) // g
    for x in A[2:]:
        lcm = (lcm * x) // gcd(lcm,x)
    print(sum((lcm//x) for x in A)%1000000007)
### BUGGY CODE ###

s = input()
cnt = 0
while True:
    if "ABC" in s:
        cnt += s.count("ABC")
        s = s.replace("ABC", "BCA")
    else:
        break
print(cnt)

### FIXED CODE ###

s = input()
s = s.replace("BC", "X")
cnt, ans = 0, 0
for i in s:
    if i == "A":
        cnt += 1
    elif i == "X":
        ans += cnt
    else:
        cnt = 0
print(ans)
### BUGGY CODE ###

def fun(x):
    ret = 1
    while x:
        ret *= x
        x -= 1
    return ret

def cmb(n,r):
    if n==r: return 1
    else: return fun(n) // (fun(r)*fun(n-r))

n = int(input())
ss = [sorted(input()) for i in range(n)]
ss.sort()

tmp = ss[0]
sss = [0]*n
j = 0
for i in range(n):
    if tmp == ss[i]: sss[j]+=1
    else:
        tmp = ss[i]
        j += 1
        sss[j]+=1

ans = 0
for i in sss:
    if i>1: ans += cmb(i,2)
print(ans)

### FIXED CODE ###

n = int(input())
d = {}

for i in range(n):
    s = "".join(sorted(input()))
    if s in d: d[s] += 1
    else: d[s] = 1
ans = 0
for i in d.values():
    ans += (i*i-i)//2
print(ans)