#include <bits/stdc++.h>
using namespace std;
int CA(int x) {
  int i = 0;
  int aux = x;
  while (aux > 0) {
    aux >>= 1;
    i++;
  }
  return (x ^ ((1 << (i)) - 1));
}
int main() {
  int n;
  cin >> n;
  int vis[n + 1];
  memset(vis, 0, sizeof(vis));
  int p[n + 1];
  long long int k = 0;
  for (int i = n; i >= 0; i--) {
    if (vis[i] == 0) {
      vis[i] = 1;
      p[i] = CA(i);
      vis[p[i]] = 1;
      p[p[i]] = i;
      k += i ^ p[i];
    }
  }
  cout << 2 * k << endl;
  for (int i = 0; i <= n; i++) cout << p[i] << " ";
  return 0;
}
