#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair pi; typedef pair pl; typedef pair pls; map m; int main() { int N; cin >> N; for (char c = 'A',i = 10; c <= 'Z'; c++,i++) { m[c] = i; } ll ans = (ll)1e20; for (int i = 0; i < N; i++) { string s; cin >> s; int mx = 0; for (int j = 0; j < s.size(); j++) { if (s[j] >= '0' && s[j] <= '9') { if (mx < s[j] - '0') mx = s[j] - '0'; } else { if (m[s[j]] > mx) mx = m[s[j]]; } } ll sum = 0; for (int j = s.size() - 1, i = 0; j >= 0; j--, i++) { if (s[j] >= '0' && s[j] <= '9') sum += (ll)pow(mx + 1, i)*(s[j] - '0'); else sum += (ll)pow(mx + 1, i)*(m[s[j]]); } ans = min(ans,sum); } cout << ans << endl; return 0; }