import java.util.*; public class Exercise110{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String[] v = new String[n]; for(int i = 0; i < n; i++){ v[i] = sc.next(); } long decimal = Long.MAX_VALUE; for(int i = 0; i < v.length; i++){ char[] num = v[i].toCharArray(); int al = 0; for(int j = 0; j < num.length; j++){ al = Math.max(changeToInt(num[j]) + 1, al); } long d = 0; for(int j = 0; j < num.length; j++){ d += changeToInt(num[num.length - 1 - j]) * (long)Math.pow(al, j); } decimal = Math.min(decimal, d); } System.out.println(decimal); } private static int changeToInt(char a){ char[] alphabet = new char[26]; for(int i = 0; i < alphabet.length; i++){ alphabet[i] = (char)(i + 65); } if(Character.isDigit(a)){ return Character.getNumericValue(a); }else{ return Arrays.binarySearch(alphabet, a) + 10; } } }