#include using namespace std; main(){ int t;cin>>t; long long ans=-1; while(t--){ string s;cin>>s; long long z=0,q=1;int mx=2,i=0; for(;s.size()>i;i++)mx=max(mx,s[i]-(s[i]<='9'?47:54));--i; for(;0<=i;i--)z+=q*(s[i]-(s[i]<='9'?48:55)),q*=mx; ans=min(z,(ans==-1?z:ans)); } cout << ans << endl; }