#include <bits/stdc++.h>
using namespace std;
int a[1111111], b[1111111], mark[1111111];
int main() {
  int n, x;
  a[1] = 1;
  int i, j;
  for (i = 2; i <= 30; i++) a[i] = a[i - 1] * 2;
  for (i = 2; i <= 30; i++) a[i] = a[i] - 1;
  cin >> n;
  long long ans = 0;
  for (i = n; i >= 0; i--)
    if (mark[i] == 0) {
      if (i == 0) {
        a[i] = 0;
        break;
      }
      for (j = 1; a[j] < i; j++) x = x;
      x = ~i;
      x = x & a[j];
      mark[i] = 1;
      mark[x] = 1;
      b[i] = x;
      b[x] = i;
      ans += 2 * (x ^ i);
    }
  if (n == 1) ans = 2;
  cout << ans << endl;
  for (i = 0; i < n; i++) cout << b[i] << ' ';
  cout << b[n] << endl;
  return 0;
}
