import java.nio.file.*;
import java.util.*;

public class Euler98 {
    public static void main(String[] args) throws Exception {
        String content = Files.readString(Path.of("resources/documents/0098_words.txt")).trim();
        String[] words = content.split(",");
        for (int i = 0; i < words.length; i++)
            words[i] = words[i].replaceAll("\"", "");
        Map<String, List<String>> groups = new HashMap<>();
        for (String w : words) {
            char[] c = w.toCharArray();
            Arrays.sort(c);
            String k = new String(c);
            groups.computeIfAbsent(k, x -> new ArrayList<>()).add(w);
        }
        List<String[]> pairs = new ArrayList<>();
        int maxLen = 0;
        for (List<String> ws : groups.values())
            if (ws.size() >= 2) {
                for (int i = 0; i < ws.size(); i++)
                    for (int j = i + 1; j < ws.size(); j++) {
                        pairs.add(new String[] { ws.get(i), ws.get(j) });
                        maxLen = Math.max(maxLen, ws.get(i).length());
                    }
            }
        Map<Integer, List<String>> sqByLen = new HashMap<>();
        for (long n = 1; Long.toString(n * n).length() <= maxLen; n++)
            sqByLen.computeIfAbsent(Long.toString(n * n).length(), k -> new ArrayList<>()).add(Long.toString(n * n));
        long best = 0;
        for (String[] pair : pairs) {
            int len = pair[0].length();
            List<String> sqs = sqByLen.getOrDefault(len, Collections.emptyList());
            for (String sq : sqs) {
                Map<Character, Character> m = new HashMap<>();
                Map<Character, Character> r = new HashMap<>();
                boolean ok = true;
                for (int i = 0; i < len; i++) {
                    char c = pair[0].charAt(i), d = sq.charAt(i);
                    if (m.containsKey(c)) {
                        if (m.get(c) != d) {
                            ok = false;
                            break;
                        }
                    } else {
                        if (r.containsKey(d)) {
                            if (r.get(d) != c) {
                                ok = false;
                                break;
                            }
                        }
                        m.put(c, d);
                        r.put(d, c);
                    }
                }
                if (!ok)
                    continue;
                StringBuilder sb = new StringBuilder();
                for (char c : pair[1].toCharArray())
                    sb.append(m.get(c));
                String mapped = sb.toString();
                if (mapped.charAt(0) == '0')
                    continue;
                long val = Long.parseLong(mapped);
                long rt = (long) Math.sqrt(val);
                if (rt * rt == val)
                    best = Math.max(best, Math.max(Long.parseLong(sq), val));
            }
        }
        System.out.println(best);
    }
}
