Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.

For permutation p = p0, p1, ..., pn, Polo has defined its beauty — number <image>.

Expression <image> means applying the operation of bitwise excluding "OR" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as "^" and in Pascal — as "xor".

Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.

Input

The single line contains a positive integer n (1 ≤ n ≤ 106).

Output

In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.

If there are several suitable permutations, you are allowed to print any of them.

Examples

Input

4


Output

20
0 2 1 4 3
import java.io.*;
import java.util.*;

public class Template implements Runnable {

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    void init() throws FileNotFoundException {
        try {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        }
    }

    String readString() throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    int[] readIntArray(int size) throws IOException {
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i] = readInt();
        }
        return res;
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    <T> List<T>[] createGraphList(int size) {
        List<T>[] list = new List[size];
        for (int i = 0; i < size; i++) {
            list[i] = new ArrayList<>();
        }
        return list;
    }

    public static void main(String[] args) {
        new Template().run();
        // new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    long memoryTotal, memoryFree;

    void memory() {
        memoryFree = Runtime.getRuntime().freeMemory();
        System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
                + " KB");
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();
            memoryTotal = Runtime.getRuntime().freeMemory();
            init();
            solve();
            out.close();
            if (System.getProperty("ONLINE_JUDGE") == null) {
                time();
                memory();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    class Brut {
        Brut(int n) {
            data = new int[n];
            for (int i = 0; i < n; i++) {
                data[i] = i;
            }
        }

        int[] data;
        List<int[]> bests = new ArrayList<>();

        int current() {
            if (bests.size() == 0) return -1;
            return calc(bests.get(0));
        }

        void brut(int pos) {
            if (pos == data.length) {
                if (current() < calc(data)) {
                    bests.clear();
                    bests.add(data.clone());
                } else if (current() == calc(data)) {
                    bests.add(data.clone());
                }
                return;
            }
            for (int i = pos; i < data.length; i++) {
                int t = data[i];
                data[i] = data[pos];
                data[pos] = t;
                brut(pos + 1);
                t = data[i];
                data[i] = data[pos];
                data[pos] = t;
            }
        }

        int calc(int[] a) {
            if (a == null) return -1;
            int sum = 0;
            for (int i = 0; i < a.length; i++) {
                sum += i ^ a[i];
            }
            return sum;
        }
    }

    void solve() throws IOException {
        int n = readInt();
        solve(n + 1);
//        Brut brut = new Brut(n);
//        brut.brut(0);
//        System.err.println(brut.current());
//        System.err.println(solve(n));
    }

    void solve(int n) {
        TrieSolution solution = new TrieSolution();
        for (int i = 0; i < n; i++) {
            solution.add(i);
        }
        long res = 0;
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            int index = solution.find(i);
            result[index] = i;
            res += index ^ i;
        }
        out.println(res);
        for (int i = 0; i < result.length; i++) {
            out.print(result[i] + " ");
        }
    }

    class TrieSolution {
        Node root = new Node();

        void add(int x) {
            Node current = root;
            for (int bit = 23; bit >= 0; bit--) {
                if (has(x, bit)) {
                    if (current.one == null) {
                        current.one = new Node();
                    }
                    current = current.one;
                } else {
                    if (current.zero == null) {
                        current.zero = new Node();
                    }
                    current = current.zero;
                }
            }
            current.terminate = true;

        }

        int find(int x) {
            return find(root, x, 23, 0);
        }


        int find(Node node, int x, int bit, int already) {
            if (node.terminate) {
                node.removed = true;
                return already;
            }
            int currentBit = 1 << bit;
            int result = 0;
            if (has(x, bit)) {
                if (node.zero != null) {
                    result = find(node.zero, x, bit - 1, already);
                } else {
                    result = find(node.one, x, bit - 1, already | currentBit);
                }
            } else {
                if (node.one != null) {
                    result = find(node.one, x, bit - 1, already | currentBit);
                } else {
                    result = find(node.zero, x, bit - 1, already);
                }
            }
            if (node.zero != null) {
                if (node.zero.removed) node.zero = null;
            }
            if (node.one != null) {
                if (node.one.removed) node.one = null;
            }
            if (node.zero == null && node.one == null) node.removed = true;
            return result;
        }

        boolean has(int x, int bit) {
            return (x & (1 << bit)) != 0;
        }
    }

    static class Node {
        Node zero;
        Node one;
        boolean terminate;
        boolean removed;
    }


}