#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;
}
I3ByYWdtYSBjb21tZW50KGxpbmtlciwgIi9zdGFjazoyMDAwMDAwMDAiKQoKCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBlbmRsICJcbiIKI2RlZmluZSByZXAoaSxuKSBmb3IoaW50IGk9MDtpPG47aSsrKQoKCmludCBzb2x2ZShzdHJpbmcgcyxzdHJpbmcgdCkKewoJcXVldWU8c3RyaW5nPnE7CglxLnB1c2gocyk7Cgl1bm9yZGVyZWRfbWFwPHN0cmluZyxpbn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