stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <string>

#include <iostream>

#include <algorithm>

using namespace std;

int H, W; string s[1009], t[3], x = "JOI";

int main() {

	cin >> H >> W;

	for (int i = 0; i < H; i++) cin >> s[i];

	cin >> t[0] >> t[1];

	int sum = 0;

	for (int i = 0; i < H - 1; i++) {

		for (int j = 0; j < W - 1; j++) {

			bool flag = true;

			for (int k = 0; k < 2; k++) {

				for (int l = 0; l < 2; l++) {

					if (s[i + k][j + l] != t[k][l]) flag = false;

				}

			}

			if (flag) sum++;

		}

	}

	int ret = 0;

	for (int i = 0; i < H; i++) {

		for (int j = 0; j < W; j++) {

			char e = s[i][j];

			int a[3] = { 0 }, z = -1;

			for (int c = 0; c < 3; c++) {

				if (x[c] == e) z = c;

				s[i][j] = x[c];

				for (int k = -1; k < 1 && i + k + 1 < H; k++) {

					if (i + k < 0) continue;

					for (int l = -1; l < 1 && j + l + 1 < W; l++) {

						if (j + l < 0) continue;

						bool flag = true;

						for (int m = 0; m < 2; m++) {

							for (int n = 0; n < 2; n++) {

								if (s[i + k + m][j + l + n] != t[m][n]) flag = false;

							}

						}

						if (flag) a[c]++;

					}

				}

			}

			for (int c = 0; c < 3; c++) ret = max(ret, sum + a[c] - a[z]);

			s[i][j] = e;

		}

	}

	cout << ret << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.312009766694083x):
#include <cstdio>

int H, W, bs, res, ret; char c, s[1009][1009], t[5][5], r[] = "JOI";

bool check(int x, int y) { return 0 <= x && x < H - 1 && 0 <= y && y < W - 1 && s[x][y] == t[0][0] && s[x][y + 1] == t[0][1] && s[x + 1][y] == t[1][0] && s[x + 1][y + 1] == t[1][1]; }

int main() {

	scanf("%d%d", &H, &W);

	for(int i = 0; i < H; i++) scanf("%s", s[i]);

	scanf("%s%s", t[0], t[1]);

	for(int i = 0; i < H; i++) {

		for(int j = 0; j < W; j++) {

			bs += check(i, j); c = s[i][j];

			for(int k = 0; k < 3; k++) {

				res = -check(i - 1, j - 1) - check(i - 1, j) - check(i, j - 1) - check(i, j);

				s[i][j] = r[k];

				res += check(i - 1, j - 1) + check(i - 1, j) + check(i, j - 1) + check(i, j);

				ret = ret > res ? ret : res;

				s[i][j] = c;

			}

		}

	}

	printf("%d\n", ret + bs);

	return 0;

}
// Slower C++ solution:
#include <vector>

#include <iostream>



using namespace std;



int main()

{

	int n, p;



	while (true)

	{

		cin >> n >> p;



		if (n == 0 && p == 0) { break; }



		int c = 0, r = p;



		vector<int> d(n, 0);



		while (true)

		{

			if (p != 0)

			{

				d[c]++; p--;

			}

			else

			{

				p = d[c]; d[c] = 0;

			}



			bool ok = false;



			if (p == 0)

			{

				ok = true;



				for (int i = 0; i < n; i++)

				{

					if (i != c && d[i] != 0)

					{

						ok = false;

					}

				}

			}



			if (ok)

			{

				break;

			}



			c = (c + 1) % n;

		}



		cout << c << endl;

	}

}

// Faster C++ solution for the same problem (measured speedup: 1.5795395567189625x):
#include <vector>

#include <iostream>

using namespace std;

int n, m;

int main() {

	while (cin >> n >> m, n) {

		int s = m;

		vector<int> v(n, 0);

		for (int i = 0; ; i = (i + 1) % n) {

			if (m != 0) v[i]++, m--;

			else m = v[i], v[i] = 0;

			if (m == 0 && s - m - v[i] == 0) {

				cout << i << endl;

				break;

			}

		}

	}

	return 0;

}
// Slower C++ solution:
#include <vector>

#include <iostream>



using namespace std;



int main()

{

	int n, p;



	while (true)

	{

		cin >> n >> p;



		if (n == 0 && p == 0) { break; }



		int c = 0, r = p;



		vector<int> d(n, 0);



		while (true)

		{

			if (p != 0)

			{

				d[c]++; p--;

			}

			else

			{

				p = d[c]; d[c] = 0;

			}



			bool ok = false;



			if (p == 0)

			{

				ok = true;



				for (int i = 0; i < n; i++)

				{

					if (i != c && d[i] != 0)

					{

						ok = false;

					}

				}

			}



			if (ok)

			{

				break;

			}



			c = (c + 1) % n;

		}



		cout << c << endl;

	}

}

// Faster C++ solution for the same problem (measured speedup: 2.702311702154163x):
#include <vector>

#include <iostream>

using namespace std;

int n, m;

int main() {

	while (cin >> n >> m, n) {

		int s = m;

		vector<int> v(n, 0);

		for (int i = 0; ; i = (i + 1 == n ? 0 : i + 1)) {

			if (m != 0) v[i]++, m--;

			else m = v[i], v[i] = 0;

			if (m == 0 && s - m - v[i] == 0) {

				cout << i << endl;

				break;

			}

		}

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <vector>

#include <string>



using namespace std;



int main() {

	int n,p;

	while (cin >> n >> p && n != 0 && p != 0) {

		vector<int> a(n, 0);

		int flag = n;

		int winner;



		int i = 0;

		while (1) {

			if (p != 0) {

				if (a[i % n] == 0) flag--;

				a[i % n] += 1;

				p--;	

			} else if (a[i % n] != 0) {

				flag++;

				p += a[i % n];

				a[i % n] = 0;

			}

			if (p == 0 && flag == n - 1) {

				winner = i % n;

				break;

			}



			i++;

		}

		cout << winner << endl;

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.4373476066724544x):
#include <iostream>

#include <vector>



using namespace std;



int main() {

	int n, p;

	while (cin >> n >> p && (n || p)) {

		vector<int> a(n, 0);

		int i = 0, num = p;

		while (1) {

			if (p != 0) {

				a[i]++;

				p--;

				if (a[i] == num) {

					cout << i << endl;

					break;

				}

			} else if (a[i] != 0) {

				p = a[i];

				a[i] = 0;

			}

			i++;

			if (i >= n) i = 0;

		}

	}

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <vector>

#include <string>

using namespace std;



bool allZero(vector<int> s, int cur) {

	for (int i = 0; i < s.size(); i++) {

		if (s[i] > 0 && i != cur) return false;

	}

	

	return true;

}

int main()

{

	// cinはscanfより基本的に遅い

	// sync_with_studio()を事前に書いておくとcinでも速度向上が期待出来る！



	bool flg = false;

	int n, p, cur;

	int curP;

	vector<int> s; 

	

	while (cin >> n >> p) {

		if (n == 0 && p == 0) {

			break;

		}



		

		cur = 0;

		curP = p;

		s.clear();

		s.resize(n);



		// 現在の候補者が碗から石を取り出す

		// 番号が降ってあるわけではない！

		while (true) {

			// 現在ターンである候補者が1つ取り出す

			if (curP > 0) {

				s[cur]++;

				curP--;

			} else if (curP == 0 && s[cur] > 0) {// 手元に小石があれば全て碗の中に入れる

				curP += s[cur];

				s[cur] = 0;

			}

			

			// 条件を満たして入れば終了

			if (curP == 0) {

				// && allZero(s, cur)) {

				flg = true;

				for (int i = 0; i < s.size(); i++) {

					if (s[i] > 0 && i != cur) flg = false;// 条件満たさず

				}

				if (flg) {

					cout << cur << endl;

					break;

				}

				

			}



			// 次の候補者へ

			cur++;

			if (cur == n) cur = 0;

		}

	

		

	}



	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.453029588566731x):
#include <iostream>

#include <algorithm>

#include <vector>

#include <string>

using namespace std;



int main()

{

	// cinはscanfより基本的に遅い

	// sync_with_studio()を事前に書いておくとcinでも速度向上が期待出来る！



	bool flg = false;

	int n, p, cur;

	int curP;

	vector<int> s; 

	

	while (cin >> n >> p) {

		if (n == 0 && p == 0) {

			break;

		}

		// 初期化処理

		cur = 0;

		curP = p;

		s.clear();

		s.resize(n);



		// 現在の候補者が碗から石を取り出す

		// 番号が降ってあるわけではない！

		while (true) {

			// 現在ターンである候補者が1つ取り出す

			if (curP > 0) {

				s[cur]++;

				curP--;

			} else if (curP == 0 && s[cur] > 0) {// 手元に小石があれば全て碗の中に入れる

				curP += s[cur];

				s[cur] = 0;

			}

			

			// 条件を満たして入れば終了

			if (curP == 0) {

				flg = true;

				for (int i = 0; i < s.size(); i++) {

					if (s[i] > 0 && i != cur) {

						flg = false;// 条件満たさず

						break;

					}

				}

				if (flg) {

					cout << cur << endl;

					break;

				}

			}



			// 次の候補者へ

			cur++;

			if (cur == n) cur = 0;

		}

	}



	return 0;

}
// Slower C++ solution:
/*

                   _ooOoo_

                  o8888888o

                  88" . "88

                  (| -_- |)

                  O\  =  /O

               ____/`---'\____

             .'  \\|     |//  `.

            /  \\|||  :  |||//  \

           /  _||||| -:- |||||-  \

           |   | \\\  -  /// |   |

           | \_|  ''\---/''  |   |

           \  .-\__  `-`  ___/-. /

         ___`. .'  /--.--\  `. . __

      ."" '<  `.___\_<|>_/___.'  >'"".

     | | :  `- \`.;`\ _ /`;.`/ - ` : | |

     \  \ `-.   \_ __\ /__ _/   .-` /  /

======`-.____`-.___\_____/___.-`____.-'======

                   `=---='

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

*/

#include<bits/stdc++.h>

#define int long long

using namespace std;

string s,t;

int sum,j,x,k,n,m; 

vector<int>mapp[30] ;

signed main ()

{

    cin>>s>>t;

	n=s.length(),m=t.length();

    for(int i=0;i<s.size();i++)

        mapp[s[i]-'a'].push_back(i+1);

    if(mapp[t[0]-'a'].size()==0)

	{

        puts("-1") ;

        return 0 ;

    }

    k=mapp[t[0]-'a'][0];

    sum+=k;

    for(int i=1;i<m;i++)

	{

        x=t[i]-'a' ;

        if(!mapp[x].size())

		{

            puts("-1") ;

            return 0 ;

        }

        j=upper_bound(mapp[x].begin(),mapp[x].end(),k)-mapp[x].begin() ;

        if(j==mapp[x].end()-mapp[x].begin())//没找到,再来一个 

            sum+=mapp[x][0]+n-k,k=mapp[x][0];

		else //找到了 

            sum+=mapp[x][j]-k,k=mapp[x][j];

    }

    cout<<sum<<endl; 

}

// Faster C++ solution for the same problem (measured speedup: 5.3118393008174065x):
#include<cstdio>

#include<cstring>

using namespace std;

char s[100005];

char t[100005];

int ind[100005][27];

bool vis1[27],vis2[27];

int main(){

	scanf("%s",s+1);

	scanf("%s",t+1);

	int lens=strlen(s+1);

	int lent=strlen(t+1);

	for(int i=1;i<=lens;i++)

		vis1[s[i]-'a']=1;

	for(int i=1;i<=lent;i++)

		vis2[t[i]-'a']=1;

	for(int i=0;i<26;i++)

		if(vis2[i]&&(!vis1[i])){

			printf("-1\n");

			return 0;

		}

	

	for(int i=lens;i>=1;i--){

		for(int j=0;j<26;j++)

			ind[i][j]=ind[i+1][j];

		ind[i][s[i]-'a']=i;

	}

	int num=0,now=1;

	for(int i=1;i<=lent;i++){

		if(now>lens||!ind[now][t[i]-'a']){

			now=1;

			num++;

		}

		now=ind[now][t[i]-'a']+1;

	}

	now--;

	printf("%lld\n",(long long)num*(long long)lens+(long long)now);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

const int N = 1e5 + 3;

vector<int> occ[26];

char s[N], t[N];

int main() {

#ifndef ONLINE_JUDGE

//	freopen("input.in", "r", stdin);

//	freopen("output.out", "w", stdout);

#endif

	scanf("%s %s", s, t);

	for (int i = 0; s[i]; ++i)

		occ[s[i] - 'a'].push_back(i + 1);

	int cycles = 0, idx = 0;

	for (int i = 0; t[i]; ++i) {

		if (occ[t[i] - 'a'].empty()) {

			cycles = 0;

			idx = -1;

			break;

		}

		auto it = upper_bound(occ[t[i] - 'a'].begin(), occ[t[i] - 'a'].end(), idx);

		if (it == occ[t[i] - 'a'].end()) {

			idx = *occ[t[i] - 'a'].begin();

			++cycles;

		} else {

			idx = *it;

		}

	}

	printf("%lld\n", 1LL * cycles * strlen(s) + idx);

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.326541398394178x):
#include<cstdio>

#include<cstring>

using namespace std;

char s[100005];

char t[100005];

int ind[100005][27];

bool vis1[27],vis2[27];

int main(){

	scanf("%s",s+1);

	scanf("%s",t+1);

	int lens=strlen(s+1);

	int lent=strlen(t+1);

	for(int i=1;i<=lens;i++)

		vis1[s[i]-'a']=1;

	for(int i=1;i<=lent;i++)

		vis2[t[i]-'a']=1;

	for(int i=0;i<26;i++)

		if(vis2[i]&&(!vis1[i])){

			printf("-1\n");

			return 0;

		}

	

	for(int i=lens;i>=1;i--){

		for(int j=0;j<26;j++)

			ind[i][j]=ind[i+1][j];

		ind[i][s[i]-'a']=i;

	}

	int num=0,now=1;

	for(int i=1;i<=lent;i++){

		if(now>lens||!ind[now][t[i]-'a']){

			now=1;

			num++;

		}

		now=ind[now][t[i]-'a']+1;

	}

	now--;

	printf("%lld\n",(long long)num*(long long)lens+(long long)now);

	return 0;

}
// Slower C++ solution:
/**

*   @Author : MeGA

*   @InitTime :01/24/20 13:35:09

*   @Idea : 

*

*

*

*

*

*

*

**/

#include <bits/stdc++.h>

using namespace std;  

int sz[26];

vector<int>pos[26];

int main(){

#ifndef ONLINE_JUDGE

  // freopen("in","r",stdin);

  // freopen("out","w",stdout);

#endif

     string str;

     cin>>str;

          

     for(int i=0;i<str.size();i++){

        pos[str[i]-'a'].push_back(i+1);

     }



     string t;

     cin>>t;

     long long ans=0;

     int prev=-1;

     for(char c:t){

        if(pos[c-'a'].size()==0){

           printf("-1");

           return 0;

        }else{

           int cur_idx=upper_bound(pos[c-'a'].begin(),pos[c-'a'].end(),prev)-pos[c-'a'].begin();

           if(cur_idx==pos[c-'a'].size()){

             cur_idx=pos[c-'a'][0];

           }else{

              cur_idx=pos[c-'a'][cur_idx];

           }

           if(prev==-1)prev=0;

        //   cerr<<cur_idx<<endl;

           if(cur_idx <= prev){

     //     cerr<<str.size()<<" "<<prev<<" ho"<<endl;

              ans+=str.size()-prev+cur_idx;

           }else{

       //    cerr<<cur_idx<<" "<<prev<<" hoi"<<endl;

             ans+=(cur_idx-prev);

           }

           prev=cur_idx;

         }

     }



     printf("%lld",ans);





}

// Faster C++ solution for the same problem (measured speedup: 5.353376495165874x):
#include<cstdio>

#include<cstring>

using namespace std;

char s[100005];

char t[100005];

int ind[100005][27];

bool vis1[27],vis2[27];

int main(){

	scanf("%s",s+1);

	scanf("%s",t+1);

	int lens=strlen(s+1);

	int lent=strlen(t+1);

	for(int i=1;i<=lens;i++)

		vis1[s[i]-'a']=1;

	for(int i=1;i<=lent;i++)

		vis2[t[i]-'a']=1;

	for(int i=0;i<26;i++)

		if(vis2[i]&&(!vis1[i])){

			printf("-1\n");

			return 0;

		}

	

	for(int i=lens;i>=1;i--){

		for(int j=0;j<26;j++)

			ind[i][j]=ind[i+1][j];

		ind[i][s[i]-'a']=i;

	}

	int num=0,now=1;

	for(int i=1;i<=lent;i++){

		if(now>lens||!ind[now][t[i]-'a']){

			now=1;

			num++;

		}

		now=ind[now][t[i]-'a']+1;

	}

	now--;

	printf("%lld\n",(long long)num*(long long)lens+(long long)now);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



const int N=1e5+10;



char ch[N];

int a[N<<1],b[N],vis[N];

int f[N<<1][26];



int main() {

	scanf("%s",ch+1);

	int n=strlen(ch+1);

	for(int i=1;i<=n;i++) a[i]=ch[i]-'a';

	for(int i=1;i<=n;i++) a[i+n]=a[i];

	scanf("%s",ch+1);

	int m=strlen(ch+1);

	for(int i=1;i<=m;i++) b[i]=ch[i]-'a';

	for(int i=1;i<=n;i++) vis[a[i]]=1;

	int flag=0;

	for(int i=1;i<=m;i++) if(!vis[b[i]]) flag=1;

	if(flag) {

		puts("-1");

		return 0;

	}

	for(int i=2*n;i;i--) {

		for(int j=0;j<26;j++) f[i][j]=f[i+1][j];

		f[i][a[i]]=i;

	}

	int cnt=0,now=0;

	for(int i=1;i<=m;i++) {

		now++;

		while(now>n) cnt++,now-=n;

		now=f[now][b[i]];

	}

	cout<<1ll*cnt*n+now<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 5.302474799226267x):
#include<cstdio>

#include<cstring>

using namespace std;

char s[100005];

char t[100005];

int ind[100005][27];

bool vis1[27],vis2[27];

int main(){

	scanf("%s",s+1);

	scanf("%s",t+1);

	int lens=strlen(s+1);

	int lent=strlen(t+1);

	for(int i=1;i<=lens;i++)

		vis1[s[i]-'a']=1;

	for(int i=1;i<=lent;i++)

		vis2[t[i]-'a']=1;

	for(int i=0;i<26;i++)

		if(vis2[i]&&(!vis1[i])){

			printf("-1\n");

			return 0;

		}

	

	for(int i=lens;i>=1;i--){

		for(int j=0;j<26;j++)

			ind[i][j]=ind[i+1][j];

		ind[i][s[i]-'a']=i;

	}

	int num=0,now=1;

	for(int i=1;i<=lent;i++){

		if(now>lens||!ind[now][t[i]-'a']){

			now=1;

			num++;

		}

		now=ind[now][t[i]-'a']+1;

	}

	now--;

	printf("%lld\n",(long long)num*(long long)lens+(long long)now);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



const int N = 1e5 + 5;

int nxt[26][N];



int main() {

    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);



    string s , t;

    cin >> s >> t;



    for (char &c : s) c -= 'a';

    for (char &c : t) c -= 'a';



    memset(nxt , -1 , sizeof nxt);

    for (int i = s.size() - 1 ;i >= 0 ;i--) {

        for (int j = 0 ;j < 26 ;j++)

            nxt[j][i] = nxt[j][i + 1];

        nxt[s[i]][i] = i;

    }



    for (char &c : t) {

        if (nxt[c][0] == -1) {

            cout << -1;

            return 0;

        }

    }



    int p = s.size() - 1;

    long long len = 0;

    for (char &c : t) {

        if (nxt[c][p + 1] != -1) {

            len += nxt[c][p + 1] - p;

            p = nxt[c][p + 1];

        } else {

            len += s.size() - p - 1;

            p = nxt[c][0];

            len += p + 1;

        }

    }



    cout << len;

}

// Faster C++ solution for the same problem (measured speedup: 11.738198032658165x):
#include <iostream>

#include <cstring>

#include <algorithm>

#include <vector>

#include <string>

using namespace std;

using ll=long long;

const int MAXN=100010;





vector<int> pos[30];

int main() {

    ios::sync_with_stdio(false);

    int res=0;



    string s,t;

    cin>>s>>t;

    for(int i=0;i<s.size();i++){

        pos[s[i]-'a'].push_back(i);

    }

    ll rev=0;

    int lastpos=-1;

    bool ok=1;

    for(int i=0;i<t.size();i++){

        auto it=lower_bound(pos[t[i]-'a'].begin(),pos[t[i]-'a'].end(),lastpos);

        if(it!=pos[t[i]-'a'].end()){

            lastpos=*it+1;

        }else{

            if(pos[t[i]-'a'].size()==0){

                ok=0;

                break;

            }

            rev++;

            lastpos=-1;

            i--;

        }

    }

    if(ok) {

        cout << (ll)rev * s.size() + (ll)lastpos << endl;

    }else{

        cout<<-1<<endl;

    }







    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



const int N = 1e5 + 5;

int nxt[26][N];



int main() {

    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);



    string s , t;

    cin >> s >> t;



    for (char &c : s) c -= 'a';

    for (char &c : t) c -= 'a';



    memset(nxt , -1 , sizeof nxt);

    for (int i = s.size() - 1 ;i >= 0 ;i--) {

        for (int j = 0 ;j < 26 ;j++)

            nxt[j][i] = nxt[j][i + 1];

        nxt[s[i]][i] = i;

    }



    for (char &c : t) {

        if (nxt[c][0] == -1) {

            cout << -1;

            return 0;

        }

    }



    int p = s.size() - 1;

    long long len = 0;

    for (char &c : t) {

        if (nxt[c][p + 1] != -1) {

            len += nxt[c][p + 1] - p;

            p = nxt[c][p + 1];

        } else {

            len += s.size() - p - 1;

            p = nxt[c][0];

            len += p + 1;

        }

    }



    cout << len;

}

// Faster C++ solution for the same problem (measured speedup: 11.970812811456303x):
#include<bits/stdc++.h>

using namespace std;

#define ll long long

vector<int>sc[27];

int main(){

	string s,t;

	cin>>s>>t;

	for(int i=0;i<s.size();i++){

		sc[s[i]-'a'].push_back(i);

	}

	int np = -1;

	ll ans = 0;

	for(int i=0;i<t.size();){

		if(sc[t[i]-'a'].size()==0)return 0*printf("-1\n");

		int pos = upper_bound(sc[t[i]-'a'].begin(),sc[t[i]-'a'].end(),np)-sc[t[i]-'a'].begin();

		if(pos==sc[t[i]-'a'].size()){

			np=-1;

			ans=ans+s.size();

		}

		else np=sc[t[i]-'a'][pos],i++;

	}

	printf("%lld\n",ans+np+1);

}
// Slower C++ solution:
#include <bits/stdc++.h>



#define F first

#define S second

#define PB push_back



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;

typedef priority_queue<int> HEAP;

typedef priority_queue<int, vector<int>, greater<int> > RHEAP;



const int N = 100010, M = 26;



int n, m;

int f[N][M];

char s[N], t[N];



int main()

{

	scanf("%s%s", s, t);

	n = strlen(s), m = strlen(t);

	

	memset(f, -1, sizeof f);

	for (int i = n - 1; i >= 0; i -- )

	{

		int c = s[i] - 'a';

		for (int j = 0; j < M; j ++ ) f[i][j] = f[i + 1][j];

		f[i][c] = i;

	}

	

	int a = 0, b = 0;

	for (int i = 0; i < m; i ++ )

	{

		int c = t[i] - 'a';

		if (f[0][c] == -1)

		{

			puts("-1");

			exit(0);

		}

		if (f[b][c] == -1) a ++ , b = 0;

		b = f[b][c] + 1;

	}

	printf("%lld\n", (LL)a * n + (LL)b);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 12.161569412244804x):
#include <bits/stdc++.h>



#define F first

#define S second

#define PB push_back



using namespace std;



typedef long long LL;

typedef pair<int, int> PII;

typedef priority_queue<int> HEAP;

typedef priority_queue<int, vector<int>, greater<int> > RHEAP;



const int N = 100010, M = 26;



int n, m;

int f[N][M];

char s[N], t[N];



int main()

{

	scanf("%s%s", s, t);

	n = strlen(s), m = strlen(t);

	

	for (int i = 0; i < M; i ++ ) f[n][i] = n;

	for (int i = n - 1; i >= 0; i -- )

	{

		for (int j = 0; j < M; j ++ ) f[i][j] = f[i + 1][j];

		f[i][s[i] - 'a'] = i;

	}

	

	int a = 0, b = 0;

	for (int i = 0; i < m; i ++ )

	{

		int c = t[i] - 'a';

		if (f[0][c] == n)

		{

			puts("-1");

			return 0;

		}

		

		if (f[b][c] >= n) a ++ , b = 0;

		b = f[b][c] + 1;

	}

	printf("%lld\n", (LL)a * n + (LL)b);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)

#define chmax(a, b) a = max(a, b)

#define chmin(a, b) a = min(a, b)

#define fi first

#define se second

#define INF 1000000009

#define LLINF 1000000000000000009LL

using ll = long long;

#define MX 100009



int mp[MX][29];



int main(){

  string s,t;

  cin>>s>>t;

  int n=s.size();

  int m=t.size();

  rep(i,MX)rep(j,29)mp[i][j]=-1;

  for(int i=n-1;i>=0;i--){

    rep(j,26){

      mp[i][j]=mp[i+1][j];

    }

    mp[i][s[i]-'a']=i;

  }

  ll ans=0;

  int p=-1;

  rep(i,m){

    int x=mp[p+1][t[i]-'a'];

    int nxt=mp[0][t[i]-'a'];

    if(p<x){

      ans+=x-p;

      p=x;

    }

    else{

      if(nxt==-1){

        ans=-1;

        break;

      }

      ans+=n-p+nxt;

      p=nxt;

    }

  }

  cout<<ans<<endl;

  return(0);

}


// Faster C++ solution for the same problem (measured speedup: 13.146282712112095x):
#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)

#define chmax(a, b) a = max(a, b)

#define chmin(a, b) a = min(a, b)

#define fi first

#define se second

#define INF 1000000009

#define LLINF 1000000000000000009LL

using ll = long long;



int main() {

  string s, t;

  cin >> s >> t;

  int n = s.size(), m = t.size();

  vector<vector<int>> is(26);

  rep(i,n) is[s[i]-'a'].push_back(i);

  rep(i,n) is[s[i]-'a'].push_back(i+n);

  ll ans = 0;

  int p = 0;

  rep(i,m) {

    int c = t[i] - 'a';

    if (is[c].size() == 0) {

      cout << -1 << endl;

      return(0);

    }

    p = *lower_bound(is[c].begin(), is[c].end(), p) + 1;

    if (p >= n) {

      p -= n;

      ans += n;

    }

  }

  ans += p;

  cout << ans << endl;

  return(0);

}