import java.util.PriorityQueue;

public class Euler665 {

    static final int kNone = Integer.MIN_VALUE;

    static class SuccessorSet {
        boolean empty = true;
        int lo = 0;
        int hi = -1;
        int[] parent;

        int nextFree(int value) {
            if (empty || value < lo || value > hi) {
                return value;
            }
            int idx = value - lo;
            int slot = parent[idx];
            if (slot == kNone) {
                return value;
            }

            int root = value;
            while (true) {
                int rIdx = root - lo;
                if (root < lo || root > hi)
                    break;
                int rSlot = parent[rIdx];
                if (rSlot == kNone)
                    break;
                root = rSlot;
            }

            int curr = value;
            while (curr != root) {
                int cIdx = curr - lo;
                int nxt = parent[cIdx];
                parent[cIdx] = root;
                curr = nxt;
            }
            return root;
        }

        void insert(int value) {
            ensureContains(value);
            int idx = value - lo;
            int slot = parent[idx];
            if (slot != kNone) {
                return;
            }
            parent[idx] = nextFree(value + 1);
        }

        void ensureContains(int value) {
            if (empty) {
                lo = value;
                hi = value;
                parent = new int[] { kNone };
                empty = false;
                return;
            }
            if (value >= lo && value <= hi) {
                return;
            }

            int newLo = lo;
            int newHi = hi;
            while (value < newLo || value > newHi) {
                int span = newHi - newLo + 1;
                if (value < newLo)
                    newLo -= span;
                if (value > newHi)
                    newHi += span;
            }

            int[] expanded = new int[newHi - newLo + 1];
            for (int i = 0; i < expanded.length; ++i)
                expanded[i] = kNone;
            int shift = lo - newLo;
            System.arraycopy(parent, 0, expanded, shift, parent.length);
            parent = expanded;
            lo = newLo;
            hi = newHi;
        }
    }

    static class Event implements Comparable<Event> {
        int activateX;
        int tValue;
        int sValue;

        Event(int activateX, int tValue, int sValue) {
            this.activateX = activateX;
            this.tValue = tValue;
            this.sValue = sValue;
        }

        @Override
        public int compareTo(Event o) {
            return Integer.compare(this.activateX, o.activateX);
        }
    }

    static long solveCase(int M) {
        SuccessorSet used = new SuccessorSet();
        SuccessorSet difSet = new SuccessorSet();
        SuccessorSet lowSet = new SuccessorSet();
        SuccessorSet highEvenSet = new SuccessorSet();
        SuccessorSet highOddSet = new SuccessorSet();
        PriorityQueue<Event> pending = new PriorityQueue<>();

        class Helper {
            void insertS(int s) {
                if ((s & 1) == 0) {
                    highEvenSet.insert(s / 2);
                } else {
                    highOddSet.insert((s - 1) / 2);
                }
            }

            void activateEvents(int x) {
                while (!pending.isEmpty() && pending.peek().activateX <= x) {
                    Event e = pending.poll();
                    lowSet.insert(e.tValue);
                    insertS(e.sValue);
                }
            }
        }
        Helper helper = new Helper();

        difSet.insert(0);
        lowSet.insert(0);
        helper.insertS(0);

        long total = 0;
        int x = 1;

        while (x <= M) {
            helper.activateEvents(x);
            x = used.nextFree(x);
            if (x > M)
                break;

            int y = x + 1;
            while (true) {
                int y1 = used.nextFree(y);
                int y2 = x + difSet.nextFree(y1 - x);
                int y3 = 2 * x + lowSet.nextFree(y2 - 2 * x);

                int shift = ((x & 1) == 0) ? (x / 2) : (x / 2 + 1);
                int y4;
                if ((x & 1) == 0) {
                    y4 = shift + highEvenSet.nextFree(y3 - shift);
                } else {
                    y4 = shift + highOddSet.nextFree(y3 - shift);
                }

                if (y4 == y)
                    break;
                y = y4;
            }

            used.insert(x);
            used.insert(y);

            if (x + y <= M) {
                total += (long) (x + y);
            }

            difSet.insert(y - x);
            lowSet.insert(y - 2 * x);
            helper.insertS(2 * y - x);
            pending.offer(new Event(y + 1, x - 2 * y, 2 * x - y));

            ++x;
        }

        return total;
    }

    public static String solve() {
        long ans = solveCase(10000000);
        return Long.toString(ans);
    }

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