#include <bits/stdc++.h>
using namespace std;
long long int a[1000000 + 100];
long long int visit[1000000 + 100];
int main() {
  long long int n;
  cin >> n;
  memset(visit, 0, sizeof(visit));
  memset(a, 0, sizeof(a));
  if (n % 2 == 0) a[0] = 0;
  for (long long int i = n; i > 0; i--) {
    if (!visit[i]) {
      for (long long int j = 1;; j++) {
        if ((1 << j) - 1 >= i) {
          a[i] = ((1 << j) - 1 - i);
          visit[((1 << j) - 1 - i)] = visit[i] = 1;
          a[((1 << j) - 1 - i)] = i;
          break;
        }
      }
    }
  }
  cout << n * (n + 1) << endl;
  for (long long int i = 0; i <= n; i++) {
    if (i != 0) printf(" ");
    cout << a[i];
  }
  printf("\n");
  return 0;
}
