import java.util.*;

class Euler52 {

    private static class Options {
        int maxMultiplier = 6;
        boolean runCheckpoints = true;
    }

    private static boolean parseIntAfterPrefix(String arg, String prefix, Options options) {
        if (!arg.startsWith(prefix)) {
            return false;
        }
        String tail = arg.substring(prefix.length());
        if (tail.isEmpty()) {
            return false;
        }

        int parsed = 0;
        for (int i = 0; i < tail.length(); i++) {
            char c = tail.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
            parsed = parsed * 10 + (c - '0');
        }
        options.maxMultiplier = parsed;
        return true;
    }

    private static boolean parseArguments(String[] args, Options options) {
        for (String arg : args) {
            if ("--skip-checkpoints".equals(arg)) {
                options.runCheckpoints = false;
                continue;
            }
            if (parseIntAfterPrefix(arg, "--max-multiplier=", options)) {
                continue;
            }

            System.err.println("Unknown argument: " + arg);
            return false;
        }
        return options.maxMultiplier >= 2;
    }

    private static String sortedDigits(int value) {
        char[] chars = Integer.toString(value).toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }

    private static int solve(int maxMultiplier) {
        for (int x = 1; ; ++x) {
            String signature = sortedDigits(x);
            boolean ok = true;
            for (int k = 2; k <= maxMultiplier; ++k) {
                if (!sortedDigits(k * x).equals(signature)) {
                    ok = false;
                    break;
                }
            }
            if (ok) {
                return x;
            }
        }
    }

    private static boolean runCheckpoints() {
        if (!sortedDigits(125874).equals(sortedDigits(251748))) {
            System.err.println("Checkpoint failed for 125874");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Options options = new Options();
        if (!parseArguments(args, options)) {
            System.exit(1);
        }
        if (options.runCheckpoints && !runCheckpoints()) {
            System.exit(2);
        }

        System.out.println(solve(options.maxMultiplier));
    }
}
