#include <bits/stdc++.h>
using namespace std;
int a[1000005];
int main() {
  int n;
  while (cin >> n) {
    for (int i = n; i >= 0; i--)
      if (!a[i]) {
        int mm = (1 << ((int)(log2(i) + 1))) - 1;
        a[i] = mm ^ i;
        a[mm ^ i] = i;
      }
    long long int ans = 0;
    for (int i = 0; i <= n; i++) {
      ans += a[i] ^ i;
    }
    cout << ans << endl;
    for (int i = 0; i <= n; i++) {
      cout << a[i];
      if (i != n)
        cout << " ";
      else
        cout << "\n";
    }
  }
  return 0;
}
