import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Euler542 {

    static class Candidate {
        long n;
        long d;
        long relevance;

        Candidate(long n, long d, long relevance) {
            this.n = n;
            this.d = d;
            this.relevance = relevance;
        }
    }

    static long powWithCap(long base, int exp, long cap) {
        BigInteger val = BigInteger.ONE;
        BigInteger b = BigInteger.valueOf(base);
        BigInteger c = BigInteger.valueOf(cap);
        for (int i = 0; i < exp; i++) {
            val = val.multiply(b);
            if (val.compareTo(c) > 0) {
                return cap + 1;
            }
        }
        return val.longValue();
    }

    static long intRoot(long n, int p) {
        long lo = 1;
        long hi = 2;
        while (powWithCap(hi, p, n) <= n) {
            if (hi > n / 2) {
                hi = n;
                break;
            }
            hi *= 2;
        }
        while (lo + 1 < hi) {
            long mid = lo + (hi - lo) / 2;
            if (powWithCap(mid, p, n) <= n) {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        return lo;
    }

    static long deltaValue(long u, int p) {
        long v = u - 1;
        BigInteger D = BigInteger.ONE;
        BigInteger vpow = BigInteger.ONE;
        BigInteger bigU = BigInteger.valueOf(u);
        BigInteger bigV = BigInteger.valueOf(v);
        for (int m = 2; m <= p + 1; m++) {
            vpow = vpow.multiply(bigV);
            D = bigU.multiply(D).add(vpow);
        }
        return D.longValue();
    }

    static int maxPowerForLimit(long n) {
        int p = 0;
        long x = 1;
        while (x <= n / 2) {
            x *= 2;
            p++;
        }
        return p;
    }

    static List<Candidate> generateRecordCandidates(long limit) {
        if (limit < 4)
            return new ArrayList<>();

        int pMax = maxPowerForLimit(limit);
        long[] uMax = new long[pMax + 1];
        for (int p = 2; p <= pMax; p++) {
            uMax[p] = intRoot(limit, p);
        }

        List<Candidate> records = new ArrayList<>();
        long currentBestD = 0;

        while (true) {
            boolean found = false;
            long bestN = 0;
            long bestD = 0;

            for (int p = 2; p <= pMax; p++) {
                long umax = uMax[p];
                if (umax < 2)
                    continue;
                if (deltaValue(umax, p) <= currentBestD)
                    continue;

                long lo = 2;
                long hi = umax;
                while (lo < hi) {
                    long mid = lo + (hi - lo) / 2;
                    if (deltaValue(mid, p) > currentBestD) {
                        hi = mid;
                    } else {
                        lo = Math.min(umax, mid + 1); // Avoid endless loops with mid+1 properly bounded
                    }
                }

                long u = lo;
                long n = powWithCap(u, p, limit);
                long d = deltaValue(u, p);
                if (!found || n < bestN || (n == bestN && d > bestD)) {
                    found = true;
                    bestN = n;
                    bestD = d;
                }
            }

            if (!found)
                break;

            records.add(new Candidate(bestN, bestD, limit));
            currentBestD = bestD;
        }

        return records;
    }

    static void computeRelevanceBounds(List<Candidate> candidates, long limit) {
        int n = candidates.size();
        for (int i = 0; i < n; i++) {
            long best = limit;
            long ni = candidates.get(i).n;
            long di = candidates.get(i).d;

            for (int j = 0; j < n; j++) {
                long nj = candidates.get(j).n;
                long dj = candidates.get(j).d;

                BigInteger lhs = BigInteger.valueOf(dj).multiply(BigInteger.valueOf(ni));
                BigInteger rhs = BigInteger.valueOf(di).multiply(BigInteger.valueOf(nj));
                if (lhs.compareTo(rhs) <= 0)
                    continue;

                BigInteger num = BigInteger.valueOf(dj).multiply(BigInteger.valueOf(nj))
                        .multiply(BigInteger.valueOf(ni));
                BigInteger den = BigInteger.valueOf(dj).multiply(BigInteger.valueOf(ni))
                        .subtract(BigInteger.valueOf(di).multiply(BigInteger.valueOf(nj)));

                BigInteger r = num.divide(den);
                if (r.compareTo(BigInteger.valueOf(best)) < 0) {
                    best = r.longValue();
                }
            }
            candidates.get(i).relevance = best;
        }
    }

    static List<Long> collectEvents(List<Candidate> candidates, long limit) {
        List<Long> events = new ArrayList<>();

        for (Candidate c : candidates) {
            long lim = Math.min(limit, c.relevance);
            if (lim < c.n)
                continue;

            long t = c.n;
            while (t <= lim) {
                events.add(t);
                if (t > lim - c.n)
                    break;
                t += c.n;
            }
        }

        Collections.sort(events);
        List<Long> uniqueEvents = new ArrayList<>();
        if (!events.isEmpty()) {
            uniqueEvents.add(events.get(0));
            for (int i = 1; i < events.size(); i++) {
                if (!events.get(i).equals(events.get(i - 1))) {
                    uniqueEvents.add(events.get(i));
                }
            }
        }
        return uniqueEvents;
    }

    static int alternatingSignSum(long l, long r) {
        if (l > r)
            return 0;
        long len = r - l + 1;
        if ((len & 1) == 0)
            return 0;
        return (l & 1) != 0 ? -1 : 1;
    }

    static class Solver {
        long limit;
        List<Candidate> candidates;
        List<Long> events;

        Solver(long limit) {
            this.limit = limit;
            this.candidates = generateRecordCandidates(limit);
            computeRelevanceBounds(this.candidates, limit);
            this.events = collectEvents(this.candidates, limit);
        }

        long S(long k) {
            if (k < 4)
                return 0;
            long best = 0;
            for (Candidate c : candidates) {
                if (c.n > k)
                    break;
                if (k > c.relevance)
                    continue;
                long val = (k / c.n) * c.d;
                if (val > best) {
                    best = val;
                }
            }
            return best;
        }

        BigInteger T(long n) {
            if (n < 4)
                return BigInteger.ZERO;

            BigInteger ans = BigInteger.ZERO;
            long current = 4;
            long sValue = S(4);
            int eventIdx = 0;

            while (current <= n) {
                long nextEvent = (eventIdx < events.size() && events.get(eventIdx) <= n) ? events.get(eventIdx)
                        : (n + 1);

                if (nextEvent > current) {
                    long r = Math.min(n, nextEvent - 1);
                    int sign = alternatingSignSum(current, r);
                    if (sign != 0) {
                        if (sign == 1) {
                            ans = ans.add(BigInteger.valueOf(sValue));
                        } else {
                            ans = ans.subtract(BigInteger.valueOf(sValue));
                        }
                    }
                    current = r + 1;
                    if (current > n)
                        break;
                }

                sValue = S(current);
                eventIdx++;
            }

            return ans;
        }
    }

    public static String solve() {
        Solver solver = new Solver(100000000000000000L);
        return solver.T(100000000000000000L).toString();
    }

    public static void main(String[] args) {
        System.out.println(solve());
    }
}
