import java.util.*;

public class Euler349 {

    static long encodeCell(int x, int y) {
        return ((long) x << 32) | (y & 0xFFFFFFFFL);
    }

    static long[] simulateBlackCounts(int steps) {
        long[] counts = new long[steps + 1];
        Set<Long> black = new HashSet<>(steps / 2 + 1024);

        int x = 0;
        int y = 0;
        int direction = 0;
        int[] dx = { 0, 1, 0, -1 };
        int[] dy = { 1, 0, -1, 0 };
        long blackCount = 0;

        for (int step = 1; step <= steps; step++) {
            long key = encodeCell(x, y);
            if (black.contains(key)) {
                black.remove(key);
                blackCount--;
                direction = (direction + 3) & 3;
            } else {
                black.add(key);
                blackCount++;
                direction = (direction + 1) & 3;
            }
            x += dx[direction];
            y += dy[direction];
            counts[step] = blackCount;
        }
        return counts;
    }

    static class Pattern {
        boolean found;
        int start;
        int period;
        long delta;

        Pattern(boolean found, int start, int period, long delta) {
            this.found = found;
            this.start = start;
            this.period = period;
            this.delta = delta;
        }
    }

    static Pattern detectLinearPattern(long[] counts, int maxPeriod, int verifyWindow) {
        int totalSteps = counts.length - 1;
        if (totalSteps < 2)
            return new Pattern(false, 0, 0, 0);

        int windowStart = (totalSteps > verifyWindow) ? (totalSteps - verifyWindow) : 0;

        for (int period = 1; period <= maxPeriod && period <= totalSteps; period++) {
            long delta = counts[totalSteps] - counts[totalSteps - period];
            boolean tailOk = true;
            for (int t = windowStart; t + period <= totalSteps; t++) {
                if (counts[t + period] - counts[t] != delta) {
                    tailOk = false;
                    break;
                }
            }
            if (!tailOk)
                continue;

            int upto = totalSteps - period;
            byte[] good = new byte[upto + 1];
            for (int t = 0; t <= upto; t++) {
                good[t] = (counts[t + period] - counts[t] == delta) ? (byte) 1 : (byte) 0;
            }

            byte[] suffixOk = new byte[upto + 2];
            suffixOk[upto + 1] = 1;
            for (int t = upto; t >= 0; t--) {
                suffixOk[t] = (byte) ((good[t] == 1 && suffixOk[t + 1] == 1) ? 1 : 0);
            }

            for (int start = 0; start <= upto; start++) {
                if (suffixOk[start] == 1) {
                    return new Pattern(true, start, period, delta);
                }
            }
        }
        return new Pattern(false, 0, 0, 0);
    }

    static long extrapolateBlackCount(long[] counts, Pattern pattern, long targetStep) {
        if (targetStep < counts.length) {
            return counts[(int) targetStep];
        }
        long offset = targetStep - pattern.start;
        long cycles = offset / pattern.period;
        long rem = offset % pattern.period;
        long base = counts[pattern.start + (int) rem];
        return base + cycles * pattern.delta;
    }

    public static String solve() {
        long targetSteps = 1000000000000000000L;
        int detectSteps = 200000;
        int maxPeriod = 500;
        int verifyWindow = 50000;

        long[] counts = simulateBlackCounts(detectSteps);
        Pattern pattern = detectLinearPattern(counts, maxPeriod, verifyWindow);
        if (!pattern.found) {
            throw new RuntimeException("Could not detect linear periodic tail");
        }
        long ans = extrapolateBlackCount(counts, pattern, targetSteps);
        return String.valueOf(ans);
    }

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