#include<bits/stdc++.h>
#define char long long
using namespace std;
char a,b[51],c,d,l,o;
int main()
{
cin>>a;
for(int i=0;i<a;i++)
{
cin>>b[i];
l++;
}
cin>>c>>d;
do{
for(int i=0;i<l;i++)
{
if(b[i]>=d and b[i]<=c)
{
cout<<0;return 0;
}
if(b[i]>c)
{
b[i+1]++;
o++;
}
else if(b[i]<c)
{
b[i+1]--;
o++;
}
}
}while(b[0]>=d and b[0]<=c and b[l]>=d and b[l]<=c);
if(o==0)cout<<-1;
cout<<o;
}