Problem:
Score : 500 points

Problem Statement

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 1

contest
son

Sample Output 1

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.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.

Solution in Julia (0.5.0) (judge verdict: Accepted)

parseInt(x) = parse(Int, x)
parseMap(x::Array{SubString{String},1}) = map(parseInt, x)
function bsearch(a,x);l,r=0,length(a)+1;while r-l>1;m=(r+l)>>1;if a[m]<x;l=m;else;r=m;end;end;r;end

function main()
	ss = readline() |> chomp
	tt = readline() |> chomp
	s = Int[Int(ss[i])-96 for i in 1:length(ss)]
	t = Int[Int(tt[i])-96 for i in 1:length(tt)]
	n = length(s)
	d = Dict{Int,Array{Int,1}}()
	dp = -ones(Int,n+1,26)
	for i in 1:n
		if haskey(d,s[i])
			push!(d[s[i]],i)
		else
			d[s[i]] = Int[i]
		end
	end
	for i in 1:n
		push!(d[s[i]],n+i)
	end
	for i in 1:n
		for j in keys(d)
			dp[i,j]=d[j][bsearch(d[j],i)]
		end
	end
	k = 0
	now = 0
	for i in 1:length(t)
		if dp[now+1,t[i]]<0
			k = -1
			break
		else
			now = dp[now+1,t[i]]
			if now>=n
				k += n
				now = now%n
			end
		end
	end
	println(k==-1?-1:k+now)
end

main()