#pragma comment(linker, "/stack:200000000")


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

#define ll long long
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)


int solve(string s,string t)
{
	queue<string>q;
	q.push(s);
	unordered_map<string,int>level;
	level[s]=0;
	int l = s.size();
	while(!q.empty())
	{
		string cur =  q.front();
		q.pop();
		for(int i=0;i<l;i++)
		{
			string first ="";
			if(i!=0) first = cur.substr(0,i);
			string tmp = cur.substr(i,1);
			for(int j=i+1;j<l;j++)
			{
				tmp+=cur[j];
				reverse(tmp.begin(),tmp.end());
				string newStr = first+tmp;
				//cout<<newStr<<" ||\n";
				if(newStr.size()<l)
				{
					newStr+=cur.substr(j+1,10-j);
				}
				if(level.find(newStr)==level.end())
				{
					level[newStr] = level[cur]+1;
					q.push(newStr);
				}
				if(newStr==t)return level[cur]+1;
				//cout<<newStr<<endl;
				reverse(tmp.begin(),tmp.end());
			}
		}
	}
	return -1;
}


int main()
{
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	string src,tar;
	while(cin>>tar>>src)
	{
		if(src=="*" && tar=="*")break;
		if(src==tar)cout<<"0\n";
		for(int i=0;i<10;i++)
		{
			if(src[i]!=tar[i])
			{
				string s="",t="";
				for(int j=i;j<10;j++)s+=src[j],t+=tar[j];
				cout<<solve(s,t)<<"\n";
				break;
			}
		}
	}
	return 0;
}