#include <bits/stdc++.h>
using namespace std;
int vals[1000005];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) vals[i] = i;
  int bit = 0;
  while ((1 << bit) < n + 1) {
    int mov = 1 << bit + 1;
    for (int i = 0; i <= n; i++) {
      if (mov > (1 << bit))
        if (i + (1 << bit) <= n) swap(vals[i], vals[i + (1 << bit)]);
      mov--;
      if (mov == 0) mov = 1 << bit + 1;
    }
    bit++;
  }
  long long ret = 0;
  for (int i = 0; i <= n; i++) ret = ret + (vals[i] ^ i);
  cout << ret;
  printf("\n");
  for (int i = 0; i <= n; i++) printf("%d ", vals[i]);
}
