#include <bits/stdc++.h>
using namespace std;
const int N = 1000000 + 10;
int vis[N] = {0}, a[N];
int main() {
  int n, len;
  cin >> n;
  for (len = 0; (1 << len) <= n; len++)
    ;
  int all = (1 << len) - 1;
  for (int i = n; i >= 0; i--) {
    int k = i ^ all;
    while ((k > n || vis[k]) && len >= 0) {
      len--;
      all = (1 << len) - 1;
      k = i ^ all;
    }
    vis[k] = 1;
    a[i] = k;
  }
  long long ans = 0;
  for (int i = 0; i <= n; ++i) ans += i ^ a[i];
  cout << ans << endl;
  for (int i = 0; i <= n; ++i) cout << a[i] << " ";
  cout << endl;
  return 0;
}
