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 C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

using System;
using System.Numerics;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace debug
{
    class main 
    {
        static void Main(string[] args)
        {
            //問題クラスを展開
            ProgramE a = new ProgramE();
            a.main();//実行する
            
        }

    }

    class ProgramA
    {
        public void main()
        {
            //入力
            string input = Console.ReadLine();
            int a = int.Parse(input);
            string s = Console.ReadLine();

            if (a >= 3200)
                Console.WriteLine(s);
            else
                Console.WriteLine("red");

        }


    }

    class ProgramB
    {
        public void main()
        {
            //入力
            string input = Console.ReadLine();
            int n = int.Parse(input);

            string[] a = Console.ReadLine().Split(' ');
            double sum = 0;

            for (int i = 0; i < n; i++)
            {
                double ai = double.Parse(a[i]);
                sum += 1 / ai;

            }

            Console.WriteLine(1.0 / sum);
        }


    }

    class ProgramC
    {
        public void main()
        {
            //入力
            string input = Console.ReadLine();
            int n = int.Parse(input);


            string[] input_b = Console.ReadLine().Split(' ');
            double[] b = new double[n];
            for (int i = 0; i < n; i++)
                b[i] = double.Parse(input_b[i]);

            Array.Sort(b);

            for (int i = 1; i < n; i++)
            {
                double temp = (b[i - 1] + b[i]) / 2.0;
                b[i] = temp;
            }

            double ans = b[n - 1];

            Console.WriteLine(ans);
        }


    }

    class ProgramD
    {
        public void main()
        {
            string[] input = Console.ReadLine().Split(' ');

            int n = int.Parse(input[0]);
            int q = int.Parse(input[1]);
            List<Root> root = new List<Root>();
            List<Root> qes = new List<Root>();

            //親子を記録しておく
            for (int i = 0; i < n - 1; i++)
            {
                string[] t = Console.ReadLine().Split(' ');
                int a = int.Parse(t[0]);
                int b = int.Parse(t[1]);
                if(b < a)
                {
                    int te = b;
                    b = a;
                    a = te;
                }
                root.Add(new Root(a, b));
            }

            //お題を入れる
            for (int i = 0; i < q; i++)
            {
                string[] t = Console.ReadLine().Split(' ');
                int a = int.Parse(t[0]);
                int b = int.Parse(t[1]);
                qes.Add(new Root(a, b));
            }

            //お題と木の関係はソート
            var root_s = root.OrderBy(x => x.par);

            //答代入
            int[] cost = new int[n];

            //あらかじめ入れておく
            foreach (var s in qes)
                cost[s.par - 1] += s.child;
            //根の方から答えを入れていく（小さい順になる）
            foreach (var r in root_s)
                cost[r.child - 1] += cost[r.par - 1];

            //答
            for (int i = 0; i < n; i++)
                Console.WriteLine(cost[i]);

        }

    }

    public class Root
    {
        public int par;
        public int child;
        public Root(int par, int child)
        {
            this.par = par;
            this.child = child;
        }

    }



    class ProgramE
    {
        public void main()
        {
            //入力
            string s = Console.ReadLine();
            string t = Console.ReadLine();

            string s2 = s + s;//2つ足すことで奇数個しかない場合も、後ろの方に追加が単純になる

            long ans = 0;//ここに答えの位置を入れていく

            //あらかじめどういう文字列か把握する
            int[,] chart = new int[26, s.Length];
            //ないものを判定する
            for (int i = 0; i < s.Length; i++)
                for (int j = 0; j < 26; j++)
                    chart[j, i] = -1;
            //ある場合
            for(int i = 0;i < s2.Length;i++)
            {
                int c = s2[i] - 'a';
                //i番目より前を埋めていく
                for (int j = i; j >= 0; j--)
                {
                    if (j >= s.Length)
                        continue;
                    //もし順序が与えられているならそれ以前は決まっている
                    if (chart[c, j] != -1)
                        break;
                    //和を足していく
                    chart[c, j] = i - j + 1;
                }

            }

            //tの文字列を貪欲法で探していく
            for (int i = 0;i < t.Length;i++)
            {
                int c = t[i] - 'a';
                if(chart[c, ans % s.Length] == -1)
                {
                    Console.WriteLine("-1");
                    return;
                }

                ans += chart[c, ans % s.Length];
            }

           

            //答え出力
            Console.WriteLine(ans);


        }

       

    }

    public class Cpos
    {
        public int c;
        public int pos;
        public Cpos(int c, int pos)
        {
            this.c = c;
            this.pos = pos;
        }

    }


}