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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.rmi.server.UnicastRemoteObject;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main //implements Runnable
{
    static long mod=1000000007;
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};


    public static void main(String[] args)   throws Exception, IOException{        
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);

//        int n=sc.nextInt();
        String s=sc.nextString();
        String t=sc.nextString();
        char c[]=s.toCharArray();
        char d[]=t.toCharArray();
//        int x[]=new int[26];
//        int y[]=new int[26];
//        
//        for (int i = 0; i < c.length; i++) {
//			x[c[i]-'a']++;
//		}
//        for (int i = 0; i < d.length; i++) {
//			y[d[i]-'a']++;
//		}
        long ans=0;
//        for (int i = 0; i < y.length; i++) {
//			if(y[i]>0 && x[i]==0) {
//				ans=-1;
//				break;
//			}
//		}
    	int ds[][]=new int[26][c.length];
    	int ta[]=new int[26];
    	int inf=1<<28;
    	fill(ta,inf);
    	for (int i = c.length-1; -1 < i; i--) {
    		ta[c[i]-'a']=i;
			for (int j = 0; j < 26; j++) {
				ds[j][i]=ta[j];
			}
		}
    	fill(ta,inf);
    	for (int i = 0; i < c.length; i++) {
    		ta[c[i]-'a']=min(ta[c[i]-'a'],i);
			for (int j = 0; j < 26; j++) {
				if(ds[j][i]==inf)ds[j][i]=ta[j];
			}
		}
//    	for (int i = 0; i < 26; i++) {
//    		db(ds[i]);
//    	}
//    	
    	for (int i = 0,p=0; i < d.length; i++) {
    		int x=d[i]-'a';
//    		db(i,x,p,ds[x][p],ans);
    		if(ds[x][p]==inf) {
    			ans=-1;
    			break;
    		}
    		if(p<=ds[x][p])ans+=ds[x][p]-p;
    		else ans+=c.length-(-ds[x][p]+p);
    		p=ds[x][p]+1;
    		p%=c.length;
    		ans++;
		}
//    	contest
//    	son
        
        out.println(ans);
        out.flush();
    }


    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }
}

//class P {
//	int x, y;
//	P(int  x, int y) {
//	  this.x=x;
//	  this.y=y;
//	}
//}


//class P {
//    int id, d;
//    P(int  id, int d) {
//        this.id=id;
//        this.d=d;
//    }
//}

class P implements Comparable<P>{
    int a,b;
    P(int a, int b) {
    	this.b=b;
    	this.a=a;
    }

    public int compareTo(P p){
    	if(a==p.a) return b-p.b;
        return -p.a+a;
    }
}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) throws IOException{
        int r[] = new int[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextInt(); 
        }
        return r;
    }
    public long[] nextLongArray(int size) throws IOException{
        long r[] = new long[size];
        for (int i = 0; i < size; i++) {
            r[i] = this.nextLong(); 
        }
        return r;
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}