stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):


#include <bits/stdc++.h>



using namespace std;



#define lli long long int

#define REP(i,s,l) for(lli i=s;i<l;i++)

#define DEBUG 0

#define INF (1LL<<50)

#define MOD 1000000007



signed main(){



	lli n,m;

	cin>>n>>m;



	/*言語を話せる人たちをまとめる*/

	map<lli,vector<lli>> l;



	/*また、j番目の人が話せる言語もまとめる*/

	map<lli,vector<lli>> p;



	for(lli i=0;i<n;i++){

		lli k;

		cin>>k;

		REP(j,0,k){

			lli tmp;

			cin>>tmp;

			l[tmp].push_back(i);

			p[i].push_back(tmp);

		}

	}



	lli lflag[100100]={0};

	lli pflag[100100]={0};



	/*0番目の人が話せる言語にアクセスする*/

	pflag[0]=1;

	queue<lli> q;

	q.push(0);



	while(q.size()){

		lli top = q.front();

		q.pop();

		if(DEBUG)cout<<"topPerson="<<top<<endl;

		/*この人は何の言語を話せる?*/

		for(auto e:p[top]){

			if(DEBUG)cout<<"Lang="<<e<<endl;

			if(lflag[e]==0){

				lflag[e]=1;

				/*この言語を話せる人をqにつっこみまくる*/

				for(auto f:l[e]){

					if(DEBUG)cout<<"nextPerson="<<f<<endl;

					if(pflag[f]==0){

						if(DEBUG)cout<<"push!"<<f<<endl;

						pflag[f]=1;

						q.push(f);

					}

				}

			}

		}

	}



	lli cnt=0;

	for(lli i=0;i<100100;i++){

		if(pflag[i])cnt++;

	}

	if(DEBUG)cout<<"cnt="<<cnt<<endl;

	if(cnt==n)cout<<"YES"<<endl;

	else cout<<"NO"<<endl;



	return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

 

using namespace std;

 

#define lli long long int

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

#define MOD 1000000007

#define NUM 2520

#define DEBUG 0

#define mp(a,b) make_pair(a,b)

#define SORT(V) sort(V.begin(),V.end())

#define PI (3.141592653589794)

#define INF (1LL<<50)





typedef struct UnionFind{

	vector<lli> par; //par[i]:iの親番号 par[3] = 2 : 3の親が2

	vector<lli> size;//size[i]:iの大きさ 自分のみのときは1,根っこにsizeを持たせる*/



	UnionFind(int N){/*最初はすべてが根であると初期化*/

		REP(i,0,N){

			par.push_back(i);

			size.push_back(1);

		}

	}



	lli root(lli x){/*データxが属する木の根を再起で得る root(x) = {xの木の根}}*/

		if(par[x] == x)return x;

		return par[x] = root(par[x]);

	}



	void unite(lli x,lli y){/*xとyの併合*/

		lli rx = root(x);

		lli ry = root(y);

		if(rx == ry)return;

		par[rx] = ry; /*xとyの根が同じじゃない: xの根をrxをyの根ryに変更する */

		lli resize = size[rx] + size[ry];

		size[rx] = resize;

		size[ry] = resize;

	}



	bool same(lli x,lli y){

		lli rx = root(x);

		lli ry = root(y);

		return rx == ry;

	}



	lli getSize(lli x){

		lli rx = root(x);

		return size[rx];

	}

};





signed main(){



	lli n,m;

	cin>>n>>m;



	UnionFind uf(n);



	map<lli,vector<lli>> lang;

	REP(i,0,n){

		lli k;

		cin>>k;

		REP(j,0,k){

			lli now;

			cin>>now;

			lang[now].push_back(i);

		}

	}



	for(auto e:lang){

		REP(i,1,e.second.size()){

			uf.unite(e.second.at(i-1),e.second.at(i));

		}

	}



	if(DEBUG)cout<<"ufSize="<<uf.getSize(0)<<endl;

	if(uf.getSize(0)==n){

		cout<<"YES"<<endl;

	}

	else{

		cout<<"NO"<<endl;

	}







	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

	}

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

                   _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; 

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/**

*   @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);





}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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);

}


// Optimized solution to the same problem (faster):
#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);

}


// Fastest known solution for this problem: