#include <bits/stdc++.h>
using namespace std;
int stmp;
const int MAX = 1000001;
const int INF = 1000000001;
int arr[MAX];
int n;
int main(int argc, char *argv[]) {
  cin >> n;
  int i = n;
  while (i) {
    int k = 1;
    while (k <= i) k *= 2;
    --k;
    for (int t = (k - i); t <= (i); ++t) arr[t] = k ^ t;
    i = k - i - 1;
  }
  long long res = 0;
  for (int t = (0); t <= (n); ++t) res += arr[t] ^ t;
  cout << res << endl;
  for (int t = (0); t <= (n); ++t) cout << arr[t] << ' ';
  cout << endl;
  return 0;
}
