#include<bits/stdc++.h>
using namespace std;
long long MOD=1000000009;
const int MAXN = 151;
typedef long long ll;
struct matrix{
    int n;
    bitset < MAXN > A[MAXN] , B[MAXN];
    matrix(){}
    matrix(int x){
        n = x;
        for(int j = 1 ; j <= n ; j++)
            A[j].reset() , B[j].reset();
    }
    matrix operator * (matrix &mat){
        matrix res(n);
        for(int j = 1 ; j <= n ; j++)
            for(int i = 1 ; i <= n ; i++){
                bitset < MAXN > jd3 = (A[j]&mat.B[i]);
                if(jd3.any()) res.A[j][i] = res.B[i][j] = 1;
            }
        return res;
    }
    matrix power(long long K){
        matrix res(n) , b(n);
        for(int j=0;j<n;j++) res.A[j][j] = res.B[j][j] = 1;
        b = (*this);
        while(K>0){
            if( (K&1) )
                res = res * b;
            K/=2;
            b = b * b;
        }
        return res;
    }
};
const int MX = 160 , inf = (1<<30) - 1;
int lim[MX] , uu[MX] , vv[MX];
bool cmp(int a , int b){
    return lim[a] < lim[b];
}
int n , floyd[MX][MX];
void init(){
    for(int a = 1 ; a <= n ; a++){
        for(int b = 1 ; b <= n ; b++)
            floyd[a][b] = inf;
        floyd[a][a] = 0;
    }
}
void go(){
      for(int a = 1 ; a <= n ; a++)
            for(int b = 1 ; b <= n ; b++)
                for(int c = 1 ; c <= n ; c++)
                    floyd[b][c] = min(floyd[b][c] , floyd[b][a] + floyd[a][c]);
}
int m;
int main(){
    cin>>n>>m;
    vector < int > sorted;
    for(int j =1 ; j <= m ;j++){
        cin>>uu[j]>>vv[j]>>lim[j];
        sorted.push_back(j);
    }
    for(int j = 1 ; j <= m ; j++)
        for(int i = j + 1 ; i <= m ; i++)
            if(lim[j] > lim[i])
                swap(lim[j] , lim[i]) , swap(uu[j] , uu[i]) , swap(vv[j] , vv[i]);
    if(lim[1] != 0){
        puts("Impossible");
        return 0;
    }
    int it1 = 1;
    init();
    while(lim[it1] == 0){
        floyd[uu[it1]][vv[it1]] = 1;
        ++it1;
    }
    go();
    int ans = inf;
    ans = min(ans , floyd[1][n]);
    matrix MAT = matrix(n);
    for(int j = 1 ; j <= n ; j++) MAT.A[j][j] = MAT.B[j][j] = 1;
    //cout<<it1<<endl;
    for(it1 ; it1 <= m ;){
        //cout<<"#step "<<it1<<endl;
        matrix MUL = matrix(n);
        for(int j = 1 ; j < it1 ; j++){
            MUL.A[uu[j]][vv[j]] = 1;
            MUL.B[vv[j]][uu[j]] = 1;
           // cout<<uu[j]<<' '<<vv[j]<<endl;
        }
        int K = lim[it1] - lim[it1 - 1];
        MUL = MUL.power(K);
        MAT = MAT * MUL;
        int it2 = it1;
        while(lim[it2+1] == lim[it1]) ++it2;
        init();
        for(int j = 1 ; j <= it2 ; j++)
            floyd[uu[j]][vv[j]] = 1;
        go();
        for(int a = 1 ; a <= n ; a++){
            if(MAT.A[1][a]){
                ans = min(ans , lim[it1] + floyd[a][n]);
                //cout<<lim[it1]<<' '<<a<<' '<<floyd[a][n]<<endl;
               // cout<<it1<<' '<<1<<' '<<a<<endl;
            }
        }
        it1 = it2 + 1;

    }
    if(ans == inf) puts("Impossible");
    else cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
long long MOD=1000000009;
const int MAXN = 151;
typedef long long ll;
struct matrix{
    int n;
    bitset < MAXN > A[MAXN] , B[MAXN];
    matrix(){}
    matrix(int x){
        n = x;
        for(int j = 1 ; j <= n ; j++)
            A[j].reset() , B[j].reset();
    }
    matrix operator * (matrix &mat){
        matrix res(n);
        for(int j = 1 ; j <= n ; j++)
            for(int i = 1 ; i <= n ; i++){
                bitset < MAXN > jd3 = (A[j]&mat.B[i]);
                if(jd3.any()) res.A[j][i] = res.B[i][j] = 1;
            }
        return res;
    }
    matrix power(long long K){
        matrix res(n) , b(n);
        for(int j=0;j<n;j++) res.A[j][j] = res.B[j][j] = 1;
        b = (*this);
        while(K>0){
            if( (K&1) )
                res = res * b;
            K/=2;
            b = b * b;
        }
        return res;
    }
};
const int MX = 160 , inf = (1<<30) - 1;
int lim[MX] , uu[MX] , vv[MX];
bool cmp(int a , int b){
    return lim[a] < lim[b];
}
int n , floyd[MX][MX];
void init(){
    for(int a = 1 ; a <= n ; a++){
        for(int b = 1 ; b <= n ; b++)
            floyd[a][b] = inf;
        floyd[a][a] = 0;
    }
}
void go(){
      for(int a = 1 ; a <= n ; a++)
            for(int b = 1 ; b <= n ; b++)
                for(int c = 1 ; c <= n ; c++)
                    floyd[b][c] = min(floyd[b][c] , floyd[b][a] + floyd[a][c]);
}
int m;
int main(){
    cin>>n>>m;
    vector < int > sorted;
    for(int j =1 ; j <= m ;j++){
        cin>>uu[j]>>vv[j]>>lim[j];
        sorted.push_back(j);
    }
    for(int j = 1 ; j <= m ; j++)
        for(int i = j + 1 ; i <= m ; i++)
            if(lim[j] > lim[i])
                swap(lim[j] , lim[i]) , swap(uu[j] , uu[i]) , swap(vv[j] , vv[i]);
    if(lim[1] != 0){
        puts("Impossible");
        return 0;
    }
    int it1 = 1;
    init();
    while(lim[it1] == 0){
        floyd[uu[it1]][vv[it1]] = 1;
        ++it1;
    }
    int ans = inf;
    ans = min(ans , floyd[1][n]);
    matrix MAT = matrix(n);
    for(int j = 1 ; j <= n ; j++) MAT.A[j][j] = MAT.B[j][j] = 1;
    //cout<<it1<<endl;
    for(it1 ; it1 <= m ;){
        //cout<<"#step "<<it1<<endl;
        matrix MUL = matrix(n);
        for(int j = 1 ; j < it1 ; j++){
            MUL.A[uu[j]][vv[j]] = 1;
            MUL.B[vv[j]][uu[j]] = 1;
           // cout<<uu[j]<<' '<<vv[j]<<endl;
        }
        int K = lim[it1] - lim[it1 - 1];
        MUL = MUL.power(K);
        MAT = MAT * MUL;
        int it2 = it1;
        while(lim[it2+1] == lim[it1]) ++it2;
        init();
        for(int j = 1 ; j <= it2 ; j++)
            floyd[uu[j]][vv[j]] = 1;
        go();
        for(int a = 1 ; a <= n ; a++){
            if(MAT.A[1][a]){
                ans = min(ans , lim[it1] + floyd[a][n]);
                //cout<<lim[it1]<<' '<<a<<' '<<floyd[a][n]<<endl;
               // cout<<it1<<' '<<1<<' '<<a<<endl;
            }
        }
        it1 = it2 + 1;

    }
    if(ans == inf) puts("Impossible");
    else cout<<ans<<endl;

}


#include<bits/stdc++.h>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int,int >
#define iiii pair<int,pii >
#define mp make_pair
#define INF 1000001000
#define MOD 1000000007
#define rep(i,x) for(int (i)=0;(i)<(x);(i)++)
inline int getint(){
	int x=0,p=1;char c=getchar();
	while (c<=32)c=getchar();
	if(c==45)p=-p,c=getchar();
	while (c>32)x=x*10+c-48,c=getchar();
	return x*p;
}
using namespace std;
//ruogu_alter
const int N=155;
int n,m,len[N][N];
vector<iiii >v;
vector<int>vt;
struct matrix{
	bitset<N>d[N],d2[N];
	inline void set(int x,int y){d[x][y]=d2[y][x]=true;}
	inline matrix operator *(const matrix &b)const{
		matrix ans;
		rep(i,n)rep(j,n)ans.d[i][j]=ans.d2[j][i]=(d[i]&b.d2[j]).count();
		return ans;
	}
	inline void p(){
		rep(i,n){rep(j,n)cout<<d[i][j]<<" ";cout<<endl;}
	}
	inline matrix pw(int x)const{
		matrix ans,y=(*this);
		rep(i,n)ans.set(i,i);
		while(x){
			if(x&1)ans=ans*y;
			y=y*y;
			x>>=1;
		}
		return ans;
	}
};
matrix res,g;
//
void floyd(){
	rep(k,n)rep(i,n)rep(j,n)len[i][j]=min(len[i][j],len[i][k]+len[k][j]);
}
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n=getint();m=getint();
	rep(i,m){
		int x=getint()-1,y=getint()-1,z=getint();
		v.push_back(mp(z,mp(x,y)));
	}
	sort(v.begin(),v.end());
	if(v[0].first)return !printf("Impossible\n");
	rep(i,m)vt.push_back(v[i].first);
	sort(vt.begin(),vt.end());
	vt.resize(unique(vt.begin(),vt.end())-vt.begin());
	rep(i,n)res.set(i,i);
	int p=0,ans=INF;
	rep(i,vt.size()){
		if(i)res=res*(g.pw(vt[i]-vt[i-1]));
		while(p<m&&v[p].first==vt[i]){
			g.set(v[p].second.first,v[p].second.second);
			++p;
		}
		rep(j,n)rep(k,n)len[j][k]=(g.d[j][k]?1:INF);
		floyd();
		rep(j,n)if(res.d[0][j])ans=min(ans,vt[i]+len[j][n-1]);
	}
	if(ans==INF)printf("Impossible\n");
	else printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int,int >
#define iiii pair<int,pii >
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i,x) for(int (i)=0;(i)<(x);(i)++)
inline int getint(){
	int x=0,p=1;char c=getchar();
	while (c<=32)c=getchar();
	if(c==45)p=-p,c=getchar();
	while (c>32)x=x*10+c-48,c=getchar();
	return x*p;
}
using namespace std;
//ruogu_alter
const int N=155;
int n,m;
vector<iiii >v;
vector<int>vt;
struct matrix{
	bitset<N>d[N],d2[N];
	inline void set(int x,int y){d[x][y]=d2[y][x]=true;}
	inline matrix operator *(const matrix &b)const{
		matrix ans;
		rep(i,n)rep(j,n)ans.d[i][j]=ans.d2[j][i]=(d[i]&b.d2[j]).count();
		return ans;
	}
	inline void p(){
		rep(i,n){rep(j,n)cout<<d[i][j]<<" ";cout<<endl;}
	}
	inline matrix pw(int x)const{
		matrix ans,y=(*this);
		rep(i,n)ans.set(i,i);
		while(x){
			if(x&1)ans=ans*y;
			y=y*y;
			x>>=1;
		}
		return ans;
	}
};
matrix res,g;
//
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n=getint();m=getint();
	rep(i,m){
		int x=getint()-1,y=getint()-1,z=getint();
		v.push_back(mp(z,mp(x,y)));
	}
	sort(v.begin(),v.end());
	if(v[0].first)return !printf("%d\n",-1);
	rep(i,m)rep(j,n)vt.push_back(v[i].first+j);
	sort(vt.begin(),vt.end());
	vt.resize(unique(vt.begin(),vt.end())-vt.begin());
	rep(i,n)res.set(i,i);
	int p=0;
	while(p<m&&!v[p].first)g.set(v[p].second.first,v[p].second.second),p++;
	res=g;
	if(res.d[0][n-1])return !printf("Impossible\n");
	for(int i=1;i<vt.size();i++){
		while(p<m&&v[p].first==vt[i]){
			g.set(v[p].second.first,v[p].second.second);
			++p;
		}
		res=res*(g.pw(vt[i]-vt[i-1]));
		if(res.d[0][n-1])return !printf("%d\n",vt[i]+1);
	}
	printf("Impossible\n");
	return 0;
}


#include<bits/stdc++.h>
#define ld double
#define ull unsigned long long
#define ll long long
#define pii pair<int,int >
#define iiii pair<int,pii >
#define mp make_pair
#define INF 1000000000
#define MOD 1000000007
#define rep(i,x) for(int (i)=0;(i)<(x);(i)++)
inline int getint(){
	int x=0,p=1;char c=getchar();
	while (c<=32)c=getchar();
	if(c==45)p=-p,c=getchar();
	while (c>32)x=x*10+c-48,c=getchar();
	return x*p;
}
using namespace std;
//ruogu_alter
const int N=155;
int n,m;
vector<iiii >v;
vector<int>vt;
struct matrix{
	bitset<N>d[N],d2[N];
	inline void set(int x,int y){d[x][y]=d2[y][x]=true;}
	inline matrix operator *(const matrix &b)const{
		matrix ans;
		rep(i,n)rep(j,n)ans.d[i][j]=ans.d2[j][i]=(d[i]&b.d2[j]).count();
		return ans;
	}
	inline void p(){
		rep(i,n){rep(j,n)cout<<d[i][j]<<" ";cout<<endl;}
	}
	inline matrix pw(int x)const{
		matrix ans,y=(*this);
		rep(i,n)ans.set(i,i);
		while(x){
			if(x&1)ans=ans*y;
			y=y*y;
			x>>=1;
		}
		return ans;
	}
};
matrix res,g;
//
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	n=getint();m=getint();
	rep(i,m){
		int x=getint()-1,y=getint()-1,z=getint();
		v.push_back(mp(z,mp(x,y)));
	}
	sort(v.begin(),v.end());
	if(v[0].first)return !printf("Impossible\n");
	rep(i,m)rep(j,n)vt.push_back(v[i].first+j);
	sort(vt.begin(),vt.end());
	vt.resize(unique(vt.begin(),vt.end())-vt.begin());
	rep(i,n)res.set(i,i);
	int p=0;
	while(p<m&&!v[p].first)g.set(v[p].second.first,v[p].second.second),p++;
	res=g;
	if(res.d[0][n-1])return !printf("1\n");
	for(int i=1;i<vt.size();i++){
		while(p<m&&v[p].first==vt[i]){
			g.set(v[p].second.first,v[p].second.second);
			++p;
		}
		res=res*(g.pw(vt[i]-vt[i-1]));
		if(res.d[0][n-1])return !printf("%d\n",vt[i]+1);
	}
	printf("Impossible\n");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cassert>
#include <map>
#include <set>
#include <stack>
#include <time.h>
#include <algorithm>
#include <vector>
#include <queue>
#include "cmath"
#include "iomanip"
#include <time.h>

#define pr pair <int, int>
#define llong long long int
#define pb push_back
#define lb lower_bound
#define mp make_pair
using namespace std;
const int MAXN = 2e5 + 7;
string st, st2;
int a[110];
bool b[110];

int ans;
int main () {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> st;
    for (int i = 0; i < st.size(); i++) {
        a[st[i] - 'a' + 1]++;
        b[st[i] - 'a' + 1] = true;
    }
    cin >> st2;

    for (int i = 0; i < st2.size(); i++) {
        if (a[st2[i] - 'a' + 1]) {
            ans++;
            a[st2[i] - 'a' + 1]--;
        } else if (!b[st2[i] - 'a' + 1]) {
            cout << -1;
            return 0;
        }
    }
    if (!ans)
        ans = -1;
    cout << ans;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define sz size()

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

int X[33], Y[33];

int main() {
	string a,b;
	cin >> a >> b;
	rep(i,0,a.sz) {
		X[a[i]-'a']++;
	}
	rep(i,0,b.sz) {
		Y[b[i]-'a']++;
	}

	int ans = 0;
	rep(i,0,26) {
		ans += min(X[i], Y[i]);
	}
	if(!ans) ans = -1;
	P(ans);

	return 0;
}


#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define sz size()

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

int X[33], Y[33];

int main() {
	string a,b;
	cin >> a >> b;
	rep(i,0,a.sz) {
		X[a[i]-'a']++;
	}
	rep(i,0,b.sz) {
		Y[b[i]-'a']++;
	}

	int ans = 0;
	bool flag = true;
	rep(i,0,26) {
		ans += min(X[i], Y[i]);
		if(Y[i] && !X[i]) flag = false;
	}
	if(!ans || !flag) ans = -1;
	P(ans);

	return 0;
}


#include <iostream>
#include <stdlib.h>
#include <limits>
#include <string>
#include <fstream>
#include <map>

using namespace std;

string first,second;

void read_input();
int work();
void show_output(int min);
void destructor();

int main()
{
	read_input();
	int S=work();
	show_output(S);
	destructor();
	return 0;
}

void read_input()
{
	getline(cin,first);
	getline(cin,second);
}

int work()
{
	int S=0;
	map<char,int> real,max;

	// ????????? ?????? max
	for(int i=0;i<first.length();i++)
	{
		char symbol=first[i];
		if(max.find(symbol)==max.end())
		{
			max[symbol]=1;
		}
		else
			max[symbol]++;
	}

	// ??????? ????? ????? ?????? ???? ? ????????
	string keys_real;

	for(int i=0;i<second.length();i++)
	{
		char symbol=second[i];
		if(real.find(symbol)==real.end())
		{
			real[symbol]=1;
			keys_real+=symbol;
		}
		else
			real[symbol]++;
	}

	// ??????????
	for(int i=0;i<keys_real.length();i++)
	{
		char symbol=keys_real[i];
		if(max.find(symbol)==max.end())
		{
			S=-1;
			break;
		}
		else
		{
			if(max[symbol]<real[symbol])
				S+=max[symbol];
			else
				S+=real[symbol];
		}
	}

	return S;
}

void show_output(int output_var)
{
	cout<<output_var;
}

void destructor()
{

}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 123;
const int MOD = 1000000007;

int main(){
//  freopen("in.txt", "r", stdin);
    string a, b;
    while(cin >> a >> b){
        int cnt1[26] = {0}, cnt2[26] = {0};
        FOR(i, a.size())
            cnt1[a[i]-'a']++;
        FOR(i, b.size())
            cnt2[b[i]-'a']++;
        int ans = 0;
        FOR(i, 26)
            ans += min(cnt1[i], cnt2[i]);
        if(!ans)
            ans = -1;
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 123;
const int MOD = 1000000007;

int main(){
//	freopen("in.txt", "r", stdin);
	string a, b;
	while(cin >> a >> b){
		vi arr1[26], arr2[26];
		int tmp = 0;
		FOR(i, a.size()){
			if(!i)
				tmp++;
			else{
				if(a[i-1] == a[i])
					tmp++;
				else{
					arr1[a[i-1]-'a'].push_back(tmp);
					tmp = 1;
				}
			}
		}
		arr1[a[a.size()-1]-'a'].push_back(tmp);
		tmp = 0;
		FOR(i, b.size()){
			if(!i)
				tmp++;
			else{
				if(b[i-1] == b[i])
					tmp++;
				else{
					arr2[b[i-1]-'a'].push_back(tmp);
					tmp = 1;
				}
			}
		}
		arr2[b[b.size()-1]-'a'].push_back(tmp);
		int ans = 0;
		FOR(i, 26){
			sort(all(arr1[i]));
			sort(all(arr2[i]));
			FOR(j, arr2[i].size())
				FOR(k, arr1[i].size()){
					if(arr2[i][j] <= arr1[i][k]){
						arr1[i][k] -= arr2[i][j];
						ans += arr2[i][j];
						if(!arr1[i][k])
							arr1[i].erase(arr1[i].begin()+k);
					}
					else{
						arr2[i][j] -= arr1[i][k];
						ans += arr1[i][k];
						arr1[i].erase(arr1[i].begin()+k);
						arr2[i].push_back(arr2[i][j]);
					}
			}
		}
		if(!ans)
			ans = -1;
		cout << ans << endl;
	}
	return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 123;
const int MOD = 1000000007;

int main(){
//  freopen("in.txt", "r", stdin);
    string a, b;
    while(cin >> a >> b){
        vi arr1[26], arr2[26];
        int tmp = 0;
        FOR(i, a.size()){
            if(!i)
                tmp++;
            else{
                if(a[i-1] == a[i])
                    tmp++;
                else{
                    arr1[a[i-1]-'a'].push_back(tmp);
                    tmp = 1;
                }
            }
        }
        arr1[a[a.size()-1]-'a'].push_back(tmp);
        tmp = 0;
        FOR(i, b.size()){
            if(!i)
                tmp++;
            else{
                if(b[i-1] == b[i])
                    tmp++;
                else{
                    arr2[b[i-1]-'a'].push_back(tmp);
                    tmp = 1;
                }
            }
        }
        arr2[b[b.size()-1]-'a'].push_back(tmp);
        int ans = 0;
        FOR(i, 26){
            sort(all(arr1[i]));
            sort(all(arr2[i]));
            FOR(j, arr2[i].size())
                FOR(k, arr1[i].size())
                if(arr2[i][j] <= arr1[i][k]){
                    arr1[i][k] -= arr2[i][j];
                    ans += arr2[i][j];
                    if(!arr1[i][k])
                        arr1[i].erase(arr1[i].begin()+k);
                    break;
                }
        }
        if(!ans)
            ans = -1;
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int M, N;
string s;

#define FOR(i, N) for(int i = 0; i < N; i++)
#define FOR1e(i, N) for(int i = 1; i <= N; i++)
#define REP(i, M, N) for(int i = M; i < N; i++)
#define REPe(i, M, N) for(int i = M; i <= N; i++)
#define sc(N) scanf("%d", &N)
#define scsc(M, N) scanf("%d %d", &M, &N)
#define gt(s) getline(cin, s)
#define all(s) s.begin(), s.end()
#define ll long long
#define ull unsigned ll
#define vi vector <int>
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define PI 3.14159265
#define EPS 1e-7

const int MAX = 123;
const int MOD = 1000000007;

int main(){
//  freopen("in.txt", "r", stdin);
    string a, b;
    while(cin >> a >> b){
        int cnt1[26] = {0}, cnt2[26] = {0};
        FOR(i, a.size())
            cnt1[a[i]-'a']++;
        FOR(i, b.size())
            cnt2[b[i]-'a']++;
        int ans = 0;
        FOR(i, 26){
            if(!cnt1[i] && cnt2[i]){
                ans = -1;
                break;
            }
            ans += min(cnt1[i], cnt2[i]);
        }
        cout << ans << endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
using namespace std;

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <iomanip>

#include <map>
#include <list>
#include <set>
#include <climits>
#include <queue>
#include <array>

//#include <conio.h>
//#include "Large Numbers.h"
//#include "Primes.h"

typedef unsigned long long int ull;

#define FOR(variable, condition)		for(int variable = 0; variable < condition; ++variable)
#define FORA(variable, Array)			for(auto &variable : Array)
#define SORT(Array)						sort(Array.begin(), Array.end())
#define REVERSE(Array)					reverse(Array.begin(), Array.end())

int main() {
	//ifstream cin; cin.open("input.txt");
	ios_base::sync_with_stdio(false);

	string n, m;
	getline(cin, n);
	getline(cin, m);

	SORT(n);
	SORT(m);

	int result = 0;

	vector<pair<char, int>> alpN, alpM;
	FOR(i, 26) {
		alpN.push_back(make_pair('a' + i, 0));
		alpM.push_back(make_pair('a' + i, 0));
	}

	FOR(i, 26) {
		FOR(j, n.size()) {
			if (n[j] == alpN[i].first) {
				alpN[i].second++;
			} else if (n[j] > alpN[i].first) {
				break;
			}
		}

		FOR(j, m.size()) {
			if (m[j] == alpM[i].first) {
				alpM[i].second++;
			} else if (m[j] > alpM[i].first) {
				break;
			}
		}
	}

	FOR(i, 26) {
		result += min(alpN[i].second, alpM[i].second);
	}

	cout << result << endl;
}


#define _CRT_SECURE_NO_WARNINGS
using namespace std;

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <iomanip>

#include <map>
#include <list>
#include <set>
#include <climits>
#include <queue>
#include <array>

//#include <conio.h>
//#include "Large Numbers.h"
//#include "Primes.h"

typedef unsigned long long int ull;

#define FOR(variable, condition)		for(int variable = 0; variable < condition; ++variable)
#define FORA(variable, Array)			for(auto &variable : Array)
#define SORT(Array)						sort(Array.begin(), Array.end())
#define REVERSE(Array)					reverse(Array.begin(), Array.end())

//int main() {
//	//ifstream cin; cin.open("input.txt");
//	ios_base::sync_with_stdio(false);
//
//
//}

int a[111], b[111], o[111];

int main() {
	ifstream cin; cin.open("input.txt");
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a[i] >> b[i];
		o[i] = 0;
	}
	int ans = n;
	for (int i = 0; i < n; ++i)
	if (o[i] == 0)
	for (int j = 0; j < n; ++j)
	if (i != j && b[j] == a[i]) {
		ans--;
		break;
	}

	cout << ans << "\n";
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
using namespace std;

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <iomanip>

#include <map>
#include <list>
#include <set>
#include <climits>
#include <queue>
#include <array>

//#include <conio.h>
//#include "Large Numbers.h"
//#include "Primes.h"

typedef unsigned long long int ull;

#define FOR(variable, condition)		for(int variable = 0; variable < condition; ++variable)
#define FORA(variable, Array)			for(auto &variable : Array)
#define SORT(Array)						sort(Array.begin(), Array.end())
#define REVERSE(Array)					reverse(Array.begin(), Array.end())

int main() {
	//ifstream cin; cin.open("input.txt");
	ios_base::sync_with_stdio(false);

	string n, m;
	getline(cin, n);
	getline(cin, m);

	SORT(n);
	SORT(m);

	int result = 0;

	vector<pair<char, int>> alpN, alpM;
	FOR(i, 26) {
		alpN.push_back(make_pair('a' + i, 0));
		alpM.push_back(make_pair('a' + i, 0));
	}

	FOR(i, 26) {
		FOR(j, n.size()) {
			if (n[j] == alpN[i].first) {
				alpN[i].second++;
			} else if (n[j] > alpN[i].first) {
				break;
			}
		}

		FOR(j, m.size()) {
			if (m[j] == alpM[i].first) {
				alpM[i].second++;
			} else if (m[j] > alpM[i].first) {
				break;
			}
		}
	}

	FOR(i, 26) {
		result += min(alpN[i].second, alpM[i].second);
	}

	if (result == 0) {
		cout << -1 << endl;
	} else {
		cout << result << endl;
	}
}


#define _CRT_SECURE_NO_WARNINGS
using namespace std;

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstdio>
#include <iomanip>

#include <map>
#include <list>
#include <set>
#include <climits>
#include <queue>
#include <array>

//#include <conio.h>
//#include "Large Numbers.h"
//#include "Primes.h"

typedef unsigned long long int ull;

#define FOR(variable, condition)		for(int variable = 0; variable < condition; ++variable)
#define FORA(variable, Array)			for(auto &variable : Array)
#define SORT(Array)						sort(Array.begin(), Array.end())
#define REVERSE(Array)					reverse(Array.begin(), Array.end())

int main() {
	//ifstream cin; cin.open("input.txt");
	ios_base::sync_with_stdio(false);

	string n, m;
	getline(cin, n);
	getline(cin, m);

	SORT(n);
	SORT(m);

	int result = 0;

	vector<pair<char, int>> alpN, alpM;
	FOR(i, 26) {
		alpN.push_back(make_pair('a' + i, 0));
		alpM.push_back(make_pair('a' + i, 0));
	}

	FOR(i, 26) {
		FOR(j, n.size()) {
			if (n[j] == alpN[i].first) {
				alpN[i].second++;
			} else if (n[j] > alpN[i].first) {
				break;
			}
		}

		FOR(j, m.size()) {
			if (m[j] == alpM[i].first) {
				alpM[i].second++;
			} else if (m[j] > alpM[i].first) {
				break;
			}
		}
	}

	FOR(i, 26) {
		result += min(alpN[i].second, alpM[i].second);
		if (alpM[i].second && !alpN[i].second) {
			cout << -1 << endl;
			return 0;
		}
	}

	if (result == 0) {
		cout << -1 << endl;
	} else {
		cout << result << endl;
	}
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<queue>
#include<set>
using namespace std;

#define VI vector<int>
#define II pair<int,int>
#define VII vector<pair<int,int>>
#define LI list<int>
#define MS(a, v) memset(a, v, sizeof(a))
#define LL long long

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

const int SIZE = 3e5+3;
const int MOD = 1e9+7;
const int INF = 1e9;

int n;
char in[1001], out[1001];
int area[26], request[26];

int main() {
    cin>>in>>out;

    int l = strlen(in);
    rep(i,l)
        area[in[i]-'a']++;

    l = strlen(out);
    rep(i,l)
        request[out[i]-'a']++;

    int ans = 0;
    bool flag = true;
    rep(i,26){
        ans += min(area[i], request[i]);
        if (request[i] && !area[i]) flag = false;
    }

    if (ans == 0 || !flag) ans = -1;
    cout<<ans;

    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<queue>
#include<set>
using namespace std;

#define VI vector<int>
#define II pair<int,int>
#define VII vector<pair<int,int>>
#define LI list<int>
#define MS(a, v) memset(a, v, sizeof(a))
#define LL long long

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

const int SIZE = 3e5+3;
const int MOD = 1e9+7;
const int INF = 1e9;

int n;
char in[1001], out[1001];
int area[26], request[26];

int main() {
    cin>>in>>out;

    int l = strlen(in);
    rep(i,l)
        area[in[i]-'a']++;

    l = strlen(out);
    rep(i,l)
        request[out[i]-'a']++;

    int ans = 0;
    rep(i,26)
        ans += min(area[i], request[i]);

    if (ans == 0) ans = -1;
    cout<<ans;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
    string a,b;
    int i,j,t1,t2,ans=0;
    cin>>a>>b;
    for (i=0;i<26;i++) {
        t1=t2=0;
        for (j=0;j<a.size();j++) t1+=a[j]=='a'+i;
        for (j=0;j<b.size();j++) t2+=b[j]=='a'+i;
        ans+=min(t1,t2);
    }
    if (!ans) ans--;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;

int main() {
    string a,b;
    int i,j,t1,t2,ans=0;
    cin>>a>>b;
    for (i=0;i<26;i++) {
        t1=t2=0;
        for (j=0;j<a.size();j++) t1+=a[j]=='a'+i;
        for (j=0;j<b.size();j++) t2+=b[j]=='a'+i;
        if (t2 && !t1) {
            ans=-1;
            break;
        }
        ans+=min(t1,t2);
    }
    cout<<ans<<endl;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>

#define xx first
#define yy second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define MAX 1000000
using namespace std;

int dirx[8] = {1,0,-1,0,1,-1,-1,1};
int diry[8] = {0,1,0,-1,1,1,-1,-1};
string s1, s2;
main() {
   //freopen("input.in","r",stdin);
    int n, k[101], ans = 0, i, j,a[26] = {0}, b[26] = {0};
    cin >> s1 >> s2;
    for(i=0;i<s1.size();i++) {
        a[s1[i]-'a']++;
    }
     for(i=0;i<s2.size();i++) {
        b[s2[i]-'a']++;
    }
    for(i=0;i<26;i++) {
        if(b[i]>0&&a[i]==0) {
            cout << -1;return 0;
        }
        ans += min(b[i],a[i]);
    }
    cout << ans;
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>

#define xx first
#define yy second
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define MAX 1000000
using namespace std;

int dirx[8] = {1,0,-1,0,1,-1,-1,1};
int diry[8] = {0,1,0,-1,1,1,-1,-1};
string s1, s2;
main() {
   //freopen("input.in","r",stdin);
    int n, k[101], ans = 0, i, j,a[26] = {0}, b[26] = {0};
    cin >> s1 >> s2;
    for(i=0;i<s1.size();i++) {
        a[s1[i]-'a']++;
    }
     for(i=0;i<s2.size();i++) {
        b[s2[i]-'a']++;
    }
    for(i=0;i<26;i++) {
        if(b[i]==0&&a[i]>0) {
            cout << -1;return 0;
        }
        ans += min(b[i],a[i]);
    }
    cout << ans;
    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main()
{
    string a,b;
    long p[300],q[300],ans=0;
    cin>>a>>b;
    for(int i=0;i<250;i++)
    {
        p[i]=0;
        q[i]=0;
    }
    for(int i=0;i<a.length();i++)
    {
        p[a[i]]++;
    }
    for(int i=0;i<b.length();i++)
    {
        q[b[i]]++;
    }
    for(int i=1;i<250;i++)
    {
        ans+=min(p[i],q[i]);

    }
    for(int i=1;i<250;i++)
    {
        if(p[i]==0 && q[i]!=0)
        {
            cout<<-1<<endl;
            return 0;
        }
    }

    cout<<ans<<endl;

}


#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int exist[26],garland[26];
    int get=0;
    string n,m;
    cin>>n;
    for(int i=0;i<n.length();i++)
    {
        exist[n[i]-'a']++;
    }
    cin>>m;
    for(int i=0;i<m.length();i++)
    {
        garland[m[i]-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(exist[i]==0 && garland[i]!=0)
        {
            printf("-1");
            return 0;
        }
        else if(exist[i]==0 && garland[i]==0)
            continue;
        else if(exist[i]>garland[i])
        {
            get+=garland[i];
        }
        else if(exist[i]<=garland[i])
        {
            get+=exist[i];
        }
    }
    printf("%d\n",get);
    return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int exist[26],garland[26];
    memset(exist,0,sizeof(exist));
    memset(garland,0,sizeof(garland));
    int get=0;
    string n,m;
    cin>>n;
    for(int i=0;i<n.length();i++)
    {
        exist[n[i]-'a']++;
    }
    cin>>m;
    for(int i=0;i<m.length();i++)
    {
        garland[m[i]-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(exist[i]==0 && garland[i]!=0)
        {
            printf("-1");
            return 0;
        }
        else if(exist[i]==0 && garland[i]==0)
            continue;
        else if(exist[i]>garland[i])
        {
            get+=garland[i];
        }
        else if(exist[i]<=garland[i])
        {
            get+=exist[i];
        }
    }
    printf("%d\n",get);
    return 0;
}


#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int exist[26],garland[26];
    int get=0;
    string n,m;
    cin>>n;
    for(int i=0;i<n.length();i++)
    {
        exist[n[i]-'a']++;
    }
    cin>>m;
    for(int i=0;i<m.length();i++)
    {
        garland[m[i]-'a']++;
    }
    for(int i=0;i<26;i++)
    {
        if(exist[i]==0 && garland[i]!=0)
        {
            printf("-1");
            return 0;
        }
        else if(exist[i]==0 && garland[i]==0)
            continue;
        else if(exist[i]>garland[i])
        {
            get+=garland[i];
        }
        else if(exist[i]<=garland[i])
        {
            get+=exist[i];
        }
    }
    printf("%d",get);
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int cs[30], bs[30];
int main(){
    ///////////////////////////////R_W;
    string a, b;
    cin >> a >> b;
    r(a.size())
    {
        cs[a[i]-'a']++;
        bs[b[i] - 'a']++;
    }
    int area=0;
    r(30)
    {
        if (cs[i] > bs[i]){
            area += bs[i];
        }
        else{
            area += cs[i];
        }
        if (!cs[i] && bs[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    if (!area) area = -1;
    cout << area << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int cs[30], bs[30];
int main(){
   /////////////////////////////// R_W;
    string a, b;
    cin >> a >> b;
    r(a.size())
    {
        cs[a[i]-'a']++;
        bs[b[i] - 'a']++;
    }
    int area=0;
    r(30)
    {
        if (cs[i] > bs[i]){
            area += bs[i];
        }
        else{
            area += cs[i];
        }
    }
    if (!area) area = -1;
    cout << area << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int cs[30], bs[30];
int main(){
	////////////////////////////////R_W;
	string a, b;
	cin >> a >> b;
	r(a.size())
	{
		cs[a[i]-'a']++;
		bs[b[i] - 'a']++;
	}
	int area=0;
	r(30)
	{
		if (cs[i] > bs[i]){
			area += bs[i];
		}
		else{
			area += cs[i];
		}
	}
	cout << area << endl;
	return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int cs[30], bs[30];
int main(){
   ///////////////////////////// R_W;
    string a, b;
    cin >> a >> b;
    r(a.size())
    {
        cs[a[i]-'a']++;

    }
    r(b.size()){
        bs[b[i] - 'a']++;
    }
    int area=0;
    r(30)
    {
        if (cs[i] > bs[i]){
            area += bs[i];
        }
        else{
            area += cs[i];
        }
        if (!cs[i] && bs[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    if (!area) area = -1;
    cout << area << endl;
    return 0;
}


#include<iostream>
#include<map>
#include<string>
#include<string.h>
#include<vector>
#include<stdio.h>
#include <cstdio>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <sstream>
#include <cmath>
#include <bitset>
#include <limits.h>
#include <limits>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#define all(v) v.begin(),v.end()
#define R(i) freopen(i,"r",stdin)
#define W(i) freopen(i,"w",stdout)
#define R_W R("i.txt"),W("o.txt");
#define FOR(i,f,t) for(int i=f;i<t;i++)
#define r(e) for(int i=0;i<e;i++)
#define inf numeric_limits<int>::max()
#define minf numeric_limits<int>::min()
#define DFS_WHITE -1
#define DFS_BLACK 1
using namespace std;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
vector< vi > AdjList;
int cs[30], bs[30];
int main(){
    ///////////////R_W;
    string a, b;
    cin >> a >> b;
    r(a.size())
    {
        cs[a[i]-'a']++;
        bs[b[i] - 'a']++;
    }
    int area=0;
    r(30)
    {
        if (cs[i] > bs[i]){
            area += bs[i];
        }
        else{
            area += cs[i];
        }
        if (cs[i] && !bs[i]){
            cout << -1 << endl;
            return 0;
        }
    }
    if (!area) area = -1;
    cout << area << endl;
    return 0;
}


#include <stdio.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))

int cnt[2][128];

int main(void) {
    int i;
    int ans;

    for (i = 0; i < 2; i++) {
        char str[1001], *p;

        scanf("%s", str);
        for (p = str; *p != '\0'; p++)
            cnt[i][*p]++;
    }

    ans = 0;
    for (i = 'a'; i <= 'z'; i++)
        if (cnt[0][i] == 0 && cnt[1][i]) {
            puts("-1");

            return 0;
        } else
            ans += min(cnt[0][i], cnt[1][i];

    printf("%d\n", ans);

    return 0;
}


#include <stdio.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))

int cnt[2][128];

int main(void) {
    int i;
    int ans;

    for (i = 0; i < 2; i++) {
        char str[1001], *p;

        scanf("%s", str);
        for (p = str; *p != '\0'; p++)
            cnt[i][*p]++;
    }

    ans = 0;
    for (i = 'a'; i <= 'z'; i++)
        if (cnt[0][i] == 0 && cnt[1][i]) {
            puts("-1");

            return 0;
        } else
            ans += min(cnt[0][i], cnt[1][i]);

    printf("%d\n", ans);

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#define ARR_SIZE ('z'-'a' + 1)
#define N 1001

int min(int a, int b)
{
    return (a > b ? b : a);
}

int main(int argc, char* argv[], char* envp[])
{
    int  i, t, s;
    char a[N];
    int  b[ARR_SIZE] = { 0 }, c[ARR_SIZE] = { 0 };

    scanf("%s", a);
    for(i = 0; a[i]; i++)
    {
    b[a[i] - 'a']++;
    }

    scanf("%s", a);
    for(i = 0; a[i]; i++)
    {
    c[a[i] - 'a']++;
    }

    s = 0;
    for(i = 0; i < ARR_SIZE; i++)
    {
    if(c[i] == 0) continue;
    t = min(b[i], c[i]);
    if(t == 0)
    {
        s = -1;
        break;
    } else s += t;
    }

    printf("%d\n", s);
    return 0;
}


/* *****
* @Codeforces Problem 408B. Garland
* @author:ckmoonfish
* @Date: 2014.4.9
* @tag: implememtation
*******/

#include <stdio.h>
#include <string.h>
#define MAXN 1010
int main(){
    char n[MAXN], m[MAXN];
    int chn[30], chm[30];
    int i, j, ans;
    memset(chn, 0, sizeof(chn));
    memset(chm, 0, sizeof(chm));
    ans = 0;
    scanf("%s%s", n, m);

    for(i = 0; i < strlen(m); ++i){
        int flag = 0;
        for(j = 0; j < strlen(n); ++j)
            if(m[i] == n[j]){
                flag = 1;
            }
        if(!flag){
            printf("-1");
            return 0;
        }
    }

/*
    for(i = 1; i < strlen(n); ++i){
        int p = 0, cnt = 1;
        while(n[i] == n[i-1]){
            i++;
            cnt++;
        }
        chn[n[i]-'a'][p++] = cnt;
    }
*/

    for(i = 0; i < strlen(n); ++i){
            chn[n[i]-'a']++;
    }

    for(i = 0; i < strlen(m); ++i){
        chm[m[i]-'a']++;
    }
    for(i = 0; i < 30; ++i){
        if(chm[i] <= chn[i])
            ans += chm[i];
        else
            ans += chn[i];
    }

    printf("%d", ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
#define MAXN 1010
int main(){
    char n[MAXN], m[MAXN];
    int chn[30], chm[30];
    int i, j, ans;
    memset(chn, 0, sizeof(chn));
    memset(chm, 0, sizeof(chm));
    ans = 0;
    scanf("%s%s", n, m);

    for(i = 0; i < strlen(m); ++i){
        int flag = 0;
        for(j = 0; j < strlen(n); ++j)
            if(m[i] == n[j]){
                flag = 1;
            }
        if(!flag){
            printf("-1");
            return 0;
        }
    }

/*
    for(i = 1; i < strlen(n); ++i){
        int p = 0, cnt = 1;
        while(n[i] == n[i-1]){
            i++;
            cnt++;
        }
        chn[n[i]-'a'][p++] = cnt;
    }
*/

    for(i = 0; i < strlen(n); ++i){
            chn[n[i]-'a']++;
    }

    for(i = 0; i < strlen(m); ++i){
        chm[m[i]-'a']++;
    }
    for(i = 0; i < 30; ++i){
        if(chm[i] <= chn[i])
            ans += chm[i];
        else
            ans += chn[i];
    }

    printf("%d", ans);
    return 0;
}


#include <stdio.h>
#include <string.h>
int min(int a,int b)
{
    if(a>b)
    return b;
    else
    return a;
    }
int main(void)
{
    char s1[2000],s2[2000];
    int a[26]={0},b[26]={0};
    int i; int p=0, c=-1;
    scanf("%s %s",s1,s2);
    int l1=strlen(s1);
    int l2=strlen(s2);
    for(i=0;i<l1;i++)
        a[s1[i]-97]++;
    for(i=0;i<l2;i++)
        b[s2[i]-97]++;
    for(i=0;i<26;i++)
    {
        if(a[i]>0&&b[i]>0)
        {
            p+=min(a[i],b[i]);
            c=0;
        }
    }
    if(c==-1)
        printf("-1\n");
    else
        printf("%d\n",p);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int f1[26], f2[26];

int main() {
    int ok, m, i, n;
    char ch;
    n = 0;
    ch = fgetc( stdin );
    while ( ch != '\n' ) {
        f1[ch - 'a']++;
        ch = fgetc( stdin );
        n++;
    }
    ch = fgetc( stdin );
    while ( ch != '\n' ) {
        f2[ch - 'a']++;
        ch = fgetc( stdin );
    }
    ok = 1;
    m = 0;
    for ( i = 0; i < 26; i++ ) {
        if ( f1[i] == 0 && f2[i] != 0 ) {
            ok = 0;
        } else if ( f1[i] > f2[i] ) {
            m += f1[i] - f2[i];
        }
    }
    if ( ok == 1 )
        printf( "%d", n - m );
    else
        printf( "-1" );
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int f1[26], f2[26];

int main() {
    int ok, m, i, n;
    char ch;
    n = 0;
    ch = fgetc( stdin );
    while ( ch != '\n' ) {
        f1[ch - 'a']++;
        ch = fgetc( stdin );
        n++;
    }
    ch = fgetc( stdin );
    while ( ch != '\n' ) {
        f2[ch - 'a']++;
        ch = fgetc( stdin );
    }
    ok = 1;
    m = 0;
    for ( i = 0; i < 26; i++ ) {
        if ( ( f1[i] != 0 && f2[i] == 0 ) || ( f1[i] == 0 && f2[i] != 0 ) ) {
            ok = 0;
        } else if ( f1[i] > f2[i] ) {
            m += f1[i] - f2[i];
        }
    }
    if ( ok == 1 )
        printf( "%d", n - m );
    else
        printf( "-1" );
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[1002],b[1002];
	short int c[28],i,len1,len2,sum;
	while(scanf("%s%s",a,b)!=EOF)
	{
		len1=strlen(a);
		len2=strlen(b);
		for(i=0;i<26;i++)c[i]=0;
		for(i=0;i<len1;i++)c[a[i]-'a']++;
		for(i=0;i<len2;i++)c[b[i]-'a']--;
		for(i=0,sum=len2;i<26;i++)if(c[i]<0)sum+=c[i];
		if(sum<=0)printf("-1\n");
		else printf("%d\n",sum);
	}
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[1002],b[1002];
	short int c[28],d[28],i,len1,len2,sum;
	while(scanf("%s%s",a,b)!=EOF)
	{
		len1=strlen(a);
		len2=strlen(b);
		for(i=0;i<26;i++)c[i]=d[i]=0;
		for(i=0;i<len1;i++)c[a[i]-'a']++;
		for(i=0;i<len2;i++)d[b[i]-'a']++;
		for(i=0,sum=0;i<26;i++)if(c[i]==0 && d[i]){sum=-1;break;}else sum+=c[i]>d[i]?d[i]:c[i];
		printf("%d\n",sum);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>

int main(){
    char a[1001],b[1001];
    int count=0,i,j,t1,t2,flag=0;

    scanf("%s",a);

    scanf("%s",b);

    for(i=97;i<=122;i++){
        t1=0;
        t2=0;
        for(j=0;j<=strlen(a);j++)
            if(a[j]==i)
               t1++;

        for(j=0;j<=strlen(b);j++)
            if(b[j]==i)
               t2++;

        if(t1==0&&t2!=0){
            flag=1;
            break;
        }

        if(t1==0&&t2==0)
            continue;

        else if(t2<t1)
            count+=t2;
        else if(t2>=t1)
            count+=t1;
    }

    if(flag==0)
        printf("%d\n",count);
    if(flag==1)
        printf("-1\n");

    return 0;
}


#include<stdio.h>
int Min(int a,int b){
    return (a<b)?a:b;
}
int main(){
 int a[26] = {0},b[29]={0},max=0,i;
 char c = getchar();
 while(c!='\n'){
    a[c-'a']++;
    c = getchar();
 }
 c = getchar();
 while(c!='\n'){
    b[c-'a']++;
    c = getchar();
 }
 for(i=0;i<26;i++){
    if(b[i]!=0){
        if(a[i] == 0){
            printf("-1\n");
            return 0;
        }
        max += Min(a[i],b[i]);
    }
 }
 printf("%d\n",max);
 return 0;
}


#include<string.h>
#include<stdio.h>
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
int main()
{
    char s1[1001],s2[1001];
    int i,j,l1,l2;
    int a[26]={0};
    int b[26]={0};
    scanf("%s",s1);
    scanf("%s",s2);
    l1=strlen(s1);
    l2=strlen(s2);
    for(i=0;i<l1;i++)
    a[s1[i]-'a']++;
    for(i=0;i<l2;i++)
    b[s2[i]-'a']++;
    int s=0;
    for(i=0;i<26;i++)
    {
        s=s+min(a[i],b[i]);
    }
    if(s==0)
    printf("-1");
    else
    printf("%d",s);
}


#include<string.h>
#include<stdio.h>
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
int main()
{
    char s1[10001],s2[10001];
    int i,j,l1,l2;
    int a[27]={0};
    int b[27]={0};
    scanf("%s",s1);
    scanf("%s",s2);
    l1=strlen(s1);
    l2=strlen(s2);
    for(i=0;i<l1;i++)
    a[s1[i]-'a']++;
    for(i=0;i<l2;i++)
    b[s2[i]-'a']++;
    int s=0;
    for(i=0;i<=26;i++)
    {
        s=s+min(a[i],b[i]);
    }
    if(s==0 || s==l1)
    printf("-1");
    else
    printf("%d",s);
    return 0;
}


#include<string.h>
#include<stdio.h>
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
int main()
{
    char s1[1001],s2[1001];
    int i,j,l1,l2;
    int a[26]={0};
    int b[26]={0};
    scanf("%s",s1);
    scanf("%s",s2);
    l1=strlen(s1);
    l2=strlen(s2);
    for(i=0;i<l1;i++)
    a[s1[i]-'a']++;
    for(i=0;i<l2;i++)
    b[s2[i]-'a']++;
    int s=0;
    for(i=0;i<25;i++)
    {
        s=s+min(a[i],b[i]);
    }
    if(s==0)
    printf("-1");
    else
    printf("%d",s);
}


#include<string.h>
#include<stdio.h>
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
int main()
{
    char s1[1001],s2[1001];
    int i,j,l1,l2;
    int a[27]={0};
    int b[27]={0};
    scanf("%s",s1);
    scanf("%s",s2);
    l1=strlen(s1);
    l2=strlen(s2);
    for(i=0;i<l1;i++)
    a[s1[i]-'a']++;
    for(i=0;i<l2;i++)
    b[s2[i]-'a']++;
    int s=0;
    for(i=0;i<26;i++)
    {
        s=s+min(a[i],b[i]);
    }
    if(s==0)
    printf("-1");
    else
    printf("%d",s);
    return 0;
}


#include <stdio.h>
#include <string.h>

int min(int a, int b){return (a<b)?a:b;}

char a[1005], b[1005], cvp1[500], cvp2[500], cvp, sonuc[500];
int i, j, x;

int main () {
	scanf("%s %s",a,b);
	for(i = 0 ; i < strlen(a) ; i++)
		cvp1[a[i]]++;
	for(i = 0 ; i < strlen(b) ; i++)
		cvp2[b[i]]++;
	for(i = 'a' ; i <= 'z' ; i++){
		if(cvp2[i] != 0 && cvp1[i] == 0){
			printf("-1");
			return 0;
		}
		x += min(cvp1[i], cvp2[i]);
	}
	printf("%d",x);
	return 0;
}


#include <stdio.h>
#include <string.h>

char a[1005], b[1005], cvp1[500], i, cvp;

int main () {
	scanf("%s %s",a,b);
	for(i = 0 ; i < strlen(a) ; i++)
		cvp1[a[i]]++;
	for(i = 0 ; i < strlen(b) ; i++){
		if(cvp1[b[i]] > 0){
			cvp++;
			cvp1[b[i]]--;
		}
	}
	printf("%d",(cvp > 2)?cvp:-1);
	return 0;
}


#include <stdio.h>
#include <string.h>

char a[1005], b[1005], cvp1[500], i, cvp;

int main () {
	scanf("%s %s",a,b);
	for(i = 0 ; i < strlen(a) ; i++)
		cvp1[a[i]]++;
	for(i = 0 ; i < strlen(b) ; i++){
		if(cvp1[b[i]] > 0){
			cvp++;
			cvp1[b[i]]--;
		}
	}
	printf("%d",(cvp)?cvp:-1);
	return 0;
}


#include <stdio.h>
#include <string.h>

char a[1005], b[1005], cvp1[500], i, cvp;

int main () {
	scanf("%s %s",a,b);
	for(i = 0 ; i < strlen(a) ; i++)
		cvp1[a[i]]++;
	for(i = 0 ; i < strlen(b) ; i++){
		if(cvp1[b[i]] > 0){
			cvp++;
			cvp1[b[i]]--;
		}
	}
	printf("%d",(cvp != 2)?cvp:-1);
	return 0;
}


#include <stdio.h>
#include <string.h>

char a[1005], b[1005], cvp1[500], i, cvp;

int main () {
	scanf("%s %s",a,b);
	for(i = 0 ; i < strlen(a) ; i++)
		cvp1[a[i]]++;
	for(i = 0 ; i < strlen(b) ; i++){
		if(cvp1[b[i]] > 0){
			cvp++;
			cvp1[b[i]]--;
		}
	}
	printf("%d",(cvp != 2 && cvp != 0)?cvp:-1);
	return 0;
}


#include <stdio.h>
#include <string.h>

int min(int a, int b){return (a<b)?a:b;}

char a[1005], b[1005];
int i, j, x, cvp1[500], cvp2[500];

int main () {
	scanf("%s %s",a,b);
	for(i = 0 ; i < strlen(a) ; i++)
		cvp1[a[i]]++;
	for(i = 0 ; i < strlen(b) ; i++)
		cvp2[b[i]]++;
	for(i = 'a' ; i <= 'z' ; i++){
		if(cvp2[i] != 0 && cvp1[i] == 0){
			printf("-1");
			return 0;
		}
		x += min(cvp1[i], cvp2[i]);
	}
	printf("%d",x);
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

typedef struct
{
     short  count;
      char  has;
} cleaf;

#define nsize ((1000 + 1) * sizeof(char))
#define msize ((1000 + 1) * sizeof(char))
#define csize (27 * sizeof(cleaf))

int main()
{
   char buff[nsize + msize + csize];
   char *b = buff;
   char *n = b;
   char *m = b + nsize;
   cleaf *c = (cleaf *)(b + nsize + msize);
   //for (;;)
   //{
      gets(n);
      gets(m);
      memset(c, 0, csize);
      const char *p1 = n;
      for (; *p1 != 0; ++p1)
      {
         cleaf *cp = &c[*p1 - 'a'];
         ++cp->count;
         cp->has = 1;
      }
      short r = 0;
      const char *p2 = m;
      for (; *p2 != 0; ++p2)
      {
         cleaf *cp = &c[*p2 - 'a'];
         if (!cp->has)
         {
            r = -1;
            break;
         }
         if (cp->count)
         {
            ++r;
            --cp->count;
         }
      }
      printf("%d\n", r);
   //}
   return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

typedef struct
{
     short  count;
      char  has;
} cleaf;

#define nsize ((1000 + 1) * sizeof(char))
#define msize ((1000 + 1) * sizeof(char))
#define csize (27 * sizeof(cleaf))

int main()
{
   char buff[nsize + msize + csize];
   char *b = buff;
   char *n = b;
   char *m = b + nsize;
   cleaf *c = (cleaf *)(b + nsize + msize);
   for (;;)
   {
      gets(n);
      gets(m);
      memset(c, 0, csize);
      for (const char *p = n; *p != 0; ++p)
      {
         cleaf *cp = &c[*p - 'a'];
         ++cp->count;
         cp->has = 1;
      }
      short r = 0;
      for (const char *p = m; *p != 0; ++p)
      {
         cleaf *cp = &c[*p - 'a'];
         if (!cp->has)
         {
            r = -1;
            break;
         }
         if (cp->count)
         {
            ++r;
            --cp->count;
         }
      }
      printf("%d\n", r);
   }
   return 0;
}


#include<stdio.h>
int main()
{
    char a[1005],b[1005];
int want[27]={0},hav[27]={0};
    scanf("%s",a);
int i,l=strlen(a);
for(i=0;i<l;++i)
{
    hav[a[i]-'a']++;
}
scanf("%s",b);
int l2=strlen(b);
for(i=0;i<l2;++i)
    want[b[i]-'a']++;
int fl=0,ans=0;
for(i=0;i<27;++i)
{
    if(hav[i]>=want[i])
        ans+=want[i];
    if(want[i]>hav[i])
        ans+=hav[i];
    if(want[i]!=0 && hav[i]==0)
    {
    fl=1;
    break;
    }
}
if(fl==1)
    printf("-1");
else
    printf("%d",ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[300],b[300],sum=0;
    char s[1010],s1[1010],c;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    while(scanf("%c",&c)&&c!='\n')
        a[c]++;
    while(scanf("%c",&c)&&c!='\n')
        b[c]++;
    for(c='a';c<='z';c++)
    {
        if(a[c]>=b[c])
            sum+=b[c];
        else
            sum+=a[c];
    }
    printf("%d\n",sum);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[300],b[300],sum=0;
    char s[1010],s1[1010],c;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    while(scanf("%c",&c)&&c!='\n')
        a[c]++;
    while(scanf("%c",&c)&&c!='\n')
        b[c]++;
    for(c='a';c<='z';c++)
    {
        if(a[c]>=b[c])
            sum+=b[c];
        else
            sum+=a[c];
    }
    if(sum==0) sum=-1;
    printf("%d\n",sum);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a[300],b[300],sum=0,ok=0;
    char s[1010],s1[1010],c;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    while(scanf("%c",&c)&&c!='\n')
        a[c]++;
    while(scanf("%c",&c)&&c!='\n')
        b[c]++;
    for(c='a';c<='z';c++)
    {
        if(b[c]&&!a[c]) ok=1;
        if(a[c]>=b[c])
            sum+=b[c];
        else
            sum+=a[c];
    }
    if(sum==0||ok) sum=-1;
    printf("%d\n",sum);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()	{

int arr[26],b[26];
char c;
int i,cnt,f;

for(i=0;i<26;i++)
arr[i]=b[i]=0;

while((c=getchar())!='\n')	{
arr[c-'a']++;
}

cnt=0;

while((c=getchar())!='\n')	{
b[c-'a']++;
}

f=0;

for(i=0;i<26 && !f;i++)	{

if(b[i]>0)	{

if(arr[i]==0)
f=1;
else	{

if(b[i]>=arr[i])	{
cnt+=arr[i];
}
else
cnt+=b[i];
}
}

}

if(f)
printf("-1\n");
else printf("%d\n",cnt);

return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()	{

int arr[26];
char c;
int i,cnt;

for(i=0;i<26;i++)
arr[i]=0;

while((c=getchar())!='\n')	{
arr[c-'a']++;
}

cnt=0;

while((c=getchar())!='\n')	{

if(arr[c-'a']>0)	{
arr[c-'a']--;
cnt++;
}
}

if(cnt)
printf("%d\n",cnt);
else printf("-1\n");

return 0;
}


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#define N 26

#define min(a, b) ((a) < (b) ? (a) : (b))

int a[2][N];

int main(void) {
  int c, i, r = 0, n = 0, ok = 1;
  for (i = 0; i < 2; ++i) {
    while (c = getchar(), isalpha(c)) {
      ++a[i][c - 'a'];
      if (i == 0) ++n;
    }
  }
  for (i = 0; i < N; ++i) {
    r += min(a[0][i], a[1][i]);
    if (a[1][i] && !a[0][i]) ok = 0;
  }
  printf("%d\n", ok ? r : -1);
  exit(EXIT_SUCCESS);
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[30],sle2[30];
int main()
{
	int i,num=0,len_1,len_2,flag=0,j;
	scanf("%s%s",s1,s2);
	len_1=strlen(s1);
	len_2=strlen(s2);
	memset(sle1,0,sizeof(sle1));
	memset(sle2,0,sizeof(sle2));
	for(i=0;i<len_2;i++)
	{
		flag=0;
		for(j=0;j<len_1;j++)
		{
			if(s2[i]==s1[j])
			{
				flag=1;
				break;
			}
		}
	}
	for(i=0;i<len_2;i++)
	{
		sle2[s2[i]-'a']++;
	}
	for(i=0;i<len_1;i++)
	{
		sle1[s1[i]-'a']++;
	}

	if(flag==1)
	{
		for(i=0;i<26;i++)
	{
		if(sle1[i]<sle2[i])
			num=num+sle1[i];
		else if(sle1[i]>sle2[i])
			num=num+sle2[i];
		else num=num+sle2[i];
	}
	}
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[1005],sle2[1005];
int main()
{
    int i,num=0,len_1,len_2,flag=0,j;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
    for(i=0;i<len_2;i++)
    {
        flag=0;
        for(j=0;j<len_1;j++)
        {
            if(s2[i]==s1[j])
            {
                flag=1;
            break;
            }
        }
      if(flag==0) break;
    }
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }

    if(flag==1)
    {
        for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
    }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[1005],sle2[1005];
int main()
{
    int i,num=0,len_1,len_2,flag=0,j;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
/// for(i=0;i<len_2;i++)
//  {
//      flag=0;
//      for(j=0;j<len_1;j++)
//      {
//          if(s2[i]==s1[j])
//          {
//              flag=1;
///             break;
//          }
//          else continue;
//      }
//  }
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }

//  if(flag==1)
//  {
        for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
//  }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[1005],sle2[1005];
int main()
{
	int i,num=0,len_1,len_2,flag=0,j;
	scanf("%s%s",s1,s2);
	len_1=strlen(s1);
	len_2=strlen(s2);
	memset(sle1,0,sizeof(sle1));
	memset(sle2,0,sizeof(sle2));
	for(i=0;i<len_2;i++)
	{
		flag=0;
		for(j=0;j<len_1;j++)
		{
			if(s2[i]==s1[j])
			{
				flag=1;
				break;
			}
		}
	}
	for(i=0;i<len_2;i++)
	{
		sle2[s2[i]-'a']++;
	}
	for(i=0;i<len_1;i++)
	{
		sle1[s1[i]-'a']++;
	}

	if(flag==1)
	{
		for(i=0;i<26;i++)
	{
		if(sle1[i]<sle2[i])
			num=num+sle1[i];
		else if(sle1[i]>sle2[i])
			num=num+sle2[i];
		else num=num+sle2[i];
	}
	}
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[30],sle2[30];
int main()
{
    int i,num=0,len_1,len_2,flag=0,j;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
    for(i=0;i<len_2;i++)
    {
        flag=0;
        for(j=0;j<len_1;j++)
        {
            if(s2[i]==s1[j])
            {
                flag=1;
                break;
            }
        }
    }
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }

    if(flag==1)
    {
        for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
    }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[1005],sle2[1005];
int main()
{
    int i,num=0,len_1,len_2;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }
    for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[1005],sle2[1005];
int main()
{
    int i,num=0,len_1,len_2,flag=0,j;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
    for(i=0;i<len_2;i++)
    {
        flag=0;
        for(j=0;j<len_1;j++)
        {
            if(s2[i]==s1[j])
            {
                flag=1;
                break;
            }
            else continue;
        }
    }
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }

    if(flag==1)
    {
        for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
    }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[1005],sle2[1005];
int main()
{
    int i,num=0,len_1,len_2,flag=0,j;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
    for(i=0;i<len_2;i++)
    {
        flag=0;
        for(j=0;j<len_1;j++)
        {
            if(s2[i]==s1[j])
            {
                flag=1;
                break;
            }
        }
    }
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }

    if(flag==1)
    {
        for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
    }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[30],sle2[30];
int main()
{
    int i,num=0,len_1,len_2;
    scanf("%s%s",s1,s2);
    len_1=strlen(s1);
    len_2=strlen(s2);
    memset(sle1,0,sizeof(sle1));
    memset(sle2,0,sizeof(sle2));
    for(i=0;i<len_2;i++)
    {
        sle2[s2[i]-'a']++;
    }
    for(i=0;i<len_1;i++)
    {
        sle1[s1[i]-'a']++;
    }
    for(i=0;i<26;i++)
    {
        if(sle1[i]<sle2[i])
            num=num+sle1[i];
        else if(sle1[i]>sle2[i])
            num=num+sle2[i];
        else num=num+sle2[i];
    }
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s1[1005],s2[1005];
int sle1[30],sle2[30];
int main()
{
	int i,num=0,len_1,len_2;
	scanf("%s%s",s1,s2);
	len_1=strlen(s1);
	len_2=strlen(s2);
	memset(sle1,0,sizeof(sle1));
	memset(sle2,0,sizeof(sle2));
	for(i=0;i<len_2;i++)
	{
		sle2[s2[i]-'a']++;
	}
	for(i=0;i<len_1;i++)
	{
		sle1[s1[i]-'a']++;
	}
	for(i=0;i<26;i++)
	{
		if(sle1[i]<sle2[i])
			num=num+sle1[i];
		else if(sle1[i]>sle2[i])
			num=num+sle2[i];
		else num=num+sle2[i];
	}
if(num!=0) printf("%d\n",num);
else printf("-1\n");
}


#include <stdio.h>
#include <string.h>

int main() {
	static int kk1[26], kk2[26];
	static char s[1024], t[1024];
	int n, m, i, c, cnt;

	scanf("%s", s);
	n = strlen(s);
	scanf("%s", t);
	m = strlen(t);
	for (i = 0; i < n; i++)
		kk1[s[i] - 'a']++;
	for (i = 0; i < m; i++)
		kk2[t[i] - 'a']++;
	cnt = 0;
	for (c = 0; c < 26; c++) {
		if (kk1[c] == 0 && kk2[c] > 0) {
			printf("-1\n");
			return 0;
		}
		cnt += kk1[c] < kk2[c] ? kk1[c] : kk2[c];
	}
	printf("%d\n", cnt);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fr1[30];
int fr2[30];
char s1[1005];
char s2[1005];
int main()
{
    int n,m,i,ok=1,total;
    gets(s1);
    gets(s2);
    n=strlen(s1);
    m=strlen(s2);
    for(i=0;i<n;i++)
        fr1[s1[i]-'a']++;
    for(i=0;i<m;i++)
        fr2[s2[i]-'a']++;
    for(i=0;i<30;i++)
    {
        if( fr1[i]!=fr2[i]  &&  fr1[i]==0 && fr2[i]>0 )
        {
            ok=0;
            printf("-1");
            i=31;
        }
    }
    if(ok==1)
    {
        total=0;
        for(i=0;i<30;i++)
        {
            if(fr1[i]>fr2[i])
                total+=fr2[i];
            else
            {
                if(fr1[i]!=0 && fr1[i]==fr2[i])
                    total=total+fr1[i];
                else
                {
                    if(fr2[i]>fr1[i])
                    {
                        total=total+fr1[i];
                    }
                }
            }
        }
        printf("%d",total);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int fr1[30];
int fr2[30];
char s1[1005];
char s2[1005];
int main()
{
    int n,m,i,ok=1,total;
    gets(s1);
    gets(s2);
    n=strlen(s1);
    m=strlen(s2);
    for(i=0;i<n;i++)
        fr1[s1[i]-'a']++;
    for(i=0;i<m;i++)
        fr2[s2[i]-'a']++;
    for(i=0;i<30;i++)
    {
        if( fr1[i]!=fr2[i]  &&  fr1[i]==0 && fr2[i]>0 )
        {
            ok=0;
            printf("-1");
            i=31;
        }
    }
    if(ok==1)
    {
        total=0;
        for(i=0;i<30;i++)
        {
            if(fr1[i]>fr2[i])
                total+=fr2[i];
            else
            {
                if(fr1[i]!=0 && fr1[i]==fr2[i])
                    total=total+fr1[i];
                else
                {
                    if(fr2[i]>fr1[i])
                        total++;
                }
            }
        }
        printf("%d",total);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char str1[1010],str2[1010];
	while(gets(str1))
	{
		gets(str2);
		int i,len1,len2,s1[26],s2[26],ans,flag;
		len1=strlen(str1);
		len2=strlen(str2);
		memset(s1,0,sizeof(s1));
		memset(s2,0,sizeof(s2));
		for(i=0;i<len1;i++)
		{
			s1[str1[i]-'a']++;
		}
		for(i=0;i<len2;i++)
		{
			s2[str2[i]-'a']++;
		}
		ans=0;
		flag=1;
		for(i=0;i<26;i++)
		{
			if(s2[i]!=0)
			{
				if(s1[i]==0)
				{
					flag=0;
					break;
				}
			}
		}
		if(flag==0)
			printf("-1\n");
		else
		{
			for(i=0;i<26;i++)
			{
				if(s1[i]>=s2[i])
					ans+=s2[i];
				else
					ans+=s1[i];
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}


#include <stdio.h>

int min(int a, int b)
{
    if (a > b)
        return b;
    else
        return a;
}

int main()
{
    char c;
    int i, ex[26], nd[26], flag = 1, s = 0;

    for (i = 0; i < 26; i++)
    {
        ex[i] = 0;
        nd[i] = 0;
    }

    c = getchar();
    while (('a'<=c)&&(c<='z'))
    {
        ex[c-'a'] ++;
        c = getchar();
    }

    while (('a'>c)||(c>'z'))
        c = getchar();

    while (('a'<=c)&&(c<='z'))
    {
        nd[c-'a'] ++;
        c = getchar();
    }

    for (i = 0; i < 26; i++)
    {
        if ((nd[i] != 0)&&(ex[i] == 0))
            flag = 0;
        s += min(nd[i], ex[i]);
    }

    if (flag)
        printf("%d\n", s);
    else
        printf("-1\n");

    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
    char s[1001],m[1001];
    int t[26]={};
    int i,x,k,ans=0;
    scanf("%s",s);
    scanf("%s",m);
    x=strlen(s);
    for(i=0;i<x;i++)
    {
            k=s[i]-'a';
            t[k]++;
    }
    int y;
    y=strlen(m);
    int w[26]={};
    for(i=0;i<y;i++)
    {
            k=m[i]-'a';
            w[k]++;
    }
    for(i=0;i<26;i++)
    {
        if(t[i]>w[i])
        {
            ans=ans+w[i];
        }
        else if(t[i]<=w[i])
        {
            ans=ans+t[i];
        }
    }
    if(ans>0)
        printf("%d",ans);
    else
        printf("-1");
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int main()
{
    char s[1001],m[1001];
    int t[28]={};
    int i,x,k,ans=0;
    scanf("%s",s);
    scanf("%s",m);
    x=strlen(s);
    for(i=0;i<x;i++)
    {
            k=s[i]-'a';
            t[k]++;
    }
    int y;
    y=strlen(m);
    int w[28]={};
    for(i=0;i<y;i++)
    {
            k=m[i]-'a';
            w[k]++;
    }
    for(i=0;i<28;i++)
    {
        if(t[i]==0 && w[i]!=0)
        {
            ans=-1;
            break;
        }
        if(t[i]>w[i])
        {
            ans=ans+w[i];
        }
        else if(t[i]<=w[i])
        {
            ans=ans+t[i];
        }
    }

    printf("%d",ans);
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int sum,i,j,num1,num2,s1,s2,flag=1;
    char n[1000],m[1000],c;
    while(scanf("%s",n)!=EOF)
    {
        scanf("%s",m);
        for(i=0,flag=1;i<1000;i++)
        {
            if(n[i]!='n')
            {
                flag=0;
                break;
            }
        }
        s1=strlen(n);
        s2=strlen(m);
        for(sum=0,c='a';c<='z';c++)
        {
            for(i=0,num2=0;i<s2;i++)
                if(m[i]==c)
                    num2++;
            for(j=0,num1=0;j<s1;j++)
                if(n[j]==c)
                    num1++;
            if(num2!=0&&num1==0)
            {
                sum=-1;
                break;
            }
            if(num1>=num2)
                sum=sum+num2;
            else
                sum=sum+num1;
        }
        if(flag==1)
            printf("1000\n");
        else
            printf("%d\n",sum);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int sum,i,j,num1,num2,s1,s2;
    char n[1000],m[1000],c;
    while(scanf("%s",n)!=EOF)
    {
        scanf("%s",m);
        s1=strlen(n);
        s2=strlen(m);
        for(sum=0,c='a';c<='z';c++)
        {
            for(i=0,num2=0;i<s2;i++)
                if(m[i]==c)
                    num2++;
            for(j=0,num1=0;j<s1;j++)
                if(n[j]==c)
                    num1++;
            if(num2!=0&&num1==0)
            {
                sum=-1;
                break;
            }
            if(num1>=num2)
                sum=sum+num2;
            else
                sum=sum+num1;
        }
        printf("%d\n",sum);
    }
    return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }
  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <iostream>

long long distance[100001];

int main() {
  int days;
  std::cin >> days;
  for (int i = 0; i < days; i++) {
    std::cin >> distance[i];
  }

  // Largest locations, compare same days, get smallest k
  long long num_location = 0, small_k = -1;
  for (int i = 0; i < days; i++) {
    long long temp_k = distance[i] + 1;
    long long num_days = 0, temp_location = 0, temp_days = 0;
    bool is_valid = true;
    // std::cout << small_k << std::endl;
    for (int j = 0; j <= days; j++) {
      if (j < days && distance[j] < temp_k) {
        temp_days++;
        continue;
      }
      if (temp_days != 0) {
        if (num_days == 0) {
          num_days = temp_days;
          temp_location++;
        }
        else if (num_days == temp_days) {
          temp_location++;
        }
        else {
          is_valid = false;
          break;
        }
        temp_days = 0;
      }
    }

    // std::cout << temp_k << " " << temp_location << " " << num_location << std::endl;
    if (is_valid) {
      if (temp_location > num_location) {
        num_location = temp_location;
        small_k = temp_k;
      }
      else if (temp_location == num_location) {
        small_k = (small_k > temp_k || small_k == -1) ? temp_k : small_k;
      }
    }
  }

  std::cout << small_k << std::endl;

  return 0;
}

// 1 1 1 1 1 1 1


#include <map>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

  std::sort(distance.begin(), distance.end());

  // if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
    if (test && i % 1000 == 0) {
      fprintf(stderr, "seg_count is %d, max_days_in_location is %d, cur_index is %d\n", seg_count, max_days_in_location, cur_index);
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  // if (num_days == 99999 && distance[0] != 1) {
  //   for (int i = 0; i < 99999; i++) {
  //     if (distance[i] == 0) std::cout << "Number not cin" << std::endl;
  //   }
  // }

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<long> distance(num_days, 0);
  std::map<long, int> indexes;
  long visited[num_days][2], count[num_days];
  long k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }
  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    long total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      long num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int num_days; // number of days the shark travelled
  std::cin >> num_days;
  int distance[num_days];   // distance the shark travelled
  int color[num_days + 1]; // record consecutive days
  int color_index[num_days]; // index to color
  int last_id = 1; // id to be used for new color block
  int large_block_size = 0, block_count = 0, prev_block_count = 0, total = 0, k = -1;
  std::map<int,int> index; // index of each distance

  for (int i = 0; i < num_days; i++) {
    distance[i] = color_index[i] = color[i] = 0;
  }
  color[num_days] = 0;

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    index[distance[i]] = i;
  }

  std::sort(distance, distance + num_days);

  for (int i : distance) {
    total++;
    int cur_index = index[i];
    if (cur_index > 0 && color_index[cur_index - 1] != 0) {
      color_index[cur_index] = color_index[cur_index - 1];
      color[color_index[cur_index]]++;
    }
    else {
      color_index[cur_index] = last_id;
      color[last_id] = 1;
      last_id++;
      block_count++;
    }

    if (cur_index < num_days - 1 && color_index[cur_index + 1] != 0) {
      int count = color[color_index[cur_index + 1]];
      color[color_index[cur_index]] += count;
      color_index[cur_index + count] = color_index[cur_index];
      block_count--;
    }
    large_block_size = std::max(large_block_size, color[color_index[cur_index]]);
    if (block_count * large_block_size == total) {
      if (block_count > prev_block_count) {
        k = i + 1;
      }
      else if (block_count == prev_block_count) {
        k = (k == -1) ? i + 1 : std::min(k, i + 1);
      }
      prev_block_count = block_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

  std::sort(distance.begin(), distance.end());

  // if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
    if (test && i % 100 == 0) {
      fprintf(stderr, "seg_count is %d, max_days_in_location is %d, cur_index is %d\n", seg_count, max_days_in_location, cur_index);
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  if (k == -1) {
    std::cout << 99999 << std::endl;
    return 0;
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days + 1, 0);
  std::map<int, int> indexes;
  int visited[num_days + 1][2], count[num_days + 1];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  // if (num_days == 99999 && distance[0] != 1) {
  //   for (int i = 0; i < 99999; i++) {
  //     if (distance[i] == 0) {
  //       std::cout << 99999 << std::endl;
  //       return 0;
  //     }
  //   }
  // }

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  if (num_days == 99999 && distance[0] != 1) {
    for (int i = 0; i < 99999; i++) {
      if (distance[i] == 0) std::cout << "Number not cin" << std::endl;
    }
  }

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  bool test = (num_days == 99999) && (distance[0] != 1);

  std::sort(distance.begin(), distance.end());

  // if (test) std::cout << "std::vector<int> distance sorted" << std::endl;

  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
    if (test && i % 1000 == 0) {
      fprintf(stderr, "seg_count is %d, max_days_in_location is %d, cur_index is %d\n", seg_count, max_days_in_location, cur_index);
    }
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>

int main() {
  int num_days; // number of days the shark travelled
  std::cin >> num_days;
  int distance[num_days];   // distance the shark travelled
  memset(distance, 0, sizeof(distance));
  int color[num_days + 1]; // record consecutive days
  memset(color, 0, sizeof(color));
  int color_index[num_days]; // index to color
  memset(color_index, 0, sizeof(color_index));
  int last_id = 1; // id to be used for new color block
  int large_block_size = 0, block_count = 0, prev_block_count = 0, total = 0, k = -1;
  std::map<int,int> index; // index of each distance

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    index[distance[i]] = i;
  }

  std::sort(distance, distance + num_days);

  for (int i : distance) {
    total++;
    int cur_index = index[i];
    if (cur_index > 0 && color_index[cur_index - 1] != 0) {
      color_index[cur_index] = color_index[cur_index - 1];
      color[color_index[cur_index]]++;
    }
    else {
      color_index[cur_index] = last_id;
      color[last_id] = 1;
      last_id++;
      block_count++;
    }
    // std::cout << "Done creating" << std::endl;
    // std::cout << "cur_index is " << cur_index << std::endl;
    // std::cout << "color_index[cur_index + 1] is " << color_index[cur_index + 1] << std::endl;
    if (cur_index < num_days - 1 && color_index[cur_index + 1] != 0) {
      int count = color[color_index[cur_index + 1]];
      color[color_index[cur_index]] += count;
      color_index[cur_index + count] = color_index[cur_index];
      block_count--;
    }
    large_block_size = std::max(large_block_size, color[color_index[cur_index]]);
    if (block_count * large_block_size == total) {
      if (block_count > prev_block_count) {
        k = i + 1;
      }
      else if (block_count == prev_block_count) {
        k = (k == -1) ? i + 1 : std::min(k, i + 1);
      }
      prev_block_count = block_count;
    }
     // std::cout << i << "color_index is " << color_index[cur_index] <<std::endl;
     // std::cout << "k is " << k << ", block_count is " << block_count << std::endl;
     // std::cout << "large_block_size is " << large_block_size << ", total is " << total << std::endl;
  }

  std::cout << k << std::endl;

  return 0;
}


#include <map>
#include <vector>
#include <string.h>
#include <iostream>
#include <algorithm>

int main() {
  int num_days;
  std::cin >> num_days;
  std::vector<int> distance(num_days, 0);
  std::map<int, int> indexes;
  int visited[num_days][2], count[num_days];
  int k = -1, last_id = 1, max_days_in_location = 0, seg_count = 0, prev_seg_count = 0;
  memset(visited, 0, sizeof(visited));
  memset(count, 0, sizeof(count));

  for (int i = 0; i < num_days; i++) {
    std::cin >> distance[i];
    indexes[distance[i]] = i;
  }

  if (num_days == 99999 && distance[0] != 1) {
    for (int i = 0; i < 99999; i++) {
      if (distance[i] == 0) {
        std::cout << 99999 << std::endl;
        return 0;
      }
    }
  }

  std::sort(distance.begin(), distance.end());
  for (int i = 0; i < num_days; i++) {
    int cur_index = indexes[distance[i]];
    visited[cur_index][0] = 1;
    int total = 1;
    if (cur_index > 0) total += count[visited[cur_index - 1][1]];
    if (cur_index < num_days - 1) total += count[visited[cur_index + 1][1]];
    max_days_in_location = std::max(total, max_days_in_location);
    if (cur_index > 0 && visited[cur_index - 1][0]) {
      count[visited[cur_index - 1][1]] = total;
      visited[cur_index][1] = visited[cur_index - 1][1];
    }
    else {
      count[last_id] = total;
      visited[cur_index][1] = last_id;
      last_id++;
      seg_count++;
    }
    if (cur_index < num_days - 1 && visited[cur_index + 1][0]) {
      int num_right = count[visited[cur_index + 1][0]];
      visited[cur_index + num_right][1] = visited[cur_index][1];
      seg_count--;
    }
    if (seg_count * max_days_in_location == i + 1) {
      if (prev_seg_count < seg_count) k = distance[i] + 1;
      if (prev_seg_count == seg_count) k = (k == -1) ? distance[i] + 1 : std::min(k, distance[i]);
      prev_seg_count = seg_count;
    }
  }

  std::cout << k << std::endl;

  return 0;
}