#include <bits/stdc++.h>
using namespace std;
int flip(int n) {
  int res = 1;
  int ans = 0;
  while (n > 0) {
    int r = (n % 2);
    if (r == 0)
      r = 1;
    else
      r = 0;
    if (r) ans += res;
    res = res * 2;
    n /= 2;
  }
  return ans;
}
int ans[1000010];
int main() {
  long long n;
  long long x = 0;
  cin >> n;
  int vis[1000010];
  memset(vis, 0, sizeof vis);
  for (int i = n; i >= 0; i--) {
    if (!vis[i]) {
      vis[i] = 1;
      int cur = flip(i);
      if (!vis[cur]) {
        vis[cur] = 1;
        ans[cur] = i;
        ans[i] = cur;
      } else
        ans[i] = i;
    }
    x += (i ^ ans[i]);
  }
  cout << x << endl;
  for (int i = 0; i <= n; i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
