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

public class Euler271 {

    static List<Long> distinctPrimeFactors(long n) {
        List<Long> factors = new ArrayList<>();
        if ((n & 1) == 0) {
            factors.add(2L);
            while ((n & 1) == 0)
                n >>= 1;
        }
        for (long p = 3; p * p <= n; p += 2) {
            if (n % p == 0) {
                factors.add(p);
                while (n % p == 0)
                    n /= p;
            }
        }
        if (n > 1)
            factors.add(n);
        return factors;
    }

    static long[] extendedGcd(long a, long b) {
        if (b == 0)
            return new long[] { a, 1, 0 };
        long[] res = extendedGcd(b, a % b);
        long g = res[0];
        long x1 = res[1];
        long y1 = res[2];
        long x = y1;
        long y = x1 - (a / b) * y1;
        return new long[] { g, x, y };
    }

    static long modInverse(long a, long mod) {
        long[] res = extendedGcd(a, mod);
        if (res[0] != 1)
            return 0;
        long r = res[1] % mod;
        if (r < 0)
            r += mod;
        return r;
    }

    static long modPow(long base, long exp, long mod) {
        long res = 1 % mod;
        long cur = base % mod;
        while (exp > 0) {
            if ((exp & 1) == 1)
                res = multiplyMod(res, cur, mod);
            cur = multiplyMod(cur, cur, mod);
            exp >>= 1;
        }
        return res;
    }

    static long multiplyMod(long a, long b, long mod) {
        return BigInteger.valueOf(a).multiply(BigInteger.valueOf(b)).mod(BigInteger.valueOf(mod)).longValue();
    }

    static long crtPair(long a1, long m1, long a2, long m2) {
        long inv = modInverse(m1 % m2, m2);
        long diff = (a2 - (a1 % m2)) % m2;
        if (diff < 0)
            diff += m2;
        long t = multiplyMod(diff, inv, m2);
        return a1 + m1 * t;
    }

    static long sum = 0;
    static List<Long> primes;
    static List<List<Long>> rootsByPrime;
    static long N;

    static void dfs(int idx, long residue, long modulus) {
        if (idx == primes.size()) {
            if (residue > 1 && residue < N) {
                sum += residue;
            }
            return;
        }
        long p = primes.get(idx);
        for (long root : rootsByPrime.get(idx)) {
            long nextResidue = crtPair(residue, modulus, root, p);
            dfs(idx + 1, nextResidue, modulus * p);
        }
    }

    static long solve(long n) {
        N = n;
        primes = distinctPrimeFactors(n);
        rootsByPrime = new ArrayList<>();
        sum = 0;

        for (long p : primes) {
            List<Long> roots = new ArrayList<>();
            for (long x = 1; x < p; x++) {
                if (modPow(x, 3, p) == 1) {
                    roots.add(x);
                }
            }
            rootsByPrime.add(roots);
        }

        dfs(0, 0, 1);
        return sum;
    }

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