import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class Euler974 {

    static class Counts implements Comparable<Counts> {
        int[] c = new int[5];

        Counts() {
        }

        Counts(int[] counts) {
            System.arraycopy(counts, 0, this.c, 0, 5);
        }

        @Override
        public int compareTo(Counts o) {
            for (int i = 0; i < 5; ++i) {
                if (c[i] != o.c[i])
                    return Integer.compare(c[i], o.c[i]);
            }
            return 0;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (!(obj instanceof Counts))
                return false;
            Counts other = (Counts) obj;
            for (int i = 0; i < 5; ++i) {
                if (c[i] != other.c[i])
                    return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 0;
            for (int val : c) {
                hash = hash * 31 + val;
            }
            return hash;
        }
    }

    static final int[] DIGITS = { 1, 3, 5, 7, 9 };
    static Map<Counts, long[]> memo = new ConcurrentHashMap<>();
    static long[] pow10mod7 = new long[50];

    static void initPow() {
        pow10mod7[0] = 1;
        for (int i = 1; i < 50; ++i) {
            pow10mod7[i] = (pow10mod7[i - 1] * 10) % 7;
        }
    }

    static long[] getRemCounts(Counts counts) {
        int totalLen = 0;
        for (int x : counts.c)
            totalLen += x;

        if (totalLen == 0) {
            long[] res = new long[7];
            res[0] = 1;
            return res;
        }

        long[] cached = memo.get(counts);
        if (cached != null) {
            return cached;
        }

        long[] totalRes = new long[7];
        int power = (int) pow10mod7[totalLen - 1];

        for (int i = 0; i < 5; ++i) {
            if (counts.c[i] > 0) {
                Counts nextCounts = new Counts(counts.c);
                nextCounts.c[i]--;

                long[] subRes = getRemCounts(nextCounts);
                int digit = DIGITS[i];
                int currentRemContribution = (digit * power) % 7;

                for (int r = 0; r < 7; ++r) {
                    if (subRes[r] > 0) {
                        int newRem = (currentRemContribution + r) % 7;
                        totalRes[newRem] += subRes[r];
                    }
                }
            }
        }

        memo.put(counts, totalRes);
        return totalRes;
    }

    static long processTuples(List<Counts> tuples) {
        long count = 0;
        for (Counts c : tuples) {
            long sumDigits = 0;
            for (int i = 0; i < 5; ++i)
                sumDigits += (long) c.c[i] * DIGITS[i];
            if (sumDigits % 3 != 0)
                continue;

            if (c.c[2] < 1)
                continue;

            Counts remaining = new Counts(c.c);
            remaining.c[2]--;

            long[] rems = getRemCounts(remaining);
            count += rems[3];
        }
        return count;
    }

    static void generatePartitions(int index, int remainingLen, int[] current, List<Counts> result) {
        if (index == 5) {
            if (remainingLen == 0) {
                result.add(new Counts(current));
            }
            return;
        }

        for (int k = 1; k <= remainingLen; k += 2) {
            current[index] = k;
            if (remainingLen - k >= (4 - index)) {
                generatePartitions(index + 1, remainingLen - k, current, result);
            }
        }
    }

    static String findNth(long N) {
        long currentCount = 0;

        for (int L = 5;; L += 2) {
            List<Counts> allTuples = new ArrayList<>();
            int[] current = new int[5];
            generatePartitions(0, L, current, allTuples);

            long countForL = processTuples(allTuples);

            if (currentCount + countForL >= N) {
                StringBuilder res = new StringBuilder();
                long targetInL = N - currentCount;

                int[] currentUsed = new int[5];
                int currentRem = 0;

                for (int pos = 0; pos < L - 1; ++pos) {
                    int power = (int) pow10mod7[L - 1 - pos];

                    boolean accepted = false;
                    for (int dIdx = 0; dIdx < 5; ++dIdx) {
                        int d = DIGITS[dIdx];
                        long ways = 0;

                        currentUsed[dIdx]++;
                        int nextRem = (currentRem + d * power) % 7;

                        long term = (nextRem + 5) % 7;
                        int neededSuffixRem = (int) ((((-term * 5) % 7) + 7) % 7);

                        for (Counts T : allTuples) {
                            long s = 0;
                            for (int k = 0; k < 5; ++k)
                                s += (long) T.c[k] * DIGITS[k];
                            if (s % 3 != 0)
                                continue;

                            boolean possible = true;
                            Counts suffixCounts = new Counts();
                            for (int k = 0; k < 5; ++k) {
                                int needed = T.c[k] - currentUsed[k];
                                if (k == 2)
                                    needed--;
                                if (needed < 0) {
                                    possible = false;
                                    break;
                                }
                                suffixCounts.c[k] = needed;
                            }
                            if (!possible)
                                continue;

                            long[] cRems = getRemCounts(suffixCounts);
                            ways += cRems[neededSuffixRem];
                        }

                        if (targetInL <= ways) {
                            res.append(d);
                            currentRem = nextRem;
                            accepted = true;
                            break;
                        } else {
                            targetInL -= ways;
                            currentUsed[dIdx]--;
                        }
                    }
                    if (!accepted)
                        return "ERROR";
                }

                res.append('5');
                return res.toString();

            } else {
                currentCount += countForL;
            }
        }
    }

    public static String solve() {
        return findNth(10_000_000_000_000_000L);
    }

    public static void main(String[] args) {
        initPow();
        if (!findNth(1).equals("1117935")) {
            System.out.println("Validation failed");
            return;
        }
        System.out.println(solve());
    }
}
