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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        var wrong_answer = new wrong_answer();
        wrong_answer.Solve();
    }
}

public class wrong_answer
{
    const int INF = 1 << 30;
    const long INFL = 1L << 60;
    const int MOD = 1000000007;
    const int MOD2 = 998244353;

    myinout IO = new myinout();
    Math2 mmath = new Math2();


    public void Solve()
    {
        //ここから
        var s = IO.str;
        var t = IO.str;
        var wh = Enumerable.Range(0, 26).Select(x => new List<int>()).ToArray();
        for (int i = 0; i < s.Length; ++i)
        {
            wh[s[i] - 'a'].Add(i);
            wh[s[i] - 'a'].Add(i + s.Length);
        }
        long n = s.Length;
        int pre = 0;
        long ans = 0;
        for (int i = 0; i < t.Length;++i){
            if(wh[t[i]-'a'].Count==0){
                IO.wri(-1);
                return;
            }
            pre=CompLib.Algorithms.Algorithm.LowerBound(wh[t[i] - 'a'], pre)+1;
            if(pre>=n){
                pre -= (int)n;
                ans += n;
            }

        }
        ans += pre;
        IO.wri(ans);

    }
}

    namespace CompLib.Algorithms
    {
    
        #region BinarySearch
        public static partial class Algorithm
        {
            static int binarySearch<T>(IList<T> a, T v, IComparer<T> cmp, bool islb)
            {
                var l = 0;
                var r = a.Count - 1;
                while (l <= r)
                {
                    var m = (l + r) / 2;
                    var res = cmp.Compare(a[m], v);
                    if (res < 0 || (res == 0 && !islb)) l = m + 1;
                    else r = m - 1;
                }
                return l;
            }
    
            /// <summary>
            /// 与えられた比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> 以上の要素であるような最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"></see> の要素の型を指定します．</typeparam>
            /// <param name="a">対象となるコレクション</param>
            /// <param name="v">対象となる要素</param>
            /// <param name="f"></param>
            /// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int LowerBound<T>(this IList<T> a, T v, Comparison<T> f) { return binarySearch(a, v, Comparer<T>.Create(f), true); }
    
            /// <summary>
            ///　デフォルトの比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> 以上の要素であるような最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"></see> の要素の型を指定します．</typeparam>
            /// <param name="a">対象となるコレクション</param>
            /// <param name="v">対象となる要素</param>
            /// <param name="f"></param>
            /// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int LowerBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, true); }
    
            /// <summary>
            /// 与えられた比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> 以上の要素であるような最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"></see> の要素の型を指定します．</typeparam>
            /// <param name="a">対象となるコレクション</param>
            /// <param name="v">対象となる要素</param>
            /// <param name="f"></param>
            /// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int UpperBound<T>(this IList<T> a, T v, Comparison<T> cmp) { return binarySearch(a, v, Comparer<T>.Create(cmp), false); }
    
            /// <summary>
            ///　デフォルトの比較関数に従って，<paramref name="a"/> の要素のうち，<paramref name="v"/> より真に大きい要素が現れる最小のインデックスを取得します．
            /// </summary>
            /// <typeparam name="T"><see cref="IList{T}"></see> の要素の型を指定します．</typeparam>
            /// <param name="a">対象となるコレクション</param>
            /// <param name="v">対象となる要素</param>
            /// <param name="f"></param>
            /// <returns><paramref name="v"/> 以上の要素であるような最小の o-indexed でのインデックス．</returns>
            /// <remarks> <paramref name="a"/> は比較関数に対して昇順であることを仮定しています．この関数は O(log N) で実行されます．</remarks>
            public static int UpperBound<T>(this IList<T> a, T v) { return binarySearch(a, v, Comparer<T>.Default, false); }
    
        }
        #endregion
    }


//b-aで降順

public class Math2
{
    public long[] fact;
    public long[] invfact;
    public void factoricalmodset(long N, long MOD = 1000000007)
    {
        fact = new long[N + 1];
        invfact = new long[N + 1];
        fact[0] = 1;

        for (long i = 1; i <= N; ++i)
        {
            fact[i] = fact[i - 1] * i % MOD;
        }
        invfact[N] = pow(fact[N], MOD - 2);

        for (long i = N; i > 0; --i)
        {
            invfact[i - 1] = invfact[i] * i % MOD;
        }

    }

    public long nCr(long n, long r, long MOD = 1000000007)
    {
        return (fact[n] * invfact[r] % MOD * invfact[n - r] % MOD);
    }

    public long pow(long i, long n, long MOD = 1000000007)
    {
        long res = 1;
        while (n > 0)
        {
            if ((n & 1) != 0) res = res * i % MOD;
            i = i * i % MOD;
            n >>= 1;
        }
        return res;
    }

    public long gcd(long i, long y)
    {
        while (y != 0)
        {
            var r = i % y;
            i = y;
            y = r;
        }
        return i;
    }

    public long lcm(long i, long y)
    {
        return i * y / gcd(i, y);
    }

    public Dictionary<long, int> primefactorization(long N)
    {
        var ret = new Dictionary<long, int>();

        for (long i = 2; i * i <= N; ++i)
        {
            int cnt = 0;
            while (N % i == 0)
            {
                cnt++;
                N /= i;
            }
            if (cnt != 0) ret[i] = cnt;

        }
        if (N >= 2) ret[N] = 1;

        return ret;

    }

    public List<long> divisorenumrate(long N)
    {
        var ret = new List<long>();

        for (long i = 1; i * i <= N; ++i)
        {

            if (N % i == 0)
            {
                ret.Add(i);
                ret.Add(N / i);
            }

        }

        return ret;

    }

    public void swap<T>(ref T a, ref T b)
    {
        var i = a;
        a = b;
        b = i;
    }

}

class myinout
{
    //in
    public string str => Console.ReadLine();

    public string[] strm => str.Split(' ');

    public long[] longm => strm.Select(long.Parse).ToArray();
    public int[] intm => strm.Select(int.Parse).ToArray();
    public char[] charm => str.ToArray();
    public long long1 => longm[0];
    public int int1 => intm[0];
    public char char1 => charm[0];

    //out
    public void wri<T>(T i)
    {
        Console.WriteLine(i);
    }
    public void wri<T>(T[] i)
    {
        Console.WriteLine(string.Join(" ", i));
    }

    public void yn(bool i)
    {
        if (i) wri("Yes");
        else wri("No");
    }

}