import java.util.function.BiConsumer

class DeBruijn {
    interface Recursable<T, U> {
        void apply(T t, U u, Recursable<T, U> r);
    }

    static <T, U> BiConsumer<T, U> recurse(Recursable<T, U> f) {
        return { t, u -> f.apply(t, u, f) }
    }

    private static String deBruijn(int k, int n) {
        byte[] a = new byte[k * n]
        Arrays.fill(a, (byte) 0)

        List<Byte> seq = new ArrayList<>()

        BiConsumer<Integer, Integer> db = recurse({ int t, int p, f ->
            if (t > n) {
                if (n % p == 0) {
                    for (int i = 1; i < p + 1; ++i) {
                        seq.add(a[i])
                    }
                }
            } else {
                a[t] = a[t - p]
                f.apply(t + 1, p, f)
                int j = a[t - p] + 1
                while (j < k) {
                    a[t] = (byte) (j & 0xFF)
                    f.apply(t + 1, t, f)
                    j++
                }
            }
        })
        db.accept(1, 1)

        StringBuilder sb = new StringBuilder()
        for (Byte i: seq) {
            sb.append("0123456789".charAt(i))
        }

        sb.append(sb.subSequence(0, n - 1))
        return sb.toString()
    }

    private static boolean allDigits(String s) {
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i)
            if (!Character.isDigit(c)) {
                return false
            }
        }
        return true
    }

    private static void validate(String db) {
        int le = db.length()
        int[] found = new int[10_000]
        Arrays.fill(found, 0)
        List<String> errs = new ArrayList<>()