#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned int n, b, x, p;
  unsigned long long sum = 0;
  cin >> n;
  int l = log10(n) / log10(2);
  if (n == 8 || n == 128) l++;
  x = pow(2, l + 1) - 1;
  int a[n + 1];
  p = n;
  int flag = 0;
  for (int i = n; i >= 0; i--) {
    b = x - i;
    if (b > p) {
      x = pow(2, l) - 1;
      b = x - i;
      l--;
      p = i;
      while (b > i) {
        if (i == 0) {
          a[0] = 0;
          flag = 1;
          break;
        }
        x = pow(2, l) - 1;
        b = x - i;
        l--;
        p = i;
      }
      if (flag == 1) break;
    }
    sum += x;
    a[i] = b;
  }
  cout << sum << endl;
  for (int i = 0; i <= n; i++) cout << a[i] << " ";
  return 0;
}
