AtCoder Beginner Contest 138 - Strings of Impurity

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.
 - Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

-----Notes-----
 - A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

-----Constraints-----
 - 1 \leq |s| \leq 10^5
 - 1 \leq |t| \leq 10^5
 - s and t consists of lowercase English letters.

-----Input-----
Input is given from Standard Input in the following format:
s
t

-----Output-----
If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

-----Sample Input-----
contest
son

-----Sample Output-----
10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.
On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.
Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Difficulty: MEDIUM

Starter code:

Solutions:
s = input()
t = input()
ans = 0
s_inx = 0
t_inx = 0
s_l = len(s)
t_l = len(t)
c = 0
while t_inx <= t_l - 1:
	try:
		temp_inx = s[s_inx:].index(t[t_inx])
		s_inx += temp_inx + 1
		t_inx += 1
		c = 0
	except:
		c += 1
		s_inx = 0
		ans += s_l
	if c == 2:
		print(-1)
		exit()
else:
	ans += s_inx
print(ans)


import sys
readline = sys.stdin.readline
S = readline().rstrip()
T = readline().rstrip()
if len(set(T) - set(S)) > 0:
	print(-1)
	return
import bisect
S2 = S * 2
from collections import defaultdict
dic = defaultdict(list)
for i in range(len(S2)):
	dic[S2[i]] += [i]
ind = -1
ans = 0
for t in T:
	targets = dic[t]
	newind = targets[bisect.bisect_left(targets, ind + 1)]
	ans += newind - ind
	if newind >= len(S):
		newind -= len(S)
	ind = newind
print(ans)


def main():
	s = str(input())
	t = str(input())
	if set(t) <= set(s):
		x = -1
		counter = 0
		for i in t:
			x = s.find(i, x + 1)
			if x == -1:
				counter += 1
				x = s.find(i)
		print(x + 1 + counter * len(s))
	else:
		print(-1)
main()


s = input()
t = input()
temp = s[:]
cnt = 0
strmax = len(t)
ans = 0
flag = True
while cnt < strmax:
	num = temp.find(t[cnt])
	if num != -1:
		ans += num + 1
		temp = temp[num + 1:]
		cnt += 1
		flag = False
	else:
		if flag:
			print('-1')
			exit(0)
		ans += len(temp)
		temp = s[:]
		flag = True
print(ans)


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)


import string
import bisect
(S, T) = (input(), input())
(lS, lT) = (len(S), len(T))
l = {s: [] for s in string.ascii_lowercase}
for i in range(lS):
	l[S[i]].append(i)
(id, ans) = (-1, 0)
for i in range(lT):
	if len(l[T[i]]) == 0:
		print(-1)
		exit()
	tmp = l[T[i]][bisect.bisect(l[T[i]], id) % len(l[T[i]])]
	if id >= tmp:
		ans += lS
	id = tmp
print(ans + id + 1)


s = input()
t = list(input())
index = -1
rep = 0
length = len(s)
ans = 0
for tar in t:
	if index + 1 == length:
		rep += 1
		index = -1
	kari_index = s[index + 1:].find(tar)
	if kari_index == -1:
		tmp = s.find(tar)
		if tmp == -1:
			ans = -1
		else:
			rep += 1
			index = tmp
	else:
		index += kari_index + 1
if ans != -1:
	ans = rep * len(s) + index + 1
print(ans)


s = str(input())
t = str(input())
if set(t).issubset(set(s)):
	p = -1
	r = 0
	for i in t:
		p = s.find(i, p + 1)
		if p == -1:
			r += 1
			p = s.find(i)
	print(p + 1 + len(s) * r)
else:
	print(-1)


from collections import Counter, defaultdict
import bisect
s = input()
t = input()
ss = s * 2
S = Counter(s)
T = Counter(t)
for i in T.keys():
	if i not in S:
		print(-1)
		return
alpha = [[] for _ in range(26)]
for (i, j) in enumerate(ss):
	alpha[ord(j) - ord('a')].append(i)
(rep, now) = (0, -1)
for i in t:
	alp = alpha[ord(i) - ord('a')]
	i = bisect.bisect_left(alp, now + 1)
	now = alp[i]
	if now >= len(s):
		now -= len(s)
		rep += 1
print(rep * len(s) + now + 1)


S = input()
T = input()
n = len(S)
al = [chr(c) for c in range(ord('a'), ord('a') + 26)]
x = {a: [] for a in al}
for i in range(len(S)):
	for a in al:
		if a == S[i]:
			x[a].append(i)

def is_ok(arg):
	if x[t][arg] + c * n > i:
		return True
	return False

def meguru_bisect(ng, ok):
	while abs(ok - ng) > 1:
		mid = (ok + ng) // 2
		if is_ok(mid):
			ok = mid
		else:
			ng = mid
	return ok
i = -1
c = 0
for t in T:
	if not x[t]:
		print(-1)
		break
	if x[t][-1] + c * n <= i:
		c += 1
	i = x[t][meguru_bisect(-1, len(x[t]) - 1)] + c * n
else:
	print(i + 1)


import bisect
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
	D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for (i, s) in enumerate(S):
	n = ord(s) - 97
	D[n][P[n]] = i + 1
	P[n] += 1
pos = 0
loop = 0
for t in T:
	n = ord(t) - 97
	if D[n] == []:
		print(-1)
		break
	_d = bisect.bisect(D[n], pos)
	_pos = D[n][_d % len(D[n])]
	if _pos <= pos:
		loop += 1
	pos = _pos
else:
	print(len(S) * loop + pos)


s = input()
t = input()
ds = {}
for i in range(len(s)):
	if s[i] in ds:
		ds[s[i]].append(i + 1)
	else:
		ds[s[i]] = [i + 1]
n = len(s)
now = 0
ans = 0
from bisect import bisect_right
for ti in t:
	if ti not in ds:
		print(-1)
		exit()
	tmp = bisect_right(ds[ti], now)
	if tmp == len(ds[ti]):
		get = n + ds[ti][0]
	else:
		get = ds[ti][tmp]
	ans += get - now
	now = get % n
print(ans)


import string
import bisect
s = input()
t = input()
S = [[] for _ in range(26)]
a = string.ascii_lowercase
now = -1
ans = -1
count = 0
for i in range(len(s)):
	for j in range(26):
		if s[i] == a[j]:
			S[j].append(i)
			break
for i in range(len(t)):
	for j in range(26):
		if t[i] == a[j]:
			break
	if len(S[j]) == 0:
		break
	nex = bisect.bisect_right(S[j], now)
	if nex == len(S[j]) or now >= S[j][nex]:
		count += 1
		now = S[j][0]
	else:
		now = S[j][nex]
	if i == len(t) - 1:
		ans = count * len(s) + now + 1
print(ans)


import bisect
s = list(input())
t = list(input())
n = len(s)
p = [[] for _ in range(26)]
for i in range(n):
	p[ord(s[i]) - 97].append(i + 1)
res = 0
b = 0
for e in t:
	k = ord(e) - 97
	if len(p[k]) == 0:
		print(-1)
		exit()
	else:
		r = bisect.bisect_right(p[k], b)
		if len(p[k]) > r:
			res += p[k][r] - b
			b = p[k][r]
		else:
			res += n - b + p[k][0]
			b = p[k][0]
print(res)


from bisect import bisect_left
S = input()
T = input()
if any((c not in S for c in T)):
	print(-1)
	return
N = len(S)
pos = {c: [] for c in 'abcdefghijklmnopqrstuvwxyz'}
for i in range(N):
	pos[S[i]].append(i)
loop = 0
cur = 0
for c in T:
	nxt = bisect_left(pos[c], cur)
	if nxt == len(pos[c]):
		loop += 1
		cur = pos[c][0] + 1
	else:
		cur = pos[c][nxt] + 1
print(loop * N + cur)


from collections import defaultdict
from bisect import bisect_right
s = input()
t = input()
idxs = defaultdict(list)
for (i, c) in enumerate(s):
	idxs[c].append(i)
for (i, c) in enumerate(s):
	idxs[c].append(len(s) + i)
(ans, cur_i) = (-1, -1)
for c in t:
	if c not in idxs:
		print(-1)
		break
	a = idxs[c]
	nxt = a[bisect_right(a, cur_i)]
	ans += nxt - cur_i
	cur_i = nxt % len(s)
else:
	print(ans + 1)


import bisect
s = input() * 2
lens = len(s) // 2
t = input()
appear = {ch: [] for ch in 'abcdefghijklmnopqrstuvwxyz'}
cnt = -1
bis = bisect.bisect_right
ans = 0
for (i, ch) in enumerate(s):
	appear[ch].append(i)
for j in t:
	if appear[j]:
		idx = bis(appear[j], cnt)
		ans += appear[j][idx] - cnt
		cnt = appear[j][idx] % lens
	else:
		print(-1)
		exit()
print(ans)


import collections
from bisect import bisect_right
d = collections.defaultdict(list)
s = input()
t = input()
for (i, S) in enumerate(s, 1):
	d[S].append(i)
k = 0
ans = 0
for i in t:
	l = d[i]
	if len(l) == 0:
		print(-1)
		quit()
	if l[-1] <= ans:
		ans = l[0]
		k += 1
	else:
		ans = l[bisect_right(l, ans)]
print(len(s) * k + ans)


from bisect import bisect_right
s = input()
t = input()
alpha = [[] for _ in range(26)]

def f(c):
	return ord(c) - ord('a')
for (i, c) in enumerate(s):
	alpha[f(c)].append(i)
idx = -1
loop = 0
for c in t:
	lst = alpha[f(c)]
	if not lst:
		ans = -1
		break
	i = bisect_right(lst, idx)
	if i == len(lst):
		loop += 1
		idx = lst[0]
	else:
		idx = lst[i]
else:
	ans = len(s) * loop + idx + 1
print(ans)


s = input()
t = input()
s_ = s[:]
ti = 0
ans = 0
flag = True
while ti < len(t):
	si = s_.find(t[ti])
	if si != -1:
		ans += si + 1
		s_ = s_[si + 1:]
		ti += 1
		flag = False
	else:
		if flag:
			print(-1)
			exit(0)
		ans += len(s_)
		s_ = s[:]
		flag = True
print(ans)


s = input()
t = input()
max = 10 ** 100
now = t[0]
cnt1 = 0
cnt2 = 0
for i in t:
	if s.find(i) == -1:
		print('-1')
		exit(0)
while True:
	temp = s[:]
	if cnt2 == len(t):
		print(cnt1)
		break
	if max == 0:
		print('-1')
		break
	while True:
		num = temp.find(now)
		if num != -1:
			cnt2 += 1
			cnt1 += num + 1
			if cnt2 < len(t):
				now = t[cnt2]
			else:
				break
			temp = temp[num + 1:]
			continue
		else:
			cnt1 += len(temp)
			break
	max -= 1


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:
		print(-1)
		exit()
	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] + LS - y
	x = next_x
ans = x + 1
print(ans)


s = input()
t = input()
s_rm = s
ans = 0
for i in t:
	if i in s_rm:
		index = s_rm.index(i)
		s_rm = s_rm[index + 1:]
		ans += index + 1
	elif i in s:
		index = s.index(i)
		ans += len(s_rm) + index + 1
		s_rm = s[index + 1:]
	else:
		ans = -1
		break
print(ans)


import bisect
s = input()
t = input()
sset = set(s)
tset = set(t)
if not all((i in sset for i in tset)):
	print(-1)
	exit()
d = {}
for x in tset:
	d[x] = sorted([j for (j, y) in enumerate(s) if x == y])
rep = 0
tmp = d[t[0]][0]
n = len(t)
for i in range(1, n):
	k = d[t[i]]
	x = bisect.bisect_right(k, tmp)
	if x == len(k):
		rep += 1
		tmp = k[0]
	else:
		tmp = k[x]
print(rep * len(s) + tmp + 1)


import sys
s = input()
t = input()
wrap_count = 0
index = -1
for c in t:
	index = s.find(c, index + 1)
	if index != -1:
		continue
	wrap_count += 1
	index = s.find(c)
	if index == -1:
		print(-1)
		sys.exit()
print(len(s) * wrap_count + index + 1)


def main():
	from bisect import bisect_left
	S = input()
	T = input()
	L = len(S)
	pos = tuple((list() for _ in range(26)))
	orda = ord('a')
	for (i, c) in enumerate(S):
		j = ord(c) - orda
		pos[j].append(i)
	rep = 0
	curr = 0
	for c in T:
		j = ord(c) - orda
		if not pos[j]:
			print(-1)
			return
		i = bisect_left(pos[j], curr)
		if i == len(pos[j]):
			rep += 1
			i = 0
		curr = pos[j][i] + 1
	print(rep * L + curr)

def __starting_point():
	main()
__starting_point()


from collections import defaultdict
from bisect import bisect_left, bisect_right
s = input()
t = input()
n = len(s)
ds = defaultdict(list)
for i in range(n * 2):
	ds[s[i % n]].append(i)
ans = 0
x = -1
for i in range(len(t)):
	if t[i] not in ds:
		ans = -1
		break
	x_next = ds[t[i]][bisect_right(ds[t[i]], x)]
	ans += x_next - x
	x = x_next % n
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
s = input()
n = len(s)
t = input()
s *= 2
ans = 0
for c in t:
	d = s[ans % n:].find(c)
	if d == -1:
		print(-1)
		exit()
	ans += d + 1
print(ans)


from bisect import *
s = input()
t = input()
if set(t) <= set(s):
	d = {}
	for (k, j) in enumerate(s * 2):
		if j in d:
			d[j].append(k)
		else:
			d[j] = [k]
	now = -1
	ans = 0
	for i in t:
		nex = d[i][bisect_right(d[i], now)]
		ans += nex - now
		if nex >= len(s):
			now = nex - len(s)
		else:
			now = nex
	print(ans)
else:
	print(-1)


from bisect import bisect_right
s = input()
N = len(s)
t = input()
d = {chr(i + ord('a')): [] for i in range(26)}
for (i, c) in enumerate(s):
	d[c].append(i + 1)
acc = 0
b = 0
for c in t:
	if len(d[c]) == 0:
		acc = -1
		break
	q = bisect_right(d[c], b)
	a = d[c][q if q != len(d[c]) else 0]
	if a > b:
		(acc, b) = (acc + a - b, a)
	else:
		(acc, b) = (acc + N - b + a, a)
print(acc)


from bisect import *
S = input()
T = input()
ds = {}
for (i, s) in enumerate(S):
	if s not in ds:
		ds[s] = [i]
	else:
		ds[s].append(i)
ind = -1
cnt = 0
for t in T:
	if t not in ds:
		ans = -1
		break
else:
	for t in T:
		if ds[t][-1] <= ind:
			ind = ds[t][0]
			cnt += 1
		else:
			ind = ds[t][bisect_right(ds[t], ind)]
	ans = cnt * len(S) + ind + 1
print(ans)


S = input()
T = input()
pos = 0
for s in T:
	if not s in S:
		pos = -1
		break
	if s in S[pos % len(S):]:
		pos += S[pos % len(S):].index(s) + 1
	else:
		pos += S.index(s) + len(S[pos % len(S):]) + 1
print(pos)


s = input()
l = input()
count = 0
ind = -1
if set(l).issubset(set(s)):
	for i in range(len(l)):
		ind = s.find(l[i], ind + 1)
		if ind == -1:
			count += 1
			ind = s.find(l[i], ind + 1)
	print(count * len(s) + ind + 1)
else:
	print(-1)


import bisect
s = input()
l = len(s)
ch = [[] for _ in range(26)]
s *= 2
for i in range(l * 2):
	ch[ord(s[i]) - ord('a')].append(i)
t = input()
ans = 0
now = -1
for i in t:
	idx = ord(i) - ord('a')
	if not ch[idx]:
		print(-1)
		break
	temp = bisect.bisect_right(ch[idx], now)
	ans += ch[idx][temp] - now
	now = ch[idx][temp] % l
else:
	print(ans)


s = input()
t = input()
r = 0
p = 0
x = 0
for i in t:
	if s.find(i) == -1:
		print(-1)
		exit()
	if p > s.find(i):
		if s[p:].find(i) == -1:
			x += 1
			p = s.find(i) + 1
		else:
			p = p + s[p:].find(i) + 1
	else:
		p = s.find(i) + 1
print(x * len(s) + p)


from bisect import bisect_left
s = input()
t = input()
n = len(s)
c = [[] for _ in range(26)]
for (i, x) in enumerate(s + s):
	c[ord(x) - ord('a')].append(i)
ans = 0
p = -1
for x in t:
	d = ord(x) - ord('a')
	if len(c[d]) == 0:
		print(-1)
		exit()
	p = c[d][bisect_left(c[d], p + 1)]
	if p >= n:
		p -= n
		ans += 1
print(ans * n + p + 1)


from collections import defaultdict
from bisect import bisect_left
S = input()
T = input()
count = defaultdict(list)
for (i, s) in enumerate(S):
	count[s] += [i]
idx = 0
cnt = 0
for t in T:
	idxs = count[t]
	if not idxs:
		print(-1)
		exit()
	i = bisect_left(idxs, idx)
	if i < len(idxs):
		idx = idxs[i] + 1
	else:
		idx = idxs[0] + 1
		cnt += 1
ans = cnt * len(S) + idx
print(ans)


import sys
from string import ascii_lowercase as a2z
input = sys.stdin.readline

def main():
	S = input().rstrip()
	T = input().rstrip()
	N = len(S)
	memo = [None] * N
	memo[-1] = {alphabet: -1 for alphabet in a2z}
	memo[-1][S[-1]] = N - 1
	for i in reversed(list(range(N - 1))):
		memo[i] = memo[i + 1].copy()
		memo[i][S[i]] = i
	ans = 0
	idx_S = 0
	for t in T:
		i = memo[idx_S][t]
		if i == -1:
			ans += N - idx_S
			idx_S = 0
			i = memo[idx_S][t]
			if i == -1:
				ans = -1
				break
			else:
				ans += i - idx_S + 1
				if i == N - 1:
					idx_S = 0
				else:
					idx_S = i + 1
		else:
			ans += i - idx_S + 1
			if i == N - 1:
				idx_S = 0
			else:
				idx_S = i + 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
import math
import bisect
import heapq
from collections import defaultdict
S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False] * 26
F2 = [False] * 26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
	F1[alpha.index(S[i])] = True
for i in range(lenT):
	F2[alpha.index(T[i])] = True
for i in range(26):
	if not F1[i] and F2[i]:
		print(-1)
		return
ind = S.index(T[0])
ans = ind + 1
for i in range(1, lenT):
	S = S[ind + 1:] + S[:ind + 1]
	ind = S.index(T[i])
	ans += ind + 1
print(ans)


import bisect

def main() -> None:
	S = input()
	T = input()
	SS = S + S
	alphas = [[] for _ in range(26)]
	for (i, s) in enumerate(SS):
		alphas[ord(s) - ord('a')].append(i)
	(rep, now) = (0, -1)
	for t in T:
		alpha = alphas[ord(t) - ord('a')]
		i = bisect.bisect_left(alpha, now + 1)
		if i == len(alpha):
			print(-1)
			return
		now = alpha[i]
		if now >= len(S):
			now -= len(S)
			rep += 1
	print(rep * len(S) + now + 1)

def __starting_point():
	main()
__starting_point()


s = input()
t = input()
N = len(s)
s2 = s + s
cs = set(t)
table = {}
for c in cs:
	if s.find(c) < 0:
		table[c] = [-1] * N
		continue
	l = [-2] * N
	p = 0
	while p < N:
		i = s2.find(c, p)
		while p <= i:
			if p < N:
				l[p] = i - p
			p += 1
	table[c] = l
r = 0
for c in t:
	d = table[c][r % N]
	if d < 0:
		print(-1)
		exit()
	r += d + 1
print(r)


from bisect import bisect_left
from string import ascii_lowercase

def main():
	s = input()
	t = input()
	set_s = set(tuple(s))
	set_t = set(tuple(t))
	if not set_t <= set_s:
		print(-1)
		return
	ans = 0
	d = {c: [] for c in ascii_lowercase}
	for (i, c) in enumerate(s):
		d[c].append(i + 1)
	now = 0
	for c in t:
		idx = bisect_left(d[c], now + 1)
		if idx == len(d[c]):
			ans += len(s)
			now = 0
			idx = bisect_left(d[c], now + 1)
		now = d[c][idx]
	ans += now
	print(ans)

def __starting_point():
	main()
__starting_point()


from bisect import bisect_left
S = input()
T = input()
ss = set(list(S))
st = set(list(T))
if not st <= ss:
	print(-1)
	return
alpha = {chr(97 + i): [] for i in range(26)}
for (i, s) in enumerate(S):
	alpha[s].append(i + 1)
n = len(S)
now = 0
ans = 0
for t in T:
	if alpha[t][-1] <= now:
		ans += n - now + alpha[t][0]
		now = alpha[t][0]
	else:
		i = bisect_left(alpha[t], now + 1)
		ans += alpha[t][i] - now
		now = alpha[t][i]
print(ans)


import sys
s1 = input()
s2 = input()
n1 = 0
t1 = 0
t2 = -1
for i1 in range(len(s2)):
	t1 = s1.find(s2[i1], t2 + 1)
	if t1 == -1:
		t1 = s1.find(s2[i1], 0, t2 + 1)
		if t1 == -1:
			print(t1)
			sys.exit()
		else:
			n1 += t1 + (len(s1) - t2)
	else:
		n1 += t1 - t2
	t2 = t1
print(n1)


import sys
s = str(input())
t = list(input())
string = s
l = 1
for i in range(len(t)):
	if s.find(t[i]) == -1:
		print(-1)
		sys.exit()
	elif string.find(t[i]) == -1:
		l += 1
		string = s[s.find(t[i]) + 1:]
	else:
		string = string[string.find(t[i]) + 1:]
print(l * len(s) - len(string))


from collections import defaultdict
import bisect
S = input()
T = input()
d = defaultdict(list)
for (i, s) in enumerate(S):
	d[s].append(i)
ans = 0
now = -1
for t in T:
	arr = d[t]
	if len(arr) == 0:
		print(-1)
		exit()
	idx = bisect.bisect_right(arr, now)
	if idx == len(arr):
		ans += 1
		now = arr[0]
	else:
		now = arr[idx]
print(ans * len(S) + now + 1)


s = input()
t = input()
i = 0
index = -1
ans = 0
while i < len(t):
	if t[i] not in s:
		break
	elif t[i] not in s[index + 1:]:
		ans += len(s) - index + s.index(t[i])
		index = s.index(t[i])
		i += 1
	else:
		ans += s[index + 1:].index(t[i]) + 1
		index += s[index + 1:].index(t[i]) + 1
		i += 1
print(ans if i == len(t) else -1)


from collections import defaultdict
from bisect import bisect
s = input()
S = len(s)
t = input()
T = len(t)
D = defaultdict(list)
Dc = defaultdict(int)
Exists = True
for i in range(S):
	D[s[i]].append(i + 1)
	Dc[s[i]] += 1
ans = 0
temp = 0
for i in range(T):
	if D[t[i]] == []:
		Exists = False
		break
	j = bisect(D[t[i]], temp)
	if j == Dc[t[i]]:
		ans += S - temp + D[t[i]][0]
		temp = D[t[i]][0]
	else:
		ans += D[t[i]][j] - temp
		temp = D[t[i]][j]
if Exists:
	print(ans)
else:
	print(-1)


import bisect
from collections import defaultdict
s = input()
n = len(s)
t = input()
if set(s) & set(t) != set(t):
	print(-1)
else:
	cnt = defaultdict(list)
	for i in range(n):
		cnt[s[i]].append(i + 1)
	loop = 0
	prev = 0
	for c in t:
		x = cnt[c]
		idx = bisect.bisect_right(x, prev)
		if idx < len(x):
			prev = x[idx]
		else:
			prev = x[0]
			loop += 1
	print(loop * n + prev)


S = input()
T = input()
if not set(T) <= set(S):
	print(-1)
	exit()
now = -1
rep = 0
for t in T:
	now = S.find(t, now + 1)
	if now == -1:
		rep += 1
		now = S.find(t)
print(rep * len(S) + now + 1)


s = input()
t = input()
(ls, lt) = (len(s), len(t))
if not set(t) <= set(s):
	print(-1)
	exit()
co = 0
ind = -1
for i in range(lt):
	ind = s.find(t[i], ind + 1)
	if ind == -1:
		ind = s.find(t[i])
		co += 1
print(co * ls + ind + 1)


from bisect import bisect_right
s = input()
t = input()
n = len(s)
mp = [[] for _ in range(26)]
s += s
for i in range(len(s)):
	mp[ord(s[i]) - ord('a')].append(i)
ans = 0
now = -1
for i in range(len(t)):
	a = ord(t[i]) - ord('a')
	nxt = bisect_right(mp[a], now)
	if len(mp[a]) == nxt:
		print(-1)
		exit()
	ans += mp[a][nxt] - now
	now = mp[a][nxt]
	if now >= n:
		now -= n
print(ans)


from bisect import *
s = input()
t = input()
pos = {}
for (j, i) in enumerate(s):
	if i in pos.keys():
		pos[i].append(j + 1)
	else:
		pos[i] = [j + 1]
n = len(s)
for i in pos.keys():
	pos[i].append(pos[i][0] + n)
ans = 0
last = 0
for i in t:
	if not i in s:
		print(-1)
		exit()
	last = pos[i][bisect(pos[i], last)]
	if last > n:
		last -= n
		ans += n
print(ans + last)


from bisect import bisect_left
import sys
s = input()
t = input()
d = {char: [] for char in 'abcdefghijklmnopqrstuvwxyz'}
for i in range(len(s)):
	d[s[i]].append(i)
cnt = 0
index = 0
for char in t:
	if not d[char]:
		print(-1)
		sys.exit()
	i = bisect_left(d[char], index)
	if i == len(d[char]):
		cnt += 1
		index = d[char][0]
	else:
		index = d[char][i]
	index += 1
ans = cnt * len(s) + index
print(ans)


import sys
from collections import defaultdict
from bisect import bisect_right
sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	S = input()
	n = len(S)
	T = input()
	pos = defaultdict(list)
	for i in range(n):
		pos[S[i]].append(i + 1)
	res = 0
	now = 0
	for t in T:
		if pos.get(t, -1) == -1:
			print(-1)
			return
		idx = bisect_right(pos[t], now)
		if len(pos[t]) == idx:
			res += n - now + pos[t][0]
			now = pos[t][0]
		else:
			res += pos[t][idx] - now
			now = pos[t][idx]
	print(res)

def __starting_point():
	resolve()
__starting_point()


import sys
sys.setrecursionlimit(int(1000000.0))
s = input()
t = input()
len_s = len(s)
set_s = set(s)
set_t = set(t)
if len(set_t - set_s) != 0:
	print(-1)
	sys.exit()
n = {}
ss = s + s
ans = 0
for c in t:
	i = ans % len_s
	if not c in n:
		n[c] = [0] * len_s
	if n[c][i] == 0:
		n[c][i] = ss[i:].find(c) + 1
	ans += n[c][i]
print(ans)


import bisect
S = input()
T = input()
A = set(S)
B = set(T)
if B - A:
	print(-1)
else:
	d = {}
	for b in B:
		d[b] = [i for i in range(len(S)) if S[i] == b]
	c = -1
	p = 0
	for t in T:
		idx = bisect.bisect_right(d[t], c)
		if idx == len(d[t]):
			c = d[t][0]
			p += 1
		else:
			c = d[t][idx]
	print(len(S) * p + c + 1)


import bisect
from collections import defaultdict
S = list(input().strip())
T = list(input().strip())
L = len(S)
if not set(T) <= set(S):
	print(-1)
	exit()
d = defaultdict(list)
for (i, s) in enumerate(S):
	d[s].append(i)
start = -1
ans = 0
for i in T:
	p = bisect.bisect_right(d[i], start)
	le = len(d[i])
	if p == le:
		ans += L - start - 1 + (d[i][0] + 1)
		start = d[i][0]
	else:
		ans += d[i][p] - start
		start = d[i][p]
print(ans)


S = input()
T = input()

def solve(S, T):
	for t in set(T):
		if t not in set(S):
			return -1
	S_double = S + S
	len_s = len(S)
	ans = -1
	for t in T:
		ans += S_double[(ans + 1) % len_s:].index(t) + 1
	return ans + 1
print(solve(S, T))


s = input()
t = input()
idx = -1
count = 0
if set(t) <= set(s):
	for i in t:
		idx = s.find(i, idx + 1)
		if idx == -1:
			idx = s.find(i)
			count += 1
	pdc = count * len(s)
	total_idx = idx + 1 + pdc
	print(total_idx)
else:
	print(-1)


def main():
	import sys
	import copy
	from bisect import bisect_right
	from collections import defaultdict
	input = sys.stdin.readline
	(s, t) = (input().strip(), input().strip())
	lens = len(s)
	index_list = defaultdict(list)
	leng = defaultdict(int)
	for (i, ss) in enumerate(s):
		index_list[ss].append(i)
	for (s, i) in index_list.items():
		if i:
			leng[s] = len(index_list[s])
	prev = -1
	ans = 0
	for tt in t:
		if tt not in leng:
			print(-1)
			return False
		p = bisect_right(index_list[tt], prev)
		if p < leng[tt]:
			prev = index_list[tt][p]
		else:
			prev = index_list[tt][0]
			ans += lens
	print(ans + prev + 1)

def __starting_point():
	main()
__starting_point()


from bisect import bisect_left
from string import ascii_lowercase
s = input()
t = input()
n = len(s)
dict_s = {c: [] for c in ascii_lowercase}
for (i, c) in enumerate(s * 2):
	dict_s[c].append(i)
for c in ascii_lowercase:
	if not dict_s[c]:
		del dict_s[c]
dict_next_i = {c: [dict_s[c][bisect_left(dict_s[c], i + 1)] for i in range(n)] for c in list(dict_s.keys())}

def next_i(i, c):
	return n * (i // n) + dict_next_i[c][i % n]

def answer():
	if not all((c in s for c in t)):
		return -1
	i = -1
	for c in t:
		i = next_i(i, c)
	return i + 1
print(answer())


S = input()
T = input()
n = len(S)
ans = 0
pre = -1
for c in T:
	pos = S.find(c, pre + 1, n)
	if pos != -1:
		ans += pos - pre
		pre = pos
	else:
		pos = S.find(c, 0, pre + 1)
		if pos != -1:
			ans += n + pos - pre
			pre = pos
		else:
			ans = -1
			break
print(ans)


s = input()
t = input()
cnt = 0
k = 0
for i in t:
	f = s.find(i, k)
	if f < 0:
		cnt += 1
		f = s.find(i)
		if f == -1:
			print(-1)
			exit()
		else:
			k = f + 1
	else:
		k = f + 1
print(cnt * len(s) + k)


import bisect
s = input()
t = input()
n = len(s)
s += s
pos = [[] for _ in range(26)]
for i in range(len(s)):
	pos[ord(s[i]) - ord('a')].append(i)
ans = 0
now = -1
for i in range(len(t)):
	c = ord(t[i]) - ord('a')
	j = bisect.bisect_right(pos[c], now)
	if j == len(pos[c]):
		print(-1)
		return
	now = pos[c][j]
	if now >= n:
		now -= n
		ans += n
print(ans + now + 1)


import bisect

def main():
	S = input()
	dS = {c: [] for c in 'abcdefghijklmnopqrstuvwxyz'}
	for (i, s) in enumerate(S):
		dS[s].append(i)
	T = input()
	ans = 0
	p = 0
	for t in T:
		if not dS[t]:
			print(-1)
			return
		if dS[t][-1] < p:
			p = 0
			ans += len(S)
		idx = bisect.bisect_left(dS[t], p)
		p = dS[t][idx] + 1
	print(ans + p)
main()


import collections, math
s = input()
t = input()
ls = collections.Counter(s)
lt = collections.Counter(t)
time_dict = {}
tmp = 0
max_time = 0
for (t_key, t_val) in list(lt.items()):
	if t_key not in ls:
		print(-1)
		return
cnt = 0
tt = list(t[::-1])
idx = 0
ss = s
while tt != []:
	next_t = tt.pop()
	idx = ss.find(next_t)
	if idx == -1:
		cnt += len(ss)
		idx = s.find(next_t)
		ss = s[idx + 1:]
		cnt += idx + 1
	else:
		cnt += idx + 1
		ss = ss[idx + 1:]
print(cnt)


import bisect
s = input()
t = input()
alphabet = [[] for _ in range(26)]
n = len(s)
s += s
for i in range(len(s)):
	alphabet[ord(s[i]) - ord('a')].append(i)
ans = 0
now = -1
for i in range(len(t)):
	t_i = ord(t[i]) - ord('a')
	j = bisect.bisect_right(alphabet[t_i], now)
	if j == len(alphabet[t_i]):
		print(-1)
		return
	else:
		now = alphabet[t_i][j]
		if now >= n:
			now -= n
			ans += n
print(ans + now + 1)


from collections import defaultdict
from bisect import bisect_right
(s, t) = (input(), input())
m = len(s)
if set(s) & set(t) != set(t):
	print(-1)
	exit()
dict = defaultdict(list)
for (i, j) in enumerate(s):
	dict[j] += [i + 1]
(ans, index) = (0, 0)
for i in t:
	point = bisect_right(dict[i], index)
	if point == len(dict[i]):
		num = dict[i][0]
	else:
		num = dict[i][point]
	if index >= num:
		ans += m
	index = num
print(ans + index)


from collections import *
from bisect import *
s = input()
n = len(s)
t = input()
d = defaultdict(list)
for (i, c) in enumerate(s):
	d[c].append(i)
m = -1
o = 0
for c in t:
	if not d[c]:
		print(-1)
		exit()
	i = bisect(d[c], m)
	if len(d[c]) == i:
		o += 1
		m = d[c][0]
	else:
		m = d[c][i]
print(o * n + m + 1)


from bisect import bisect_right
s = input()
N = len(s)
si = [[] for _ in range(26)]
for i in range(N):
	si[ord(s[i]) - ord('a')].append(i + 1)
ans = 0
t = input()
M = len(t)
for j in range(M):
	ti = si[ord(t[j]) - ord('a')]
	if len(ti) == 0:
		print(-1)
		exit()
	d = bisect_right(ti, ans % N)
	if d == len(ti):
		ans += ti[0] + N - ans % N
	else:
		ans += ti[d] - ans % N
print(ans)


import bisect
s = list(input())
t = list(input())
if len(set(t) - set(s)) > 0:
	print(-1)
	exit()
c = set(s)
dic = {}
for char in c:
	dic[char] = []
for i in range(len(s)):
	dic[s[i]] += [i]
last = -1
r = 0
for char in t:
	tmp = last
	idx = bisect.bisect_right(dic[char], last)
	if idx < len(dic[char]):
		last = dic[char][idx]
	if tmp == last:
		r += 1
		last = dic[char][0]
print(len(s) * r + last + 1)


from collections import defaultdict
from bisect import bisect_left

def solve(s, t):
	dd = defaultdict(list)
	for (i, c) in enumerate(s):
		dd[c].append(i)
	inv = 0
	index = 0
	for c in t:
		x = bisect_left(dd[c], index)
		if x == len(dd[c]):
			inv += 1
			if not dd[c]:
				return -1
			index = dd[c][0] + 1
		else:
			index = dd[c][x] + 1
	return len(s) * inv + index

def main():
	s = input()
	t = input()
	print(solve(s, t))

def __starting_point():
	main()
__starting_point()


import bisect
s = input()
t = input()
n = len(s)
dic = {}
for (idx, i) in enumerate(s * 2):
	if i in dic.keys():
		dic[i].append(idx)
	else:
		dic[i] = [idx]
(ans, p) = (0, -1)
for i in t:
	if i not in dic.keys():
		print(-1)
		return
	p = dic[i][bisect.bisect_left(dic[i], p + 1)]
	if p >= n:
		p -= n
		ans += n
print(ans + p + 1)


s = input()
n = len(s)
l = [[] for i in range(26)]
for (i, c) in enumerate(s * 2):
	l[ord(c) - ord('a')].append(i)
from bisect import *
shukai = 0
now = 0
t = input()
for c in t:
	x = ord(c) - ord('a')
	if len(l[x]) == 0:
		print(-1)
		exit()
	i = bisect_left(l[x], now)
	now = l[x][i] + 1
	if now >= n:
		now -= n
		shukai += 1
print(shukai * n + now)


s = input()
t = input()
cnt = 0
mod = -1
tmp_s = ''
for cha in t:
	if cha not in s:
		print(-1)
		exit()
	elif cha in tmp_s:
		idx = tmp_s.find(cha)
		mod += idx + 1
	else:
		idx = s.find(cha)
		if idx <= mod:
			cnt += 1
		mod = idx
	if mod == len(s) - 1:
		tmp_s = ' '
	else:
		tmp_s = s[mod + 1:]
print(len(s) * cnt + mod + 1)


from collections import defaultdict
import bisect
S = input()
T = input()
d = defaultdict(list)
for (i, s) in enumerate(S, start=1):
	d[s].append(i)
ans = 0
i = 0
for t in T:
	if not d[t]:
		print(-1)
		exit()
	if i >= d[t][-1]:
		ans += len(S)
		i = d[t][0]
		continue
	pos = bisect.bisect_right(d[t], i)
	i = d[t][pos]
ans += i
print(ans)


import bisect

def ctoi(char):
	return ord(char) - ord('a')
s = input()
l = len(s)
t = input()
if len(set(t) - set(s)) != 0:
	print(-1)
	exit()
pos = [[] for _ in range(26)]
for i in range(2 * l):
	pos[ctoi(s[i % l])].append(i + 1)
index = 0
loop = 0
for i in range(len(t)):
	tmp = bisect.bisect_right(pos[ctoi(t[i])], index)
	next_index = pos[ctoi(t[i])][tmp]
	if next_index >= l:
		loop += 1
	index = next_index % l
print(l * loop + index)


from collections import defaultdict
import bisect
s = input()
t = input()
counter = defaultdict(list)
for (i, c) in enumerate(s):
	counter[c].append(i)
prev = 10 ** 10
cycle = 0
for (i, c) in enumerate(t):
	if c not in counter:
		print(-1)
		exit()
	idx = counter[c]
	found = bisect.bisect_right(idx, prev)
	if found >= len(idx):
		cycle += 1
		prev = idx[0]
	else:
		prev = idx[found]
print((cycle - 1) * len(s) + prev + 1)


from collections import Counter, defaultdict
from bisect import bisect_right
s = input()
t = input()
c = Counter(s)
for e in t:
	if c[e] == 0:
		print(-1)
		return
ss = s + s
d = defaultdict(list)
for (i, e) in enumerate(ss):
	d[e].append(i)
now = -1
ans = 0
for e in t:
	i = bisect_right(d[e], now)
	nxt = d[e][i]
	ans += nxt - now
	now = nxt % len(s)
print(ans)


import string
import bisect
S = input()
T = input()
n = len(S)
sa = {_: [] for _ in string.ascii_lowercase}
for (i, s) in enumerate(S):
	sa[s] += [i]
ans = 0
i = -1
for t in T:
	if sa[t]:
		j = bisect.bisect_left(sa[t], i + 1)
		if j == len(sa[t]):
			ni = sa[t][0] + n
		else:
			ni = sa[t][j]
		ans += ni - i
		i = ni % n
	else:
		print(-1)
		exit()
print(ans)


s = input()
t = input()
wrap = 0
index = -1
for i in t:
	index = s.find(i, index + 1)
	if index != -1:
		continue
	wrap += 1
	index = s.find(i)
	if index == -1:
		print(-1)
		exit()
print(len(s) * wrap + index + 1)


from bisect import bisect_left
from collections import defaultdict
S = input()
T = input()
a = 97
grid = defaultdict(list)
for (idx, s) in enumerate(S + S):
	grid[s].append(idx)
ans = 0
cur = 0
lenS = len(S)
for t in T:
	if t in grid:
		tList = grid[t]
		ncur = tList[bisect_left(tList, cur)]
		ans += ncur - cur + 1
		cur = (ncur + 1) % lenS
	else:
		ans = -1
		break
print(ans)


import sys
sys.setrecursionlimit(200000)
from collections import Counter, defaultdict
from bisect import bisect_left

def read():
	S = input().strip()
	T = input().strip()
	return (S, T)

def have_ahswer(S, T):
	cs = Counter(S)
	ct = Counter(T)
	for k in list(ct.keys()):
		if cs[k] == 0:
			return False
	return True

def solve(S, T):
	Z = 'abcdefghijklmnopqrstuvwxyz'
	if not have_ahswer(S, T):
		return -1
	D = defaultdict(list)
	for (i, s) in enumerate(S):
		D[s].append(i)
	ans = 0
	prev_index = -1
	N = len(S)
	for t in T:
		a = D[t]
		i = bisect_left(a, prev_index + 1)
		if i < len(a):
			next_index = a[i]
			ans += next_index - prev_index
		else:
			next_index = a[0]
			ans += N + next_index - prev_index
		prev_index = next_index
	return ans

def __starting_point():
	inputs = read()
	print('{}'.format(solve(*inputs)))
__starting_point()


from bisect import bisect_left
S = input()
T = input()
memo = {}
for (i, s) in enumerate(S):
	if s not in memo:
		memo[s] = [i]
	else:
		memo[s].append(i)
cnt = 0
pre = -1
for t in T:
	if t not in memo:
		print(-1)
		quit()
	m = bisect_left(memo[t], pre + 1)
	if m >= len(memo[t]):
		cnt += 1
		pre = memo[t][0]
	else:
		pre = memo[t][m]
print(cnt * len(S) + pre + 1)


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)
		return
	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)


import sys
import math
from collections import deque
import bisect
sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().strip()
NI = lambda : int(input())
NMI = lambda : map(int, input().split())
NLI = lambda : list(NMI())
SI = lambda : input()

def main():
	S = SI()
	T = SI()
	if set(S) | set(T) != set(S):
		print(-1)
		return
	s2i = {}
	for (i, s) in enumerate(S):
		if s in s2i:
			s2i[s].append(i)
		else:
			s2i[s] = [i]
	now = 0
	digit = -1
	for t in T:
		target = bisect.bisect_right(s2i[t], digit)
		if target == len(s2i[t]):
			digit = s2i[t][0]
			now += 1
		else:
			digit = s2i[t][target]
	print(now * len(S) + digit + 1)

def __starting_point():
	main()
__starting_point()


from bisect import bisect_right
s = tuple(map(lambda c: ord(c) - ord('a'), input()))
t = tuple(map(lambda c: ord(c) - ord('a'), input()))
(S, T) = (set(s), set(t))
if T & S != T:
	print(-1)
	return
(d, l) = ([[] for _ in range(26)], [0] * 26)
for (idx, c) in enumerate(s):
	d[c].append(idx)
	l[c] += 1
prev = -1
loop = 0
for c in t:
	idx = bisect_right(d[c], prev)
	if idx >= l[c]:
		loop += 1
		prev = d[c][0]
	else:
		prev = d[c][idx]
print(loop * len(s) + prev + 1)


slist = input()
tlist = input()
scounter = 0
slength = len(slist)
slist = slist + slist
preposition = -1
for tchar in tlist:
	preposition = slist.find(tchar, preposition + 1)
	if preposition == -1:
		scounter = -1
		break
	elif preposition >= slength:
		scounter += slength
		preposition -= slength
if scounter == -1:
	print(scounter)
else:
	print(scounter + preposition + 1)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left, bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil, pi, factorial
from operator import itemgetter

def I():
	return int(input())

def MI():
	return list(map(int, input().split()))

def LI():
	return list(map(int, input().split()))

def LI2():
	return [int(input()) for i in range(n)]

def SI():
	return input().rstrip()

def printns(x):
	print('\n'.join(x))

def printni(x):
	print('\n'.join(list(map(str, x))))
inf = 10 ** 17
mod = 10 ** 9 + 7
s = SI()
t = SI()
a = len(s)
b = len(t)
alphasm = [chr(i) for i in range(97, 97 + 26)]
alphadic = {}
for i in range(26):
	alphadic[alphasm[i]] = i
strlis = [[] for i in range(26)]
for i in range(a):
	strlis[alphadic[s[i]]].append(i)
rd = 0
pos = -1
i = 0
while i < b:
	if len(strlis[alphadic[t[i]]]) == 0:
		print(-1)
		return
	if pos == -1:
		pos = strlis[alphadic[t[i]]][0]
		i += 1
	elif strlis[alphadic[t[i]]][-1] <= pos:
		rd += 1
		pos = -1
	else:
		pos = strlis[alphadic[t[i]]][bisect_right(strlis[alphadic[t[i]]], pos)]
		i += 1
print(rd * a + pos + 1)


import bisect
en2asc = lambda s: ord(s) - 97
S = input()
T = input()
if not set(S) >= set(T):
	print(-1)
	exit()
lst = [[] for _ in range(26)]
for (i, s) in enumerate(S):
	lst[en2asc(s)].append(i)
L = len(S)
cur = 0
for t in T:
	t = en2asc(t)
	b = bisect.bisect_left(lst[t], cur % L)
	if b == len(lst[t]):
		cur = (cur // L + 1) * L
		b = bisect.bisect_left(lst[t], cur % L)
	cur = cur // L * L + lst[t][b] + 1
print(cur)


from collections import defaultdict
from bisect import bisect_left
S = input()
T = input()
SS = S + S
lens = len(S)
index = defaultdict(list)
for (i, ss) in enumerate(SS):
	index[ss].append(i)
x = -1
for t in T:
	arr = index[t]
	if len(arr) == 0:
		x = -2
		break
	y = (x + 1) % lens
	i = bisect_left(arr, y)
	x = x + 1 + arr[i] - y
answer = x + 1
print(answer)


from collections import defaultdict
import bisect
S = input()
T = input()
d = defaultdict(list)
for (i, s) in enumerate(S, 1):
	d[s].append(i)
ans = 0
cnt = 0
for t in T:
	c = d[t]
	if len(c) == 0:
		print(-1)
		quit()
	if c[-1] <= ans:
		ans = c[0]
		cnt += 1
	else:
		ans = c[bisect.bisect_right(c, ans)]
print(len(S) * cnt + ans)


from bisect import bisect_left
from collections import defaultdict as dd

def main():
	s = input()
	t = input()
	n = len(s)
	dic = dd(list)
	for (idx, i) in enumerate(s * 2):
		dic[i].append(idx)
	(ans, p) = (0, -1)
	for i in t:
		if i not in dic.keys():
			print(-1)
			return
		p = dic[i][bisect_left(dic[i], p + 1)]
		if p >= n:
			p -= n
			ans += n
	print(ans + p + 1)

def __starting_point():
	main()
__starting_point()


from bisect import bisect

def ctoi(c):
	return ord(c) - ord('a')

def solve():
	S = input()
	T = input()
	N = len(S)
	idx = [[] for i in range(26)]
	for i in range(2 * N):
		idx[ctoi(S[i % N])].append(i)
	ans = -1
	for c in T:
		if not idx[ctoi(c)]:
			return -1
		ans += idx[ctoi(c)][bisect(idx[ctoi(c)], ans % N)] - ans % N
	return ans + 1
print(solve())


from collections import Counter
import bisect
s = input()
t = input()
sc = Counter(s)
tc = Counter(t)
for (i, j) in tc.most_common():
	if sc[i] == 0:
		print(-1)
		return
target = ord('a')
ordl = [[] for _ in range(26)]
for (i, j) in enumerate(s):
	ordl[ord(j) - target].append(i)
ordllen = [len(ordl[i]) for i in range(26)]
times = 0
cnt = -1
ans = 0
for i in t:
	k = ord(i) - target
	m = bisect.bisect_right(ordl[k], cnt)
	if m == ordllen[k]:
		times += 1
		cnt = ordl[k][0]
	else:
		cnt = ordl[k][m]
ans = times * len(s) + cnt + 1
print(ans)


from bisect import *
s = input()
t = input()
d = {}
for (i, c) in enumerate(s):
	d.setdefault(c, []).append(i + 1)
m = -1
n = len(s)
r = 0
for c in t:
	if not c in d:
		print(-1)
		exit()
	i = bisect(d[c], m)
	if len(d[c]) == i:
		r += 1
		m = d[c][0]
	else:
		m = d[c][i]
print(r * n + m)


def f():
	(s, t) = (input(), input())
	(n, m) = (len(s), len(t))
	table = [[] for i in range(26)]
	d = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
	for j in range(n):
		table[d[s[j]]].append(j)
	now = 0
	ind = 0
	if set(list(t)) - set(list(s)):
		print(-1)
		return
	from bisect import bisect_left as bl
	for p in t:
		if ind > table[d[p]][-1]:
			now += 1
			ind = 0
		ind = table[d[p]][bl(table[d[p]], ind)] + 1
	print(now * n + ind)

def __starting_point():
	f()
__starting_point()


def main():
	from functools import lru_cache
	import bisect
	S = input()
	T = input()
	if set(S) & set(T) != set(T):
		print(-1)
	else:
		lt = len(T)
		ls = len(S)
		st = set(T)
		D = dict()
		for t in st:
			D[t] = list()
			for (i, s) in enumerate(S):
				if t == s:
					D[t].append(i)
		pre = -1
		cnt = 0

		@lru_cache(maxsize=None)
		def judge(pre, t):
			X = D[t]
			index = bisect.bisect(X, pre)
			if len(X) == index:
				return (X[0], True)
			else:
				return (X[index], False)
		for t in T:
			J = judge(pre, t)
			pre = J[0]
			if J[1]:
				cnt += 1
		print(cnt * len(S) + pre + 1)

def __starting_point():
	main()
__starting_point()


import sys
s = str(input())
t = str(input())
for i in range(len(t)):
	if s.find(t[i]) == -1:
		print(-1)
		break
else:
	a = 0
	count = 0
	for k in t:
		if k in s[a:]:
			a = s.find(k, a) + 1
		else:
			count += 1
			a = s.find(k) + 1
	print(count * len(s) + a)


from collections import defaultdict
from bisect import bisect_left

def main():
	s = input()
	n = len(s)
	s = s + s
	t = input()
	if set(t) - set(s):
		print(-1)
		exit()
	d = defaultdict(list)
	for i in range(2 * n):
		d[s[i]] += [i]
	cur = ncnt = 0
	for c in t:
		x = d[c][bisect_left(d[c], cur)]
		if x < n:
			cur = x + 1
		else:
			cur = x - n + 1
			ncnt += 1
	print(ncnt * n + cur)
main()


from bisect import bisect_left
s = input()
t = input()
oa = ord('a')
l = [[] for _ in range(26)]
for (i, c) in enumerate(s):
	l[ord(c) - oa].append(i)
x = -1
p = 0
for c in t:
	ll = l[ord(c) - oa]
	if len(ll) == 0:
		print(-1)
		exit()
	y = bisect_left(ll, x)
	if y == len(ll):
		p += 1
		x = ll[0] + 1
	else:
		x = ll[y] + 1
print(p * len(s) + x)


from collections import *
from bisect import *
s = input()
t = input()
slen = len(s)
d = defaultdict(list)
for (i, c) in enumerate(s):
	d[c].append(i)
nowplace = -1
roop = 0
for c in t:
	if not d[c]:
		print(-1)
		exit()
	i = bisect(d[c], nowplace)
	if len(d[c]) == i:
		roop += 1
		nowplace = d[c][0]
	else:
		nowplace = d[c][i]
print(roop * slen + nowplace + 1)


S = input()
T = input()
p = 0
e = 1
for s in T:
	q = p % len(S)
	if not s in S:
		e = 0
		break
	if s in S[q:]:
		p += S[q:].index(s) + 1
	else:
		p += S.index(s) + len(S[q:]) + 1
print((p + 1) * e - 1)


from bisect import bisect_right
from collections import defaultdict
s = input()
t = input()
idx = defaultdict(list)
for i in range(len(s)):
	idx[s[i]].append(i)
num = 0
prev = -1
for c in t:
	l = len(idx[c])
	if l == 0:
		print(-1)
		exit()
	j = bisect_right(idx[c], prev)
	if j == l:
		prev = idx[c][0]
		num += 1
	else:
		prev = idx[c][j]
print(len(s) * num + prev + 1)


s = input()
t = input()
a = -1
c = 0
if not set(t) <= set(s):
	print(-1)
	exit()
for i in t:
	a = s.find(i, a + 1)
	if a == -1:
		c += 1
		a = s.find(i)
print(c * len(s) + a + 1)


import bisect
s = input()
t = input()
if set(list(s + t)) != set(list(s)):
	print(-1)
	exit()
check = set(list(t))
dic = {}
for Str in check:
	dic[Str] = []
for i in range(len(s)):
	if s[i] in check:
		dic[s[i]] += [i]
times = 0
now = -1
for p in t:
	if now >= dic[p][-1]:
		now = dic[p][0]
		times += 1
	else:
		now = dic[p][bisect.bisect(dic[p], now)]
print(len(s) * times + now + 1)


import bisect
S = list(input())
T = list(input())
sT = set(T)
N = len(S)
TI = {}
for i in range(N):
	s = S[i]
	if s in sT:
		if s in TI:
			TI[s].append(i)
		else:
			TI[s] = [i]
i = -1
for t in T:
	if t not in TI:
		print(-1)
		exit()
	else:
		ni = TI[t][bisect.bisect_right(TI[t], i % N) % len(TI[t])]
		if ni > i % N:
			i += ni - i % N
		else:
			i += N - i % N + ni
print(i + 1)


import bisect
s = input()
t = input()
s_set = set([i for i in s])
t_set = set([i for i in t])
if t_set - s_set & t_set:
	print(-1)
	exit()
a = {}
for i in t_set:
	a[i] = []
	for (k, j) in enumerate(s):
		if j == i:
			a[i].append(k)
	a[i].sort()
ans = 0
j = 0
for i in t:
	if a[i][-1] < j:
		ans += len(s)
		j = 0
	k = bisect.bisect_left(a[i], j)
	j = a[i][k] + 1
print(ans + j)


from bisect import bisect_left
s = input()
t = input()
dic = {}
for (ind, c) in enumerate(s):
	if c in dic:
		dic[c].append(ind)
	else:
		dic[c] = [ind]
loop = 0
ind = 0
try:
	for c in t:
		if ind > dic[c][-1]:
			ind = dic[c][0] + 1
			loop += 1
			continue
		tmp = bisect_left(dic[c], ind)
		ind = dic[c][tmp] + 1
	print(len(s) * loop + ind)
except:
	print(-1)


import bisect
s = input()
t = input()
d = dict()
for i in 'abcdefghijklmnopqrstuvwxyz':
	d[i] = []
for (i, c) in enumerate(s):
	d[c].append(i)
now = 0
ans = 0
for c in t:
	l = len(d[c])
	if l == 0:
		print(-1)
		break
	i = bisect.bisect_left(d[c], now)
	if i < l:
		now = d[c][i] + 1
	else:
		ans += len(s)
		i = bisect.bisect_left(d[c], 0)
		now = d[c][i] + 1
else:
	ans += now
	print(ans)


def main():
	s = input()
	t = input()
	n = len(s)
	now = -1
	ans = 1
	if not set(t) <= set(s):
		print(-1)
		exit()
	for x in t:
		now = s.find(x, now + 1)
		if now == -1:
			ans += n
			now = s.find(x)
	print(ans + now)
main()


import bisect
s = list(input())
t = list(input())
N = len(s)
sd = {x: [] for x in sorted(set(s))}
if not set(t) <= set(sd.keys()):
	print(-1)
	exit()
for (i, c) in enumerate(s):
	sd[c].append(i)
index = -1
ans = 0
for c in t:
	i = bisect.bisect_left(sd[c], index + 1)
	if i == len(sd[c]):
		ans += 1
		i = bisect.bisect_left(sd[c], 0)
	index = sd[c][i]
print(ans * N + index + 1)


import sys

def solve(s: str, t: str):
	from collections import defaultdict
	from bisect import bisect
	d = defaultdict(list)
	for (i, c) in enumerate(s):
		d[c].append(i)
	(ans, i) = (0, -1)
	for c in t:
		if not d[c]:
			return -1
		j = bisect(d[c], i)
		if len(d[c]) == j:
			ans += len(s) - i + d[c][0]
			i = d[c][0]
		else:
			ans += d[c][j] - i
			i = d[c][j]
	return ans

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	s = next(tokens)
	t = next(tokens)
	print(solve(s, t))

def test():
	import doctest
	doctest.testmod()

def __starting_point():
	test()
	main()
__starting_point()


import sys
s = input()
t = input()
s_ = s
num = 1
for i in range(len(t)):
	if s.find(t[i]) == -1:
		print(-1)
		sys.exit()
	elif s_.find(t[i]) == -1:
		num += 1
		s_ = s[s.find(t[i]) + 1:]
	else:
		s_ = s_[s_.find(t[i]) + 1:]
print(num * len(s) - len(s_))


from bisect import *
from collections import defaultdict
import sys
input = sys.stdin.readline
S = input().rstrip()
T = input().rstrip()
L = len(S)
dic = defaultdict(list)
for (i, s) in enumerate(S):
	dic[s].append(i)
s0 = -1
cnt = 0
for t in T:
	if not dic[t]:
		print(-1)
		exit()
	p = bisect_right(dic[t], s0)
	if p >= len(dic[t]):
		cnt += 1
		s0 = min(dic[t])
	else:
		s0 = dic[t][p]
print(L * cnt + s0 + 1)


import bisect
s = input()
t = input()
n = len(s)
s0 = {}
for i in range(n):
	s0.setdefault(s[i], [])
	s0[s[i]].append(i)
num1 = 0
num2 = -1
flag = True
for i in t:
	if i not in s:
		flag = False
		break
	num = bisect.bisect_right(s0[i], num2)
	if num == len(s0[i]):
		num1 += 1
		num2 = s0[i][0]
	else:
		num2 = s0[i][num]
if flag == True:
	print(num1 * n + num2 + 1)
else:
	print(-1)


def main():
	s = str(input())
	s_ = set(list(s))
	t = str(input())
	t_ = set(list(t))
	alphabet = 'abcdefghijklmnopqrstuvwxyz'
	if t_ <= s_:
		length_s = len(s)
		res = [[length_s] * 26 for _ in range(length_s + 1)]
		for i in range(length_s - 1, -1, -1):
			for j in range(26):
				if s[i] == alphabet[j]:
					res[i][j] = i + 1
				else:
					res[i][j] = res[i + 1][j]
		ans = 0
		now = 0
		for i in range(len(t)):
			for j in range(26):
				if t[i] == alphabet[j]:
					ans += res[now][j] - now
					now = res[now][j]
					if now == length_s:
						if t[i] == s[-1]:
							now = 0
						else:
							ans += res[0][j]
							now = res[0][j]
		print(ans)
	else:
		print(-1)
main()


from bisect import bisect_right
s = input()
t = input()
n = len(s)
if set(list(t)) != set(list(s)) & set(list(t)):
	ans = -1
else:
	dct = {}
	for (i, ss) in enumerate(s):
		if ss in dct:
			dct[ss].append(i)
		else:
			dct[ss] = [i]
	idx = -1
	ans = 0
	for tt in t:
		m = bisect_right(dct[tt], idx)
		if m == len(dct[tt]):
			idx = dct[tt][0]
			ans += n
		else:
			idx = dct[tt][m]
	ans += idx + 1
print(ans)


S = input()
T = input()
if not set(S) >= set(T):
	print(-1)
	exit()
str_repeat_num = 0
now_idx = 0
for t in T:
	if t in S[now_idx:]:
		now_idx += S[now_idx:].index(t) + 1
	else:
		str_repeat_num += 1
		now_idx = now_idx = S[:now_idx].index(t) + 1
print(len(S) * str_repeat_num + now_idx)


s = input()
t = input()
import bisect
dist = [[] for i in range(30)]
for j in range(len(s)):
	dist[ord(s[j]) - ord('a')].append(j + 1)
cnt = 0
nw = 1
for i in range(len(t)):
	chrno = ord(t[i]) - ord('a')
	if len(dist[chrno]) == 0:
		print(-1)
		exit()
	neIdx = bisect.bisect_left(dist[chrno], nw)
	if neIdx == len(dist[chrno]):
		cnt += 1
		nw = dist[chrno][0] + 1
	else:
		nw = dist[chrno][neIdx] + 1
print(len(s) * cnt + nw - 1)


import bisect
ind = {}
s = input()
t = input()
for (i, c) in enumerate(s):
	if c in ind:
		ind[c].append(i + 1)
	else:
		ind[c] = [i + 1]
k = len(s)
nk = 0
if not t[0] in ind:
	print(-1)
	return
nw = ind[t[0]][0]
pv = nw
for i in range(1, len(t)):
	c = t[i]
	if not c in ind:
		print(-1)
		return
	if ind[c][-1] > pv:
		pv = ind[c][bisect.bisect_left(ind[c], pv + 1)]
	else:
		nk += k
		pv = ind[c][0]
	nw = pv + nk
print(nw)


import bisect
(S, T) = (input(), input())
lenS = len(S)
alp = [[] for i in range(26)]
for i in range(len(S)):
	alp[ord(S[i]) - 97].append(i)
if alp[ord(T[0]) - 97] == []:
	print(-1)
	exit()
l = [alp[ord(T[0]) - 97][0]]
for c in T[1:]:
	A = alp[ord(c) - 97]
	if A == []:
		print(-1)
		exit()
	l.append(A[bisect.bisect_left(A, l[-1] + 1) % len(A)])
ans = 0
for i in range(len(l) - 1):
	if l[i] >= l[i + 1]:
		ans += 1
print(ans * lenS + l[-1] + 1)


s = input()
t = input()
for i in range(len(t)):
	if not t[i] in s:
		print(-1)
		break
else:
	j = -1
	counter = 0
	for i in range(len(t)):
		if t[i] in s[j + 1:]:
			j = s[j + 1:].find(t[i]) + j + 1
		else:
			counter += 1
			j = s.find(t[i])
	print(counter * len(s) + j + 1)


from bisect import bisect_right, bisect_left
s = [ord(i) - ord('a') for i in input()]
t = [ord(i) - ord('a') for i in input()]
index = [[] for _ in range(26)]
for (i, x) in enumerate(s):
	index[x].append(i)
l_s = len(s)
for x in index:
	if x:
		x.append(x[0] + l_s)
cnt = -1
for r in t:
	seq = index[r]
	if not seq:
		cnt = -2
		break
	i = cnt % l_s
	j = bisect_right(seq, i)
	cnt += seq[j] - i
print(cnt + 1)


s = input()
t = input()
idx = -1
count = 0
if set(t) <= set(s):
	for i in t:
		idx = s.find(i, idx + 1)
		if idx == -1:
			idx = s.find(i)
			count += 1
		rep = count * len(s)
	print(idx + 1 + rep)
else:
	print(-1)


from collections import defaultdict
import bisect
import sys
s = input()
t = input()
d = defaultdict(list)
ls = len(s)
lt = len(t)
for i in range(ls):
	d[s[i]].append(i)
buf = -1
l = 0
for i in range(lt):
	if not d[t[i]]:
		print(-1)
		sys.exit()
	idx = bisect.bisect_right(d[t[i]], buf)
	if idx >= len(d[t[i]]):
		buf = d[t[i]][0]
		l += 1
	else:
		buf = d[t[i]][idx]
print(l * ls + buf + 1)


from collections import defaultdict
from bisect import bisect_right
s = input()
t = input()
n = len(s)
m = len(t)
aa = set(s)
for i in t:
	if i not in aa:
		print(-1)
		exit()
d = defaultdict(list)
for i in range(n):
	d[s[i]].append(i)
pos = -1
cnt = 0
for c in t:
	j = bisect_right(d[c], pos)
	if j < len(d[c]):
		pos = d[c][j]
	else:
		cnt += 1
		pos = d[c][0]
print(cnt * n + pos + 1)


s = input()
t = input()
if len(set(t) - set(s)) > 0:
	print(-1)
else:
	s2 = s * 2
	s2pos = {}
	for i in range(len(s2)):
		if s2[i] in s2pos:
			s2pos[s2[i]].append(i)
		else:
			s2pos[s2[i]] = [i]
	cur = -1
	import bisect
	rev = 0
	for c in t:
		cur += 1
		pos = s2pos[c]
		p = bisect.bisect_left(pos, cur)
		cur = pos[p]
		if cur >= len(s):
			cur -= len(s)
			rev += 1
	else:
		print(rev * len(s) + cur + 1)


import bisect
w1 = input()
w2 = input()
d = {}
for i in list('qwertyuiopasdfghjklzxcvbnm'):
	d[i] = []
for i in range(len(w1)):
	d[w1[i]].append(i + 1)
num = len(w1)
for i in w2:
	if d[i] == []:
		print(-1)
		exit()
now = 0
count = 0
num2 = len(w2)
for i in range(num2):
	d1 = d[w2[i]]
	if d1[-1] < now:
		now = d1[0] + 1
		count += 1
	else:
		index = bisect.bisect_left(d1, now)
		now = d1[index] + 1
print(count * num + now - 1)


s = input()
t = input()
ans = 0
cur = 0
cnt = 0
for i in t:
	f = s.find(i, cur)
	if f == -1:
		cnt += 1
		f = s.find(i)
		if f == -1:
			print(-1)
			exit()
		else:
			cur = f + 1
	else:
		cur = f + 1
print(cur + len(s) * cnt)


s = input().strip()
t = input().strip()
if not set(t) <= set(s):
	print(-1)
	exit()
result = 0
now_i = 0
for c in t:
	if not c in s[now_i:]:
		result += len(s) - now_i
		now_i = 0
	di = s[now_i:].index(c)
	di += 1
	result += di
	now_i = (now_i + di) % len(s)
print(result)


S = input()
T = input()
from collections import defaultdict
nx = [defaultdict(lambda : -1) for i in range(len(S) + 1)]
lastpos = defaultdict(lambda : 0)
SS = S + S
for (i, c) in enumerate(SS[::-1]):
	i = len(SS) - 1 - i
	if i < len(S):
		for (k, v) in lastpos.items():
			nx[i + 1][k] = v - i
	lastpos[c] = i
for (k, v) in lastpos.items():
	nx[0][k] = v + 1
ans = 0
for c in T:
	if nx[ans % len(S)][c] == -1:
		print(-1)
		return
	ans += nx[ans % len(S)][c]
print(ans)


s = input()
t = input()
if len(set(t) - set(s)) > 0:
	print(-1)
	exit(0)
ss = s * 2
ind = -1
turn = 0
for c in t:
	nex = ss[ind + 1:].find(c)
	ind = ind + 1 + nex
	if ind >= len(s):
		ind -= len(s)
		turn += 1
print(turn * len(s) + ind + 1)


from bisect import bisect
S = input()
T = input()
c = [[] for _ in range(26)]
for (i, s) in enumerate(S):
	j = ord(s) - ord('a')
	c[j].append(i)
for t in set(T):
	if not c[ord(t) - ord('a')]:
		print(-1)
		exit()
rnd = 0
k = -1
for t in T:
	ti = ord(t) - ord('a')
	i = bisect(c[ti], k)
	if i == len(c[ti]):
		k = c[ti][0]
		rnd += 1
	else:
		k = c[ti][i]
print(rnd * len(S) + k + 1)


from collections import defaultdict
import bisect

def solve():
	s = input()
	t = input()
	d = defaultdict(list)
	st_s = set(s)
	for i in range(len(s)):
		d[s[i]].append(i)
	round = 0
	prev = -1
	for i in range(len(t)):
		if not t[i] in st_s:
			print(-1)
			return
		now = getNextIdx(prev, d[t[i]])
		if now <= prev:
			round += 1
		prev = now
	print(prev + round * len(s) + 1)

def getNextIdx(prev, idx_list):
	ret_idx = bisect.bisect_right(idx_list, prev)
	if ret_idx == len(idx_list):
		return idx_list[0]
	return idx_list[ret_idx]

def __starting_point():
	solve()
__starting_point()


import bisect
s = input()
t = input()
dic = {}
for i in range(len(t)):
	if t[i] in dic:
		continue
	if t[i] not in dic:
		dic[t[i]] = []
for i in range(len(s)):
	if s[i] in dic:
		dic[s[i]].append(i)
for (k, v) in dic.items():
	if v == []:
		print(-1)
		return
n = -1
k = 0
for i in range(len(t)):
	l = bisect.bisect_right(dic[t[i]], n)
	if l < len(dic[t[i]]):
		n = dic[t[i]][l]
	else:
		n = dic[t[i]][0]
		k += 1
print(len(s) * k + n + 1)


from bisect import bisect_right
s = input()
t = input()
p = {c: [] for c in s}
for (x, c) in enumerate(s):
	p[c].append(x)
z = 0
l = -1
for c in t:
	if c not in p:
		print(-1)
		break
	x = bisect_right(p[c], l)
	if x == len(p[c]):
		x = 0
		z += 1
	l = p[c][x]
else:
	print(z * len(s) + l + 1)


import bisect
from collections import defaultdict
s = input()
t = input()
if set(s) & set(t) != set(t):
	print(-1)
	exit()
cnt = defaultdict(list)
for (i, c) in enumerate(s, 1):
	cnt[c].append(i)
loop = 0
prev = 0
for w in t:
	l = cnt[w]
	idx = bisect.bisect_right(l, prev)
	if idx < len(l):
		prev = l[idx]
	else:
		prev = l[0]
		loop += 1
ans = loop * len(s) + prev
print(ans)


s = input()
t = input()
count = 0
ind = -1
if set(t) <= set(s):
	for i in range(len(t)):
		ind = s.find(t[i], ind + 1)
		if ind == -1:
			count += 1
			ind = s.find(t[i], ind + 1)
	print(count * len(s) + ind + 1)
else:
	print(-1)


sl = list(input())
tl = list(input())
n = len(sl)
from collections import defaultdict
dic = defaultdict(list)
for (idx, s) in enumerate(sl):
	dic[s].append(idx)
	dic[s].append(idx + n)
for s in dic:
	dic[s] = sorted(dic[s])
import bisect
ans = 0
now = -1
for t in tl:
	if dic[t] == []:
		print(-1)
		exit()
	idx = bisect.bisect_right(dic[t], now)
	ans += dic[t][idx] - now
	now = dic[t][idx]
	now %= n
print(ans)


from collections import defaultdict
from bisect import bisect_left
s = input()
t = input()
ids = defaultdict(list)
for (i, c) in enumerate(s):
	ids[c].append(i)
i = 0
ans = 0
for c in t:
	if c not in ids:
		print(-1)
		return
	ls = ids[c]
	j = bisect_left(ls, i)
	if j == len(ls):
		ans += len(s)
		i = ls[0] + 1
	else:
		i = ls[j] + 1
print(ans + i)


from collections import defaultdict
import bisect
S = input()
T = input()
d = defaultdict(list)
for (i, s) in enumerate(S):
	d[s].append(i)
x = -1
cnt = 0
for t in T:
	if len(d[t]) == 0:
		print(-1)
		exit()
	y = bisect.bisect_right(d[t], x)
	if y == len(d[t]):
		cnt += 1
		x = d[t][0]
	else:
		x = d[t][y]
ans = len(S) * cnt + x + 1
print(ans)


(s, t) = ([ord(c) - ord('a') for c in input()] for _ in 'xx')
if set(t) - set(s):
	print(-1)
else:
	u = s + s
	nxt = [None for _ in range(len(u) + 1)]
	nxt[len(u)] = [0 for _ in range(26)]
	for i in range(len(u) - 1, -1, -1):
		nxt[i] = [v + 1 for v in nxt[i + 1]]
		nxt[i][u[i]] = 0
	p = 0
	for c in t:
		p += nxt[p % len(s)][c] + 1
	print(p)


import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
sys.setrecursionlimit(10 ** 6)

def zz():
	return list(map(int, sys.stdin.readline().split()))

def z():
	return int(sys.stdin.readline())

def S():
	return sys.stdin.readline()[:-1]

def C(line):
	return [sys.stdin.readline() for _ in range(line)]
s = S()
t = S()
set_s = set(s)
set_t = set(t)
if (set_t <= set_s) is False:
	print(-1)
	return
alphabet2index = defaultdict(list)
for (i, alphabet) in enumerate(s):
	alphabet2index[alphabet].append(i + 1)
ans = 0
pos = 0
prev_pos = 0
base = 0
n = -1
c = 0
for _t in t:
	n = s.find(_t, n + 1)
	if n == -1:
		c += 1
		n = s.find(_t)
ans = n + c * len(s) + 1
print(ans)


import sys
from heapq import heappush, heappop, heappushpop
from collections import defaultdict
input = sys.stdin.readline
S = input().rstrip()
T = input().rstrip()
lenS = len(S)
lenT = len(T)
F1 = [False] * 26
F2 = [False] * 26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
	F1[alpha.index(S[i])] = True
for i in range(lenT):
	F2[alpha.index(T[i])] = True
for i in range(26):
	if not F1[i] and F2[i]:
		print(-1)
		return
ind = S.index(T[0])
ans = ind + 1
for i in range(1, lenT):
	S = S[ind + 1:] + S[:ind + 1]
	ind = S.index(T[i])
	ans += ind + 1
print(ans)


from collections import defaultdict
from bisect import *
S = input().rstrip()
T = input().rstrip()
LS = len(S)
index = defaultdict(list)
for (i, s) in enumerate(S):
	index[s].append(i)
x = -1
loop = 0
for t in T:
	arr = index[t]
	if len(arr) == 0:
		print(-1)
		exit()
	i = bisect(arr, x)
	if i == len(arr):
		loop += 1
		x = arr[0]
	else:
		x = arr[i]
print(loop * LS + x + 1)


import bisect
S = input()
T = input()
D = dict()
for i in range(len(T)):
	if T[i] in D:
		continue
	else:
		D[T[i]] = []
for i in range(len(S)):
	if S[i] in D:
		D[S[i]].append(i)
if len(set(T) - set(S)) > 0:
	print(-1)
else:
	r = -1
	cnt = 0
	for i in range(len(T)):
		m = bisect.bisect_right(D[T[i]], r)
		if m < len(D[T[i]]):
			r = D[T[i]][m]
		else:
			r = D[T[i]][0]
			cnt += 1
	print(len(S) * cnt + r + 1)


from collections import defaultdict
from bisect import bisect_left
S = input()
T = input()
lenS = len(S)
if set(S) >= set(T):
	SS = S + S
	pos = defaultdict(list)
	for (i, s) in enumerate(SS):
		pos[s].append(i)
	ans = -1
	for t in T:
		lb = (ans + 1) % lenS
		nxt = bisect_left(pos[t], lb)
		ans += pos[t][nxt] - lb + 1
	ans += 1
	print(ans)
else:
	print(-1)


s = input()
t = input()
x = len(s)
y = len(t)
a = 0
b = x
c = 0
a0 = 0
for i in range(0, y):
	while True:
		a = s.find(t[i:i + 1], a0) + 1
		if a <= 0:
			a = s.find(t[i:i + 1]) + 1
		if a <= 0:
			print('-1')
			exit()
		if a <= a0:
			c = c + a + b
		else:
			c = c + a - a0
		b = x - a
		a0 = a
		break
print(c)


import bisect
S = list(input())
T = list(input())
N = len(S)
p = [[] for _ in range(26)]
for i in range(N):
	p[ord(S[i]) - 97].append(i + 1)
res = 0
b = 0
for t in T:
	k = ord(t) - 97
	if len(p[k]) == 0:
		print(-1)
		exit()
	r = bisect.bisect_right(p[k], b)
	if len(p[k]) > r:
		res += p[k][r] - b
		b = p[k][r]
	else:
		res += N - b + p[k][0]
		b = p[k][0]
print(res)


import bisect
from collections import defaultdict
S = input()
T = input()
len_S = len(S)
s2i = defaultdict(list)
for (i, si) in enumerate(S):
	s2i[si].append(i)
ans = 1
pi = -1
for ti in T:
	si = s2i.get(ti)
	if not si:
		ans = -1
		break
	il = bisect.bisect_right(si, pi)
	if len(si) <= il:
		ans += len_S
		pi = si[0]
	else:
		pi = si[il]
if ans != -1:
	ans += pi
print(ans)


import bisect
import sys
s = list(input())
t = list(input())
n = len(s)
l = [chr(i) for i in range(97, 97 + 26)]
ct = [[] for i in range(26)]
for i in range(len(s)):
	ct[l.index(s[i])].append(i)
ans = 0
y = -1
for i in range(len(t)):
	x = l.index(t[i])
	if len(ct[x]) == 0:
		print(-1)
		sys.exit()
	z = bisect.bisect(ct[x], y)
	if z == len(ct[x]):
		ans = ans // n * n + n + ct[x][0]
		y = ct[x][0]
	else:
		ans = ans // n * n + ct[x][z]
		y = ct[x][z]
print(ans + 1)


import copy
s = input()
t = input()
sn = len(s)
tn = len(t)
a = [[] for i in range(26)]
for i in range(sn):
	num = ord(s[i]) - 97
	a[num].append(i)
table = [[0] * sn for i in range(26)]
for i in range(26):
	if a[i]:
		table[i][:] = [copy.deepcopy(a[i][0])] * sn
	f = 0
	for j in a[i]:
		table[i][f:j] = [copy.deepcopy(j)] * (j - f)
		f = copy.deepcopy(j)
flg = True
cnt = 0
x = -1
for i in range(tn):
	num = ord(t[i]) - 97
	if not a[num]:
		flg = False
		break
	xx = copy.deepcopy(x)
	x = table[num][x]
	if xx >= x:
		cnt += 1
print(cnt * sn + x + 1) if flg else print(-1)


s = input()
t = input()
ans1 = 0
ans2 = 0
ss = s * 2
for i in range(len(t)):
	a = ss.find(t[i], ans2)
	if a == -1:
		ans = -1
		break
	if a >= len(s):
		ans1 += 1
	ans2 = a % len(s) + 1
	ans = ans1 * len(s) + ans2
print(ans)


import bisect
s = input()
t = input()
d = {}
for i in range(len(s)):
	if s[i] in d:
		d[s[i]] += [i]
	else:
		d[s[i]] = [i]
c = 0
cur = -1
for i in t:
	if i not in d:
		print(-1)
		exit()
	if cur < d[i][-1]:
		cur = d[i][bisect.bisect_right(d[i], cur)]
		continue
	else:
		cur = d[i][0]
		c += 1
print(c * len(s) + cur + 1)


S = input()
T = input()
if all([c in S for c in T]) != True:
	print(-1)
	exit()
str_repeat_num = 0
now_idx = 0
for t in T:
	if t in S[now_idx:]:
		now_idx += S[now_idx:].index(t) + 1
	else:
		str_repeat_num += 1
		now_idx = S[:now_idx].index(t) + 1
print(len(S) * str_repeat_num + now_idx)


S = list(str(input()))
T = list(str(input()))
S_ = set(list(S))
T_ = set(list(T))
for c in T:
	if c not in S_:
		print(-1)
		return
d = [[] for _ in range(26)]
for (i, s) in enumerate(S):
	d[ord(s) - ord('a')].append(i)
q = 0
pre = -1
import bisect
for t in T:
	j = ord(t) - ord('a')
	idx = bisect.bisect_right(d[j], pre)
	if idx == len(d[j]):
		q += 1
		pre = d[j][0]
	else:
		pre = d[j][idx]
print(q * len(S) + pre + 1)


from collections import defaultdict
import bisect
s = list(input())
t = list(input())
dic = defaultdict(list)
for i in range(len(s)):
	dic[s[i]].append(i)
loop = 0
mark = -1
for i in t:
	if len(dic[i]) == 0:
		loop = -1
		break
	ind = bisect.bisect_right(dic[i], mark)
	if ind == len(dic[i]):
		ind = 0
		loop += 1
	mark = dic[i][ind]
if loop == -1:
	print(-1)
else:
	print(loop * len(s) + mark + 1)


import bisect
s = input()
t = input()
ss = s + s
char = [[] for _ in range(26)]
for i in range(len(ss)):
	char[ord(ss[i]) - 97].append(i + 1)
ans = 0
tmp = 0
for i in range(len(t)):
	if not char[ord(t[i]) - 97]:
		print(-1)
		break
	k = bisect.bisect_right(char[ord(t[i]) - 97], tmp)
	ans += char[ord(t[i]) - 97][k] - tmp
	tmp = char[ord(t[i]) - 97][k]
	if tmp - len(s) > 0:
		tmp -= len(s)
else:
	print(ans)


import sys
import re
import math
import collections
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics
import queue
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 16
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline())
ns = lambda : list(map(int, sys.stdin.readline().split()))
na = lambda : list(map(int, sys.stdin.readline().split()))
nb = lambda : list([int(x) - 1 for x in sys.stdin.readline().split()])

def main():
	s = list(input())
	t = collections.deque(list(input()))
	jump = [[0] * 26 for _ in range(len(s) + 1)]
	for i in range(len(s) - 1, -1, -1):
		jump[i] = jump[i + 1].copy()
		jump[i][ord(s[i]) - 97] = i + 1
	ans = 0
	point = 0
	while len(t) > 0:
		tmp = t.popleft()
		character = ord(tmp) - 97
		if jump[point][character] == 0 and point == 0:
			print(-1)
			return
		point = jump[point][character]
		if point == 0:
			t.appendleft(tmp)
			ans += 1
	print(ans * len(s) + point)

def __starting_point():
	main()
__starting_point()


from bisect import *
s = input()
t = input()
a = [[] for i in range(26)]
for i in range(len(s)):
	a[ord(s[i]) - 97].append(i + 1)
p = -1
b = [len(i) for i in a]
r = 0
for i in t:
	if b[ord(i) - 97] == 0:
		print(-1)
		break
	c = bisect_right(a[ord(i) - 97], p)
	if c == b[ord(i) - 97]:
		r += 1
		p = a[ord(i) - 97][0]
	else:
		p = a[ord(i) - 97][c]
else:
	print(r * len(s) + p)


from bisect import bisect_right
s = input()
n = len(s)
t = input()
m = len(t)
tr = set(s)
for c in t:
	if c not in s:
		print(-1)
		exit(0)
d = {c: [] for c in 'abcdefghijklmnopqrstuvwxyz'}
for (i, c) in enumerate(s):
	d[c].append(i)
idx = -1
ans = 0
for c in t:
	tmp = bisect_right(d[c], idx)
	if tmp == len(d[c]):
		idx = d[c][0]
		ans += 1
	else:
		idx = d[c][tmp]
print(ans * n + idx + 1)


s = input()
t = input()
index = -1
Wrap = 0
for a in t:
	index = s.find(a, index + 1)
	if index != -1:
		continue
	Wrap += 1
	index = s.find(a)
	if index == -1:
		print(-1)
		exit()
print(len(s) * Wrap + index + 1)


s = input()
t = input()
from bisect import bisect
points = [[] for i in range(26)]
a = ord('a')
for (i, c) in enumerate(s):
	points[ord(c) - a].append(i)
for p in points:
	p.append(10 ** 9)
n = len(s)
idx = -1
stage = 0
for c in t:
	c = ord(c) - a
	p = points[c]
	if len(p) == 1:
		stage = 0
		idx = -2
		break
	idx = p[bisect(p, idx)]
	if idx > 10 ** 6:
		idx = p[0]
		stage += 1
print(stage * n + idx + 1)


S = input()
T = input()
length = len(S)
ans = 0
pre = -1
for t in T:
	x = S.find(t, pre + 1, length)
	if x != -1:
		ans += x - pre
		pre = x
	else:
		x = S.find(t, 0, pre + 1)
		if x != -1:
			ans += length + x - pre
			pre = x
		else:
			ans = -1
			break
print(ans)


from collections import defaultdict
import bisect
(s, t) = [x.strip() for x in open(0).readlines()]
if set(t) - set(s) != set():
	print(-1)
else:
	d = defaultdict(list)
	n = len(s)
	for (i, c) in enumerate(s, 1):
		d[c].append(i)
	for k in d:
		d[k].append(d[k][0] + n)
	ans = 0
	for c in t:
		(q, r) = divmod(ans, n)
		x = d[c][bisect.bisect(d[c], r)]
		ans = n * q + x
	print(ans)


s = input()
t = input()
l_s = len(s)
l_t = len(t)
s2 = s + s
l_s2 = len(s2)
next_idx = [[l_s2] * 26 for i in range(l_s2)]
for i in reversed(range(1, l_s2)):
	for j in range(26):
		next_idx[i - 1][j] = next_idx[i][j]
	next_idx[i - 1][ord(s2[i]) - ord('a')] = i
cur_idx = 0
for i in range(l_t):
	if i == 0:
		flg = False
		for j in range(l_s):
			if t[i] == s[j]:
				cur_idx += j
				flg = True
				break
		if flg:
			continue
		else:
			print('-1')
			return
	else:
		idx = next_idx[cur_idx % l_s][ord(t[i]) - ord('a')]
	if idx == l_s2:
		print('-1')
		return
	cur_idx += idx - cur_idx % l_s
print(cur_idx + 1)


from bisect import bisect
s = input()
t = input()
if len(set(t) & set(s)) != len(set(t)):
	print(-1)
	exit()
d = dict()
for (i, si) in enumerate(s):
	if si in d:
		d[si].append(i)
	else:
		d[si] = [i]
ans = 0
res = d[t[0]][0]
for i in range(1, len(t)):
	index = bisect(d[t[i]], res)
	if index == len(d[t[i]]):
		ans += len(s)
		res = d[t[i]][0]
	else:
		res = d[t[i]][index]
ans += res + 1
print(ans)


import bisect
s = input()
t = input()
l = len(s)
d = dict()
for i in range(l):
	if s[i] in d.keys():
		d[s[i]].append(i + 1)
	else:
		d[s[i]] = [i + 1]
ans = 0
now = 0
for i in t:
	if i not in d.keys():
		print(-1)
		exit()
	a = bisect.bisect_right(d[i], now)
	if a == len(d[i]):
		now = d[i][0]
		ans += l
	else:
		now = d[i][a]
print(ans + now)


from bisect import bisect as bi
s = input()
t = input()
(x, c, ans, num) = ([[] for i in range(26)], [0] * 26, 0, -1)
for i in range(len(s)):
	x[ord(s[i]) - 97].append(i)
	c[ord(s[i]) - 97] += 1
for i in t:
	if x[ord(i) - 97]:
		b = bi(x[ord(i) - 97], num)
		if b == c[ord(i) - 97]:
			ans += len(s)
			num = x[ord(i) - 97][0]
		else:
			num = x[ord(i) - 97][b]
	else:
		(ans, num) = (-1, -1)
		break
print(ans + num + 1)


import bisect
s = input()
L = len(s)
s *= 2
t = input()
pl = {chr(i + 97): [] for i in range(26)}
for i in range(len(s)):
	pl[s[i]] += [i]
cnt = 0
rem = 0
for i in t:
	if pl[i] == []:
		print(-1)
		exit()
	cnt += pl[i][bisect.bisect_right(pl[i], rem - 1)] - (rem - 1)
	rem = cnt % L
print(cnt)


from bisect import bisect_left
S = input()
T = input()
Ns = len(S)
S = S + S
ABC = [[] for _ in range(26)]
for (i, s) in enumerate(S):
	s = ord(s) - 97
	ABC[s].append(i)
ans = 0
l_s = 0
for t in T:
	t = ord(t) - 97
	abc = ABC[t]
	l = bisect_left(abc, l_s)
	if l == len(abc):
		print(-1)
		exit()
	l_s = abc[l]
	l_s += 1
	if l_s >= Ns:
		ans += Ns
		l_s -= Ns
print(ans + l_s)


s = input()
t = input()
counter = 0
idx = -1
if set(t) <= set(s):
	for i in range(len(t)):
		idx = s.find(t[i], idx + 1)
		if idx == -1:
			counter += 1
			idx = s.find(t[i], idx + 1)
	print(counter * len(s) + idx + 1)
else:
	print(-1)


import collections
s = input()
t = input()
s_len = len(s)
s_idx = dict([])
for (i, key) in enumerate(s):
	try:
		s_idx[key].append(i + 1)
	except KeyError:
		s_idx[key] = [i + 1]

def binary_search(li, tmp, ng, ok):
	mid = (ng + ok) // 2
	if ok - ng < 2:
		if ok == len(li):
			return s_len + li[0]
		else:
			return li[ok]
	if li[mid] <= tmp:
		return binary_search(li, tmp, mid, ok)
	else:
		return binary_search(li, tmp, ng, mid)
ans = 0
ex_i = 0
for key in t:
	try:
		i = binary_search(s_idx[key], ex_i, -1, len(s_idx[key]))
		ans += i - ex_i
		ex_i = i % s_len
	except KeyError:
		ans = -1
		break
print(ans)


import bisect
S = list(input())
T = list(input())
A = [[] for i in range(26)]
for i in range(len(S)):
	A[ord(S[i]) - 97].append(i + 1)
a = 0
b = 0
for i in range(len(T)):
	k = ord(T[i]) - 97
	if len(A[k]) == 0:
		print(-1)
		exit()
	if b >= A[k][-1]:
		a = a + 1
		b = A[k][0]
		continue
	else:
		b = A[k][bisect.bisect_right(A[k], b, lo=0, hi=len(A[k]))]
print(a * len(S) + b)


s = input()
t = input()
t_length = len(t)
temp = s[:]
count = 0
ans = 0
for i in range(len(t)):
	a = s.find(t[i])
	if a == -1:
		print(-1)
		exit()
while count < t_length:
	value = temp.find(t[count])
	if value == -1:
		ans += len(temp)
		temp = s[:]
	else:
		ans += value + 1
		count += 1
		temp = temp[value + 1:]
print(ans)


import bisect
s = list(input())
t = input()
size = len(s)
dic = {}
for c in t:
	if c in dic:
		continue
	dic[c] = [i for (i, val) in enumerate(s) if val == c]
	if len(dic[c]) == 0:
		print(-1)
		exit()
pos = -1
loop = 0
for c in t:
	l = dic[c]
	if pos < l[-1]:
		index = bisect.bisect_right(l, pos)
		pos = l[index]
	else:
		pos = l[0]
		loop += 1
print(loop * size + pos + 1)


s = input()
t = input()
pos = 0
itr = 0
S = set(s)
T = set(t)
if S >= T:
	for i in t:
		pos = s.find(i, pos)
		if pos < 0:
			itr += 1
			pos = s.find(i) + 1
		else:
			pos += 1
	print(itr * len(s) + pos)
else:
	print(-1)


from bisect import bisect
s = input()
t = input()

def ctoi(c):
	return ord(c) - ord('a')
if not set(s) >= set(t):
	print(-1)
else:
	alph = [[] for _ in range(26)]
	for i in range(len(s)):
		alph[ctoi(s[i])].append(i)
	for a in alph:
		if a:
			a.append(a[0] + len(s))
	cur = len(s) - 1
	ans = 0
	for i in range(len(t)):
		ind = bisect(alph[ctoi(t[i])], cur)
		ans += alph[ctoi(t[i])][ind] - cur
		cur = alph[ctoi(t[i])][ind] % len(s)
	print(ans)


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(repeat * len(s) + last + 1)


s = input()
t = input()
temp = s[:]
cnt = 0
strmax = len(t)
ans = 0
for i in t:
	if s.find(i) == -1:
		print('-1')
		exit(0)
while cnt < strmax:
	num = temp.find(t[cnt])
	if num != -1:
		ans += num + 1
		temp = temp[num + 1:]
		cnt += 1
	else:
		ans += len(temp)
		temp = s[:]
print(ans)


import bisect
s = input()
t = input()
p = {}
for i in range(len(s)):
	c = s[i]
	if c not in p:
		p[c] = []
	p[c].append(i)
over = 0
ret = 0
for c in t:
	if c not in p:
		print(-1)
		exit(0)
	ind = bisect.bisect_left(p[c], ret)
	if ind < len(p[c]):
		ret = p[c][ind] + 1
	else:
		ret = p[c][0] + 1
		over += 1
print(over * len(s) + ret)


from collections import defaultdict
from bisect import bisect
s = input()
t = input()
S = defaultdict(list)
for (i, c) in enumerate(list(s)):
	S[c].append(i + 1)
now = 0
num = 0
for c in t:
	if c not in S:
		print(-1)
		exit()
	P = S[c]
	x = bisect(P, now)
	if x < len(P):
		num += P[x] - now
		now = P[x]
	else:
		num += len(s) - now
		num += P[0]
		now = P[0]
print(num)


import bisect
s = input()
t = input()
n = len(s) * 2
dic = {}
for (idx, i) in enumerate(s * 2):
	if i in dic.keys():
		dic[i].append(idx)
	else:
		dic[i] = [idx]
ans = k = 0
before = '-1'
for i in t:
	if i not in dic.keys():
		print(-1)
		exit()
	t = bisect.bisect_left(dic[i], ans % n)
	if before == i:
		t += 1
	if len(dic[i]) == t:
		t = 0
		k += n
	ans = dic[i][t] + k
	before = i
print(ans + 1)


from bisect import bisect_right
s = input()
t = input()
dic = {}
n = len(s)
for i in range(n):
	if s[i] in dic:
		dic[s[i]].append(i)
	else:
		dic[s[i]] = [i]
k = -1
r = 0
for i in t:
	if i in dic:
		a = bisect_right(dic[i], k)
		if a == len(dic[i]):
			r += 1
			k = dic[i][0]
		else:
			k = dic[i][a]
	else:
		print(-1)
		exit()
print(r * n + k + 1)


import bisect
s = input()
t = input()
d = {c: [] for c in t}
for (i, c) in enumerate(s):
	if c in d:
		d[c].append(i)
n = -1
ans = 0
for (j, c) in enumerate(t):
	l = d[c]
	if l:
		i = bisect.bisect_right(l, n)
		if i == len(l):
			ans += len(s)
			n = l[0]
		else:
			n = l[i]
		if j == len(t) - 1:
			if i == len(l):
				ans += l[0] + 1
			else:
				ans += l[i] + 1
	else:
		ans = -1
		break
print(ans)


import heapq
S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False] * 26
F2 = [False] * 26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
	F1[alpha.index(S[i])] = True
for i in range(lenT):
	F2[alpha.index(T[i])] = True
for i in range(26):
	if not F1[i] and F2[i]:
		print(-1)
		return
ind = S.index(T[0])
ans = ind + 1
for i in range(1, lenT):
	S = S[ind + 1:] + S[:ind + 1]
	ind = S.index(T[i])
	ans += ind + 1
print(ans)


import bisect
(s, t) = (input(), input())
(n, nx, r) = (len(s), {}, 0)
for (i, c) in enumerate(s + s):
	if nx.get(c) is None:
		nx[c] = []
	nx[c].append(i + 1)
for c in t:
	l = nx.get(c)
	if l is None:
		print(-1)
		exit()
	r += l[bisect.bisect(l, r % n)] - r % n
print(r)


import sys
import bisect
ss = input()
t = input()
n = len(ss)
cnt = {}
s = ss * 2
for (i, x) in enumerate(s):
	if x not in cnt:
		cnt[x] = []
	cnt[x].append(i)
idx = 0
ten = 0
for z in t:
	if z not in ss:
		print(-1)
		sys.exit()
	idx = cnt[z][bisect.bisect_left(cnt[z], idx)] + 1
	if idx >= n:
		idx -= n
		ten += 1
suffix = idx + ten * n
print(suffix)


s = input()
t = input()
T = len(t)
num = 0
nowi = -1
nexti = -1
for (i, ti) in enumerate(t):
	nexti = s[nowi + 1:].find(ti)
	if nexti == -1:
		num += 1
		nexti = s.find(ti)
		nowi = nexti
		if nexti == -1:
			print(-1)
			exit()
	else:
		nowi = nexti + nowi + 1
if num <= 10 ** 100 - 1:
	print(len(s) * num + nowi + 1)


s = input()
t = input()
pre_index = 0
ans = 0
for i in t:
	if i not in s:
		ans = -1
		break
	else:
		temp = s[pre_index:]
		if i in temp:
			ans += temp.index(i) + 1
			pre_index += temp.index(i) + 1
		else:
			ans += len(temp)
			pre_index = s.index(i) + 1
			ans += pre_index
print(ans)


s = input()
t = input()
n = len(s)
s += s
pos = [[-1] * 26 for _ in range(n * 2 + 1)]
for i in range(n * 2 - 1, -1, -1):
	k = ord(s[i]) - ord('a')
	for j in range(26):
		if j == k:
			pos[i][j] = i
		else:
			pos[i][j] = pos[i + 1][j]
ans = 0
now = 0
for c in t:
	x = ord(c) - ord('a')
	if pos[now][x] == -1:
		print(-1)
		return
	ans += -~pos[now][x] - now
	now = -~pos[now][x] % n
print(ans)


from bisect import bisect_left
S = input()
T = input()
if not set(list(T)) <= set(list(S)):
	print(-1)
	exit()
sToI = {s: [] for s in set(list(S))}
for (i, s) in enumerate(S):
	sToI[s].append(i)
now = 0
R = 0
for t in T:
	I = sToI[t]
	nx = bisect_left(I, now)
	if nx == len(I):
		nx = 0
		R += 1
	now = I[nx] + 1
print(now + R * len(S))


s = input()
t = input()
ans = 0
s1 = s
ans = 0
flag = 1
for i in t:
	if i not in s1:
		flag = 0
if flag == 0:
	print(-1)
else:
	for i in t:
		if i in s1:
			ans += s1.index(i) + 1
			s1 = s1[s1.index(i) + 1:]
		else:
			ans = (ans + len(s) - 1) // len(s) * len(s)
			ans += s.index(i) + 1
			s1 = s[s.index(i) + 1:]
	print(ans)


(s, t) = (input(), input())
if not set(list(t)) <= set(list(s)):
	print(-1)
	return
ss = [[] for i in range(26)]
for (i, v) in enumerate(s):
	ss[ord(v) - 97].append(i)
(nowind, loop) = (-1, 0)
from bisect import bisect_left as bl, bisect_right as br
for i in t:
	ind = ord(i) - 97
	if ss[ind][-1] < nowind:
		nowind = -1
		loop += 1
	nowind = ss[ind][bl(ss[ind], nowind)] + 1
print(loop * len(s) + nowind)


from bisect import bisect_left as bisect
from collections import defaultdict
(s, t) = (input(), input())
(lens, sets) = (len(s), set(s))
if not all((i in sets for i in t)):
	print(-1)
	return
alp = defaultdict(list)
for (i, si) in enumerate(s):
	alp[si].append(i)
(ans, cnt) = (1 - lens, 0)
for i in t:
	ans += lens * (cnt == 0)
	num = bisect(alp[i], cnt)
	ans += lens * (num == len(alp[i]))
	cnt = (alp[i][num % len(alp[i])] + 1) % lens
print(ans + (cnt - 1) % lens)


from bisect import bisect_left
s = input()
t = input()
pos = {}
for (i, c) in enumerate(s):
	if c not in pos:
		pos[c] = [i]
	else:
		pos[c].append(i)
ans = 0
pre = -1
for c in t:
	if c not in pos:
		print(-1)
		exit()
	l = bisect_left(pos[c], pre + 1)
	if l >= len(pos[c]):
		p = pos[c][0]
		ans += len(s)
	else:
		p = pos[c][l]
	ans += p - pre
	pre = p
print(ans)


3
from collections import defaultdict
import bisect

def main():
	s = input().strip()
	t = input().strip()
	ns = len(s)
	if len(set(list(t)) - set(list(s))):
		print(-1)
		return
	ichs = defaultdict(list)
	for (i, c) in enumerate(s):
		ichs[c].append(i)
	ic = -1
	cnt = 0
	for c in t:
		ix = bisect.bisect_right(ichs[c], ic)
		if ix >= len(ichs[c]):
			cnt += 1
			ix = 0
		elif ic == ichs[c][ix]:
			if ix + 1 >= len(ichs[c]):
				cnt += 1
				ix = 0
			else:
				ix += 1
		ic = ichs[c][ix]
	print(cnt * ns + ic + 1)

def __starting_point():
	main()
__starting_point()


from math import *
from bisect import *
s = input()
t = input()
n = len(s)
nn = len(t)
ss = [[] for i in range(26)]
tt = [[] for i in range(26)]
ans = 0
k = 0
for i in range(n):
	ss[ord(s[i]) - 97].append(i + 1)
for i in t:
	j = ss[ord(i) - 97]
	if len(j) == 0:
		print(-1)
		exit()
	elif j[-1] <= ans:
		ans = j[0]
		k += 1
	else:
		ans = j[bisect(j, ans)]
print(k * n + ans)


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)


import math
import queue
import bisect
from collections import deque, defaultdict
import heapq as hpq
from sys import stdin, setrecursionlimit
ipt = stdin.readline
setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7

def main():
	s = input()
	ls = len(s)
	t = input()
	if t[0] in s:
		idx = s.find(t[0])
	else:
		print(-1)
		return
	for i in t[1:]:
		nt = s.find(i, idx % ls + 1)
		ntl = s.find(i)
		if ntl == -1:
			print(-1)
			return
		elif nt == -1:
			idx = idx // ls * ls + ls + ntl
		else:
			idx = idx // ls * ls + nt
	print(idx + 1)
	return None

def __starting_point():
	main()
__starting_point()


def abc138_e():
	s = str(input())
	t = str(input())
	sd = dict()
	for (i, c) in enumerate(s):
		if sd.get(c) == None:
			sd[c] = []
		sd[c].append(i)
	from bisect import bisect_left
	k = 0
	p = 0
	for c in t:
		if sd.get(c) == None:
			return print(-1)
		idx = bisect_left(sd[c], p)
		if idx == len(sd[c]):
			k += 1
			p = 0
			idx = bisect_left(sd[c], p)
		p = sd[c][idx] + 1
	ans = k * len(s) + p
	return print(ans)

def __starting_point():
	abc138_e()
__starting_point()


from bisect import bisect_left
a = input()
b = input()
c = [[] for i in range(26)]
for i in range(len(a)):
	c[ord(a[i]) - 97].append(i)
m = -1
ans = 0
roop = 0
for i in range(len(b)):
	s = ord(b[i]) - 97
	if c[s] == []:
		print(-1)
		exit()
	else:
		p = bisect_left(c[s], m + 1)
		if p < len(c[s]):
			m = c[s][p]
		else:
			roop += 1
			m = c[s][0]
print(roop * len(a) + m + 1)


s = input()
t = input()
if set(t) & set(s) == set(t):
	n = -1
	c = 0
	for i in t:
		n = s.find(i, n + 1)
		if n == -1:
			c += 1
			n = s.find(i)
	print(n + len(s) * c + 1)
else:
	print(-1)


import bisect
s = input()
t = input()
s_dict = {c: [] for c in s}
for (i, c) in enumerate(s):
	s_dict[c].append(i)
len_s = len(s)
ans = 0
prev_i = -1
for c in t:
	if c not in s_dict:
		ans = -1
		prev_i = -1
		break
	elif s_dict[c][-1] <= prev_i:
		ans += len_s
		prev_i = s_dict[c][0]
	else:
		prev_i = s_dict[c][bisect.bisect_right(s_dict[c], prev_i, lo=0, hi=len(s_dict[c]))]
print(ans + prev_i + 1)


from collections import defaultdict
from bisect import *
(s, t) = (input(), input())
(before, count) = (len(s), 0)
idx = defaultdict(list)
if not set(t) <= set(s):
	print('-1')
	exit()
for (i, l) in enumerate(s):
	idx[l] += [i]
for c in t:
	N = idx[c]
	index = bisect_left(N, before)
	if index > len(N) - 1:
		before = N[0] + 1
		count += 1
	else:
		before = N[index] + 1
print((count - 1) * len(s) + before)


import sys
s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0] * t_len
rt1 = 0
rt2 = 0
s = s + s
for m in range(t_len):
	rt1 = s.find(t[m], rt1)
	if rt1 == -1:
		print(rt1)
		sys.exit()
	elif rt1 < s_len:
		pass
	else:
		rt1 = rt1 % s_len
		rt2 += 1
	rt1 += 1
rt = rt1 + rt2 * s_len
print(rt)


s = list(str(input()))
t = list(str(input()))
X = [[] for _ in range(26)]
for (i, c) in enumerate(s):
	j = ord(c) - ord('a')
	X[j].append(i)
ans = 0
import bisect
cur = -1
for c in t:
	j = ord(c) - ord('a')
	if len(X[j]) == 0:
		print(-1)
		exit()
	k = bisect.bisect_right(X[j], cur)
	if k != len(X[j]):
		cur = X[j][k]
	else:
		cur = X[j][0]
		ans += len(s)
ans += cur + 1
print(ans)


import bisect
from collections import defaultdict
s = input()
t = input()
D = defaultdict(list)
for (i, c) in enumerate(s, start=1):
	D[c].append(i)
times = 0
cs = 0
for c in t:
	if c not in D.keys():
		print(-1)
		exit()
	else:
		i = bisect.bisect_left(D[c], cs + 1)
		if i == len(D[c]):
			times += 1
			cs = D[c][0]
		else:
			cs = D[c][i]
print(times * len(s) + cs)


import sys
from collections import defaultdict
import bisect

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

def main():
	s = list(input())
	t = list(input())
	dic = defaultdict(list)
	for (index, string) in enumerate(s):
		dic[string].append(index)
	cycle = 0
	ind = -1
	for string in t:
		if dic[string] == []:
			print(-1)
			return
		elif dic[string][-1] <= ind:
			cycle += 1
			ind = dic[string][0]
		else:
			ind = dic[string][bisect.bisect_right(dic[string], ind)]
	print(cycle * len(s) + ind + 1)

def __starting_point():
	main()
__starting_point()


def main():
	S = input()
	N = len(S)
	INF = N + 1
	T = input()
	next_loc = [[INF for _ in range(N)] for _ in range(26)]
	for (i, s) in enumerate(S):
		c = ord(s) - ord('a')
		next_loc[c][i - 1] = 1
	for c in range(26):
		n = INF
		for i in range(N - 1, -(N + 1), -1):
			n = min(next_loc[c][i], n + 1)
			next_loc[c][i] = n
	ans = 0
	index = -1
	for t in T:
		c = ord(t) - ord('a')
		i = next_loc[c][index]
		if i == INF:
			print(-1)
			return
		ans += i
		index = (index + i) % N
	print(ans)

def __starting_point():
	main()
__starting_point()


s = input()
t = input()
if not set(t) <= set(s):
	print(-1)
else:
	a = 0
	seq = s
	for k in t:
		p = seq.find(k) + 1
		if p == 0:
			p = s.find(k) + 1
			a += len(seq)
			seq = s
		a += p
		seq = seq[p:]
	print(a)


s = input()
t = input()
n = 0
c = 0
for e in t:
	try:
		i = s[c:].index(e)
		n += i + 1
		c = (c + i + 1) % len(s)
	except ValueError:
		try:
			i = s.index(e)
			n += len(s) - c + i + 1
			c = i + 1
		except ValueError:
			n = -1
			break
print(n)


def solve():
	s = input()
	t = input()
	ans = 0
	alp = [[] for _ in range(27)]
	length_s = len(s)
	for i in range(length_s):
		char = ord(s[i]) - ord('a')
		alp[char].append(i + 1)
	length_t = len(t)
	import bisect
	for i in range(length_t):
		char = ord(t[i]) - ord('a')
		if alp[char]:
			if ans % length_s >= alp[char][-1]:
				ans = (ans // length_s + 1) * length_s
			index = bisect.bisect_right(alp[char], ans % length_s)
			ans += alp[char][index] - ans % length_s
		else:
			return -1
	return ans
print(solve())


s = input()
t = input()
ans1 = 0
ans2 = 0
m = len(s)
n = len(t)
s = s + s
for i in range(n):
	a = s.find(t[i], ans2)
	if a == -1:
		print(-1)
		exit()
	if a >= m:
		ans1 += 1
	ans2 = a % m + 1
print(ans1 * m + ans2)


import copy
s = input()
t = input()
if set(t) - set(s):
	print(-1)
	exit()
t_index = 0
s_index = 0
count = 0
copy_s = copy.deepcopy(s)
while len(t) > t_index:
	index = copy_s.find(t[t_index])
	if index >= 0:
		t_index += 1
		copy_s = copy_s[index + 1:]
	else:
		copy_s = copy.deepcopy(s)
		count += 1
print(count * len(s) + len(s) - len(copy_s))


s = input()
t = input()
if set(t).issubset(set(s)):
	i = 0
	x = s
	for k in t:
		p = x.find(k) + 1
		if p == 0:
			i += len(x)
			p = s.find(k) + 1
			x = s
		i += p
		x = x[p:]
	print(i)
else:
	print(-1)


def solve():
	import bisect
	S = input()
	T = input()
	dic = {}
	length = {}
	for (i, s) in enumerate(S, 1):
		if s not in dic:
			dic[s] = []
		dic[s].append(i)
	for (k, v) in list(dic.items()):
		if k not in length:
			length[k] = 0
		length[k] = len(v)
	(l, turn, prov) = (len(S), 0, 0)
	for t in T:
		if t not in dic:
			print(-1)
			return
		x = bisect.bisect(dic[t], prov)
		if x < length[t]:
			prov = dic[t][x]
		else:
			turn += 1
			prov = dic[t][0]
	print(turn * l + prov)
solve()


from bisect import bisect_left

def solve():
	S = input()
	T = input()
	if not set(T) <= set(S):
		return -1
	pos = {c: [] for c in 'abcdefghijklmnopqrstuvwxyz'}
	for (i, c) in enumerate(S):
		pos[c].append(i)
	ans = 0
	cur_pos = 0
	for t in T:
		nxt_idx = bisect_left(pos[t], cur_pos)
		if nxt_idx == len(pos[t]):
			ans += len(S)
			cur_pos = 0
			nxt_idx = bisect_left(pos[t], cur_pos)
			cur_pos = pos[t][nxt_idx] + 1
		else:
			cur_pos = pos[t][nxt_idx] + 1
	return ans + cur_pos
print(solve())


from bisect import bisect_right, bisect_left
S = input()
T = input()

def calc(ch):
	return ord(ch) - ord('a')
LST = [[] for _ in range(26)]
for t in T:
	nt = calc(t)
	if len(LST[nt]) > 0:
		continue
	for i in range(len(S)):
		if t == S[i]:
			LST[nt].append(i)
	if len(LST[nt]) == 0:
		print(-1)
		return
NS = len(S)
NofLoop = 0
now = 0
for i in range(len(T)):
	t = T[i]
	nt = calc(t)
	rel_now = now - NofLoop * NS
	if rel_now < LST[nt][0]:
		now = NofLoop * NS + LST[nt][0] + 1
	elif LST[nt][-1] < rel_now:
		NofLoop += 1
		now = NofLoop * NS + LST[nt][0] + 1
	else:
		idx = bisect_left(LST[nt], rel_now)
		now = NofLoop * NS + LST[nt][idx] + 1
print(now)


(S, T) = (input(), input())

def func(S, T):
	merge_S = list(set(S))
	merge_T = list(set(T))
	for t in merge_T:
		if t not in merge_S:
			return -1
	S_2 = S + S
	len_s = len(S)
	ans = -1
	for t in T:
		ans += S_2[(ans + 1) % len_s:].index(t) + 1
	return ans + 1
print(func(S, T))


def solve():
	S = input()
	T = input()
	N = len(S)
	M = 2 * N
	S += S
	nxt = [[M] * 26 for i in range(M + 1)]
	for i in range(M - 1, -1, -1):
		for j in range(26):
			nxt[i][j] = nxt[i + 1][j]
		nxt[i][ord(S[i]) - ord('a')] = i
	ans = 0
	i = 0
	for c in T:
		pos = ord(c) - ord('a')
		if nxt[i][pos] == M:
			return -1
		ans += nxt[i][pos] - i + 1
		i = (nxt[i][pos] + 1) % N
	return ans
print(solve())


s = input()
t = input()
s_ = s[:]
cnt = 0
tlen = len(t)
ans = 0
flag = True
while cnt < tlen:
	num = s_.find(t[cnt])
	if num != -1:
		ans += num + 1
		s_ = s_[num + 1:]
		cnt += 1
		flag = False
	else:
		if flag:
			print(-1)
			exit(0)
		ans += len(s_)
		s_ = s[:]
		flag = True
print(ans)


from bisect import bisect_left
s = input()
n = len(s)
s = s + s
t = input()
m = len(t)
ls = [[] for _ in range(26)]
for i in range(2 * n):
	ls[ord(s[i]) - 97].append(i)
res = 0
flg = True
ind = -1
for i in range(m):
	if ls[ord(t[i]) - 97] == []:
		flg = False
		break
	nind = ls[ord(t[i]) - 97][bisect_left(ls[ord(t[i]) - 97], ind + 1)]
	res += nind - ind
	if nind >= n:
		nind -= n
	ind = nind
if flg:
	print(res)
else:
	print(-1)


import sys
input = sys.stdin.readline
S = input().rstrip()
T = input().rstrip()
if not set(T) <= set(S):
	print(-1)
	return
ind = -1
cnt = 0
for t in T:
	ind = S.find(t, ind + 1)
	if ind == -1:
		cnt += 1
		ind = S.find(t)
print(len(S) * cnt + ind + 1)


from bisect import bisect_left
s = input()
t = input()
n = len(s)
s = s * 2
r = {}
for (i, v) in enumerate(s):
	if v in r.keys():
		r[v].append(i + 1)
	else:
		r[v] = [i + 1]
now = 0
lt = len(t)
for v in t:
	if v in r.keys():
		index = bisect_left(r[v], now % n + 1)
		tmp = r[v][index]
		now += tmp - now % n
	else:
		print(-1)
		exit()
print(now)


s = input()
t = input()
ct = 0
k = 0
for i in t:
	f = s.find(i, k)
	if f < 0:
		ct += 1
		f = s.find(i)
		if f == -1:
			print(-1)
			break
		else:
			k = f + 1
	else:
		k = f + 1
else:
	print(ct * len(s) + k)


A = tuple(range(10 ** 5))
S = input()
T = input()
lenS = len(S)
lenT = len(T)
F1 = [False] * 26
F2 = [False] * 26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(lenS):
	F1[alpha.index(S[i])] = True
for i in range(lenT):
	F2[alpha.index(T[i])] = True
for i in range(26):
	if not F1[i] and F2[i]:
		print(-1)
		return
ind = S.index(T[0])
ans = ind + 1
for i in range(1, lenT):
	S = S[ind + 1:] + S[:ind + 1]
	ind = S.index(T[i])
	ans += ind + 1
print(ans)


s = input()
t = input()
n = len(s)
now = -1
count = 0
if set(t) <= set(s):
	for i in t:
		now = s.find(i, now + 1)
		if now == -1:
			count += 1
			now = s.find(i)
	print(now + 1 + count * n)
else:
	print(-1)


import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]: [] for i in range(len(S) - 1)}
for i in range(len(S) - 1):
	E[S[i]].append(i)
T = input()
a = len(S) - 1
ans = 0
now = -1
for i in range(len(T) - 1):
	if not T[i] in E.keys():
		print(-1)
		exit()
	s = E[T[i]]
	b = bisect.bisect_right(s, now)
	if b == len(s):
		ans += s[0] + 1 + a - 1 - now
		now = s[0]
	else:
		ans += s[b] - now
		now = s[b]
print(ans)


s = input()
t = input()
ans = 0
for ch in t:
	if ch not in s:
		print('-1')
		exit()
x = s
for ch in t:
	p = x.find(ch) + 1
	if p == 0:
		ans += len(x)
		p = s.find(ch) + 1
		x = s
	ans += p
	x = x[p:]
print(ans)


def find(base, indexes):
	n = len(indexes)
	ng = -1
	ok = n
	while ok - ng > 1:
		mid = (ok + ng) // 2
		if indexes[mid] > base:
			ok = mid
		else:
			ng = mid
	return indexes[0] if ok == n else indexes[ok]
s = input()
t = input()
s_set = set(s)
t_set = set(t)
if t_set.issubset(s_set) is False:
	print(-1)
	return
s_indxes = [[] for _ in range(26)]
for i in range(len(s)):
	c = s[i]
	idx = ord(c) - ord('a')
	s_indxes[idx].append(i)
base = -1
loop = 0
for i in range(len(t)):
	c = t[i]
	idx = ord(c) - ord('a')
	n_base = find(base, s_indxes[idx])
	if n_base <= base:
		loop += 1
	base = n_base
print(loop * len(s) + base + 1)


import math, itertools, fractions, heapq, collections, bisect, sys, queue, copy
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return [int(x) for x in sys.stdin.readline().split()]

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def LS():
	return sys.stdin.readline().split()

def S():
	return input()

def main():
	s = S()
	n = len(s)
	t = S()
	n2 = len(t)
	d = {}
	for (i, x) in enumerate(s):
		if x in d:
			y = d[x]
			y.append(i)
			d[x] = y
		else:
			d[x] = [i]
	i = 0
	j = 0
	ans = 1
	while j < n2:
		check = t[j]
		if check not in d:
			return -1
		l = d[check]
		if i < l[0]:
			ans += l[0] - i + 1
			i = l[0] + 1
			j += 1
		elif l[-1] < i:
			ans += n - (i + 1) + l[0] + 2
			i = l[0] + 1
			j += 1
		elif i == l[0]:
			ans += 1
			i += 1
			j += 1
		elif i == l[-1]:
			ans += 1
			i += 1
			j += 1
		else:
			left = 0
			right = len(l)
			while right - left > 1:
				m = (right + left) // 2
				if l[m] >= i:
					right = m
				else:
					left = m
			ans += l[right] - i + 1
			i = l[right] + 1
			j += 1
		i %= n
	return ans - 1
print(main())


from bisect import bisect_left, bisect
s = list(input())
sn = len(s)
t = list(input())
tn = len(t)
kouho = []
for i in range(26):
	kar = []
	now = chr(ord('a') + i)
	for j in range(sn):
		if s[j] == now:
			kar.append(j)
	kouho.append(kar)
cou = 0
now = -1
for i in range(tn):
	u = ord(t[i]) - ord('a')
	if kouho[u]:
		tugi = bisect(kouho[u], now)
		if tugi == len(kouho[u]):
			cou += 1
			now = kouho[u][0]
		else:
			now = kouho[u][tugi]
	else:
		print(-1)
		return
print(cou * sn + now + 1)


from bisect import *

def alph_to_num(s):
	return ord(s) - ord('a')

def solve():
	ans = 0
	S = input()
	T = input()
	lis = [[] for _ in range(26)]
	for (i, s) in enumerate(S):
		lis[alph_to_num(s)].append(i)
	ind = -1
	for t in T:
		n = alph_to_num(t)
		if not len(lis[n]):
			return -1
		if ind >= lis[n][-1]:
			ans += len(S)
			ind = lis[n][0]
		else:
			ind = lis[n][bisect_right(lis[n], ind)]
	ans += ind + 1
	return ans
print(solve())


from bisect import bisect
s = input()
t = input()
a = 'abcdefghijklmnopqrstuvwxyz'
d = {}
for ch in a:
	d[ch] = []
for (ch, n) in zip(s, range(len(s))):
	d[ch].append(n)
rep = 0
p = -1
for ch in t:
	if len(d[ch]) == 0:
		print('-1')
		exit()
	else:
		i = bisect(d[ch], p)
		if i != len(d[ch]):
			p = d[ch][i]
		else:
			p = d[ch][0]
			rep += 1
print(len(s) * rep + p + 1)


from collections import defaultdict
import bisect
S = input()
T = input()
if set(T) - set(S):
	print(-1)
	exit()
dic = defaultdict(list)
for (pos, s) in enumerate(S):
	dic[s].append(pos)
now = -1
count = 0
for t in T:
	if dic[t][-1] < now + 1:
		now = -1
		count += 1
	idx = bisect.bisect_left(dic[t], now + 1)
	now = dic[t][idx]
ans = count * len(S) + now + 1
print(ans)


s = input()
t = input()
moji = list(set(s))
if all([k in moji for k in set(t)]) is not True:
	print(-1)
else:
	a = 0
	l = len(s)
	seq = s
	for k in t:
		p = seq.find(k) + 1
		if p == 0:
			p = s.find(k) + 1
			a += p + len(seq)
			seq = s
		else:
			a += p
		seq = seq[p:]
	print(a)


from bisect import *
s = input()
t = input()
n = len(s)
d = dict(zip([chr(i + 97) for i in range(26)], [[] for _ in range(26)]))
for (i, j) in enumerate(s, 1):
	d[j] += [i]
ans = 0
cnt = 0
for i in t:
	if d[i]:
		j = bisect(d[i], ans)
		if j == len(d[i]):
			cnt += 1
			ans = d[i][0]
		else:
			ans = d[i][j]
	else:
		(cnt, ans) = (0, -1)
		break
print(cnt * n + ans)


from string import ascii_lowercase
from bisect import bisect_right, bisect_left

def main():
	s = input()
	t = input()
	len_s = len(s)
	s_all = {s: [] for s in ascii_lowercase}
	for i in range(len_s):
		s_all[s[i]].append(i + 1)
	ans = 0
	if any((s_all[tt] == [] for tt in t)):
		ans = -1
	else:
		s_ind = 1
		for ss in ascii_lowercase:
			s_all[ss].sort()
		for i in range(len(t)):
			next_ind = bisect_left(s_all[t[i]], s_ind)
			if next_ind == len(s_all[t[i]]):
				ans += len_s - s_ind + 1
				ans += s_all[t[i]][0]
				s_ind = s_all[t[i]][0] + 1
			else:
				ans += s_all[t[i]][next_ind] - s_ind + 1
				s_ind = s_all[t[i]][next_ind] + 1
	print(ans)

def __starting_point():
	main()
__starting_point()


def main():
	s = input()
	t = input()
	if not set(t).issubset(set(s)):
		return -1
	m = 0
	st = s
	for i in t:
		if i in st:
			j = st.index(i)
			m += j + 1
			st = st[j + 1:]
		else:
			m += len(st)
			st = s
			j = st.index(i)
			st = st[j + 1:]
			m += j + 1
	return m
print(main())


from bisect import bisect
s = list(input())
t = list(input())
lenT = len(t)
lenS = len(s)
alp = []
for i in range(26):
	array = []
	now = chr(ord('a') + i)
	for j in range(lenS):
		if s[j] == now:
			array.append(j)
	alp.append(array)
(count, now) = (0, -1)
for i in range(lenT):
	j = ord(t[i]) - ord('a')
	if alp[j]:
		next = bisect(alp[j], now)
		if next == len(alp[j]):
			count += 1
			now = alp[j][0]
		else:
			now = alp[j][next]
	else:
		print(-1)
		return
ans = count * lenS + now + 1
print(ans)


s = input()
t = input()
temp = -1
cycle = 0
ans = True
for tt in t:
	if tt in s[temp + 1:]:
		k = s[temp + 1:].find(tt)
		temp += k + 1
	elif tt in s:
		temp = s.find(tt)
		cycle += 1
	else:
		ans = False
		break
if ans:
	print(cycle * len(s) + temp + 1)
else:
	print(-1)


from collections import defaultdict
from bisect import bisect_left
s = input()
t = input()
if set(s) >= set(t):
	d = defaultdict(list)
	for (i, x) in enumerate(s * 2):
		d[x].append(i)
	ans = 0
	cur = 0
	N = len(s)
	for x in t:
		cur_new = d[x][bisect_left(d[x], cur)]
		ans += cur_new - cur + 1
		cur = (cur_new + 1) % N
	print(ans)
else:
	print(-1)


import sys
s = input()
t = list(input())
string = s
length = 1
for i in range(len(t)):
	if s.find(t[i]) == -1:
		print(-1)
		sys.exit()
	if string.find(t[i]) == -1:
		string = s
		length += 1
	string = string[string.find(t[i]) + 1:]
print(length * len(s) - len(string))