#include <bits/stdc++.h>
const double pi = 3.141592653589793238462;
const double eps = 1e-10;
using namespace std;
int arr[1000010];
const int root = 1, maxp = 22;
int node[2100000][3];
int newnode = 2;
void addtrie(int i) {
  int tmp = root, mask, j;
  for (j = 0; j <= maxp; j++) {
    mask = (1 << (maxp - j));
    if (i & mask) {
      if (node[tmp][1]) {
        tmp = node[tmp][1];
      } else {
        node[tmp][1] = newnode++;
        tmp = newnode - 1;
      }
    } else {
      if (node[tmp][0]) {
        tmp = node[tmp][0];
      } else {
        node[tmp][0] = newnode++;
        tmp = newnode - 1;
      }
    }
    node[tmp][2]++;
  }
}
int findanddel(int i) {
  int tmp = root, mask, ans = 0, j;
  for (j = 0; j <= maxp; j++) {
    mask = (1 << (maxp - j));
    if (i & mask) {
      if (node[tmp][0] && node[node[tmp][0]][2] > 0)
        tmp = node[tmp][0];
      else
        tmp = node[tmp][1], ans += mask;
    } else {
      if (node[tmp][1] && node[node[tmp][1]][2] > 0)
        tmp = node[tmp][1], ans += mask;
      else
        tmp = node[tmp][0];
    }
    node[tmp][2]--;
  }
  return ans;
}
int find(int i) {
  int tmp = root, mask, ans = 0, j;
  for (j = 0; j <= maxp; j++) {
    mask = (1 << (maxp - j));
    if (i & mask) {
      if (node[tmp][0] && node[node[tmp][0]][2] > 0)
        tmp = node[tmp][0];
      else
        tmp = node[tmp][1], ans += mask;
    } else {
      if (node[tmp][1] && node[node[tmp][1]][2] > 0)
        tmp = node[tmp][1], ans += mask;
      else
        tmp = node[tmp][0];
    }
  }
  return ans;
}
void del(int i) {
  int tmp = root, mask, ans = 0, j;
  for (j = 0; j <= maxp; j++) {
    mask = (1 << (maxp - j));
    if (i & mask) {
      if (node[tmp][0] && node[node[tmp][0]][2] > 0)
        tmp = node[tmp][0];
      else
        tmp = node[tmp][1], ans += mask;
    } else {
      if (node[tmp][1] && node[node[tmp][1]][2] > 0)
        tmp = node[tmp][1], ans += mask;
      else
        tmp = node[tmp][0];
    }
    node[tmp][2]--;
  }
}
int main() {
  int i, j, k, n, tc;
  long long m = 0;
  scanf("%d", &n);
  for (i = 0; i <= n; i++) addtrie(i);
  for (i = n; i >= 0; i--) {
    arr[i] = findanddel(i);
    m = m + (i ^ arr[i]);
  }
  cout << m << endl;
  for (i = 0; i <= n; i++) printf("%d ", arr[i]);
  return 0;
}
