#include <bits/stdc++.h>
using namespace std;
struct trie {
  trie* node[2];
  long long int mark;
  trie() {
    mark = 0;
    node[0] = NULL;
    node[1] = NULL;
  }
};
trie* insert(trie* t, long long int num, long long int bit) {
  t->mark++;
  if (bit == -1) return t;
  int k = num >> bit;
  k %= 2;
  if (t->node[k] == NULL) {
    t->node[k] = new trie();
  }
  t->node[k] = insert(t->node[k], num, bit - 1);
  return t;
}
long long int b = 0;
void search(trie* t, long long int num, long long int bit) {
  t->mark--;
  if (bit == -1) return;
  int k = num >> bit;
  k %= 2;
  k ^= 1;
  if (t->node[k] == NULL || t->node[k]->mark == 0) {
    b = b + (1 - k) * (1 << bit);
    search(t->node[1 - k], num, bit - 1);
  } else {
    b = b + k * (1 << bit);
    search(t->node[k], num, bit - 1);
  }
}
int main() {
  long long int n;
  cin >> n;
  long long int p[n + 1];
  trie* t;
  t = new trie();
  long long int ans = 0;
  for (int i = 0; i <= n; i++) t = insert(t, i, 20);
  for (int i = n; i >= 0; i--) {
    b = 0;
    search(t, i, 20);
    p[i] = b;
    ans = ans + (b ^ i);
  }
  cout << ans << "\n";
  for (int i = 0; i <= n; i++) printf("%lld ", p[i]);
}
