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 Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

program kadai
   implicit none
    character(len=100000)  ::  s
    character(len=100000)  ::  t
    character(len=1),allocatable  ::  sos(:)
    character(len=1),allocatable  ::  sot(:)
    integer  ::  i,all
    integer(8)  ::  count
    integer  ::  size_s,size_t
    integer  ::  flag,sflag,j

    read(*,*) s
    read(*,*) t

    size_s=len_trim(s)
    size_t=len_trim(t)
    allocate(sos(size_s))
    allocate(sot(size_t))
    !write(*,*) len_trim(s),len_trim(t)

    do i=1,size_s
        sos(i)=s(i:i)
    end do
    do i=1,size_t
        sot(i)=t(i:i)
    end do

    call qsort(sos)
    call qsort(sot)
    !write(*,*) sos
    !write(*,*) sot
    j=1
    flag=0
    sflag=0
    do i=1,size_s
        if (sos(i)==sot(j)) then
            do while(sos(i)==sot(j))
                flag=flag+1
                j=j+1
            end do
        end if
        if (j==size_t+1) then
            sflag=1
            exit
        end if
    end do

    if (sflag==0) then
        write(*,*) -1
        stop
    end if


    all=1
    count=1
    j=1
    do
        do i=1,size_s
            if (s(i:i)==t(j:j)) then
                j=j+1
            end if
            count=count+1
            if (j==size_t+1) then
                write(*,*) count-1
                stop
            end if
        end do
    end do

stop
contains

subroutine swap(a,b)
implicit none
character(len=*),intent(inout)  ::  a,b
character(len=10)  ::  c
c=a
a=b
b=c
end subroutine swap

subroutine qsort(x)
implicit none
character(len=*), intent(inout)  ::  x(:)
integer  ::  first,last
first=1
last=size(x)
call q2sort(x,first,last)
return
end subroutine qsort

recursive subroutine q2sort(x,first,last)
implicit none
character(len=*), intent(inout)  ::  x(:)
integer, intent(inout) ::  first,last
integer  ::  fir, las
integer  ::  i,j
character(len=10)  ::  n
n=x((first+last)/2)
i=first
j=last
do
do while (x(i)<n)
i=i+1
end do
do while(x(j)>n)
j=j-1
end do
if (i>j .or. i==j) then
exit
end if
call swap(x(i),x(j))
i=i+1
j=j-1
end do
if (first<i-1) then
las=i-1
call q2sort(x,first,las)
end if
if (last>j+1) then
fir=j+1
call q2sort(x,fir,last)
end if
end subroutine q2sort


end program kadai