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: Accepted)

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

namespace PAST0
{
    using static Ex;
    public class Solve
    {
        public static void Main(string[] args)
        {
            new PAST_K();
        }
    }
    public class PAST_K
    {
        char[] s, t;
        public PAST_K()
        {
            s = Read.ToCharArray();
            t = Read.ToCharArray();

            int len = 'z' - 'a' + 1;
            var list = new List<long>[len];
            while (len > 0)
            {
                len--;
                list[len] = new List<long>();
            }

            for (long i = 0; i < s.Length; i++)
            {
                var c = s[i];
                list[c - 'a'].Add(i);
            }

            long loop = -1;
            long j = s.Length;
            for (long i = 0; i < t.Length; i++)
            {
                var c = list[t[i] - 'a'];
                if (!c.Any())
                {
                    (-1).Cw();
                    return;
                }
                var index = UpperBound(c, j);
                if (index < 0) index = ~index;

                if (index < c.Count)
                    j = c[index];
                else
                {
                    loop++;
                    j = c[0];
                }
            }

            long min = s.Length * loop + j + 1;
            Math.Max(loop + 1, min).Cw();
        }
    }
    public class PAST_L
    {
        public PAST_L()
        {
        }
    }
    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }


        static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
        public static int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
        public static long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }

        public static int LowerBound<T>(T[] a, T v)
        {
            return LowerBound(a, v, Comparer<T>.Default);
        }

        public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Length - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res == -1) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }

        public static int UpperBound<T>(List<T> a, T v)
        {
            return UpperBound(a, v, Comparer<T>.Default);
        }

        public static int UpperBound<T>(List<T> a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Count - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res <= 0) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }

        public class PriorityQueue<T> where T : IComparable<T>
        {
            List<T> list = new List<T>();
            void PushHeap(T v)
            {
                int n = list.Count;
                list.Add(v);

                while (n != 0)
                {
                    int i = (n - 1) / 2;
                    if (list[n].CompareTo(list[i]) > 0)
                        Swap(list, i, n);
                    n = i;
                }
            }
            void PopHeap()
            {
                int n = list.Count - 1;
                list[0] = list[n];
                list.RemoveAt(n);

                for (int i = 0, j; (j = 2 * i + 1) < n;)
                {
                    if ((j != n - 1) && (list[j].CompareTo(list[j + 1]) < 0))
                        j++;
                    if (list[i].CompareTo(list[j]) < 0)
                        Swap(list, i, j);
                    i = j;
                }
            }
            public void Push(T elem) { PushHeap(elem); }
            public T Pop() { var v = Peek; PopHeap(); return v; }
            public T Peek { get { return list[0]; } }
            public int Count { get { return list.Count; } }
        }

    }
    public struct Mod : IEquatable<Mod>, IEquatable<long>
    {
        static public long _mod = 1000000007; long v;
        public Mod(long x) { if (x < _mod && x >= 0) v = x; else if ((v = x % _mod) < 0) v += _mod; }
        static public implicit operator Mod(long x) => new Mod(x);
        static public implicit operator long(Mod x) => x.v;
        public void Add(Mod x) { if ((v += x.v) >= _mod) v -= _mod; }
        public void Sub(Mod x) { if ((v -= x.v) < 0) v += _mod; }
        public void Mul(Mod x) => v = (v * x.v) % _mod;
        public void Div(Mod x) => v = (v * Inverse(x.v)) % _mod;
        static public Mod operator +(Mod x, Mod y) { var t = x.v + y.v; return t >= _mod ? new Mod { v = t - _mod } : new Mod { v = t }; }
        static public Mod operator -(Mod x, Mod y) { var t = x.v - y.v; return t < 0 ? new Mod { v = t + _mod } : new Mod { v = t }; }
        static public Mod operator *(Mod x, Mod y) => x.v * y.v;
        static public Mod operator /(Mod x, Mod y) => x.v * Inverse(y.v);
        static public bool operator ==(Mod x, Mod y) => x.v == y.v;
        static public bool operator !=(Mod x, Mod y) => x.v != y.v;
        static public long Inverse(long x) { long b = _mod, r = 1, u = 0, t = 0; while (b > 0) { var q = x / b; t = u; u = r - q * u; r = t; t = b; b = x - q * b; x = t; } return r < 0 ? r + _mod : r; }
        public bool Equals(Mod x) => v == x.v;
        public bool Equals(long x) => v == x;
        public override bool Equals(object x) => x == null ? false : Equals((Mod)x);
        public override int GetHashCode() => v.GetHashCode();
        public override string ToString() => v.ToString();
        static List<Mod> _fact = new List<Mod>() { 1 };
        static void B(long n) { if (n >= _fact.Count) for (int i = _fact.Count; i <= n; ++i) _fact.Add(_fact[i - 1] * i); }
        static public Mod Comb(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)] / _fact[(int)k]; }
        static public Mod Perm(long n, long k) { B(n); if (n == 0 && k == 0) return 1; if (n < k || n < 0) return 0; return _fact[(int)n] / _fact[(int)(n - k)]; }
    }
}