#include <bits/stdc++.h>
using namespace std;
int INF = numeric_limits<int>::max();
int LINF = numeric_limits<long>::max();
int LLINF = numeric_limits<long long>::max();
const double EPS = 1e-6;
void solve(int n, vector<int>& ans) {
  if (n <= 0) return;
  int elder = 0;
  for (int i = 30; i >= 0; --i) {
    int mask = (1 << i);
    if (mask & n) {
      elder = i;
      break;
    }
  }
  int num = (1 << (elder + 1)) - 1;
  int r = n;
  int l = num ^ r;
  while (l <= n) {
    ans[r] = l;
    r--;
    l++;
  }
  solve(r, ans);
}
int main() {
  int n;
  cin >> n;
  vector<int> ans(n + 1);
  solve(n, ans);
  long long sum = 0;
  for (int i = 0; i < n + 1; ++i) {
    sum += i ^ ans[i];
  }
  cout << sum << endl;
  for (int i = 0; i < n + 1; ++i) {
    cout << ans[i] << " ";
  }
}
