import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Euler963 {

    static final int K_MAX_K = 24;
    static final long K_SCALE = 1L << 20;

    static class ValueKey {
        long fScaled;
        int star;
        short[] up;

        ValueKey() {
            this.fScaled = 0;
            this.star = 0;
            this.up = new short[K_MAX_K + 1];
        }

        ValueKey(long fScaled, int star, short[] up) {
            this.fScaled = fScaled;
            this.star = star;
            this.up = up;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            ValueKey valueKey = (ValueKey) o;
            return fScaled == valueKey.fScaled &&
                    star == valueKey.star &&
                    Arrays.equals(up, valueKey.up);
        }

        @Override
        public int hashCode() {
            long h = 1469598103934665603L;
            h ^= fScaled;
            h *= 1099511628211L;
            h ^= star;
            h *= 1099511628211L;
            for (short c : up) {
                h ^= c;
                h *= 1099511628211L;
            }
            return (int) (h ^ (h >>> 32));
        }

        public ValueKey copy() {
            return new ValueKey(this.fScaled, this.star, Arrays.copyOf(this.up, this.up.length));
        }
    }

    static ValueKey addValues(ValueKey a, ValueKey b) {
        short[] newUp = new short[K_MAX_K + 1];
        for (int i = 0; i <= K_MAX_K; ++i) {
            newUp[i] = (short) (a.up[i] + b.up[i]);
        }
        return new ValueKey(a.fScaled + b.fScaled, a.star ^ b.star, newUp);
    }

    static ValueKey valueForZeroFractionCase(int n3) {
        ValueKey out = new ValueKey();
        int zerosToRight = 0;
        int x = n3;
        while (x > 0) {
            int d = x % 3;
            if (d == 0) {
                ++zerosToRight;
            } else {
                out.star ^= 1;
                if (zerosToRight <= K_MAX_K) {
                    out.up[zerosToRight]++;
                }
            }
            x /= 3;
        }
        return out;
    }

    static ValueKey[] buildValues(int limit) {
        ValueKey[] values = new ValueKey[limit + 1];
        values[0] = new ValueKey();

        for (int n = 1; n <= limit; ++n) {
            int q = n / 3;
            int r = n % 3;
            ValueKey cur = values[q].copy();

            if (r == 1) {
                cur.fScaled -= K_SCALE;
            } else if (r == 2) {
                cur.star ^= 1;
            } else {
                if (cur.fScaled <= -2 * K_SCALE) {
                    cur.fScaled += K_SCALE;
                } else if (cur.fScaled < 0) {
                    cur.fScaled /= 2;
                } else {
                    cur = valueForZeroFractionCase(n);
                }
            }
            values[n] = cur;
        }
        return values;
    }

    static String solveFast(int limit) {
        ValueKey[] values = buildValues(limit);

        Map<ValueKey, Long> freq = new HashMap<>();
        for (int n = 1; n <= limit; ++n) {
            freq.put(values[n], freq.getOrDefault(values[n], 0L) + 1L);
        }

        List<ValueKey> uniq = new ArrayList<>(freq.keySet());
        long[] f = new long[uniq.size()];
        for (int i = 0; i < uniq.size(); i++) {
            f[i] = freq.get(uniq.get(i));
        }

        Map<ValueKey, Long> pairCount = new HashMap<>();

        for (int i = 0; i < uniq.size(); ++i) {
            long fi = f[i];
            ValueKey sSelf = addValues(uniq.get(i), uniq.get(i));
            pairCount.put(sSelf, pairCount.getOrDefault(sSelf, 0L) + fi * (fi + 1L) / 2L);

            for (int j = i + 1; j < uniq.size(); ++j) {
                ValueKey s = addValues(uniq.get(i), uniq.get(j));
                pairCount.put(s, pairCount.getOrDefault(s, 0L) + fi * f[j]);
            }
        }

        java.math.BigInteger ans = java.math.BigInteger.ZERO;
        for (long c : pairCount.values()) {
            java.math.BigInteger bc = java.math.BigInteger.valueOf(c);
            ans = ans.add(bc.multiply(bc));
        }
        return ans.toString();
    }

    public static String solve() {
        return solveFast(100000);
    }

    public static void main(String[] args) {
        if (!solveFast(5).equals("21")) {
            System.out.println("Validation failed");
            return;
        }
        System.out.println(solve());
    }
}
