#include <bits/stdc++.h>
using namespace std;
long long f = 0, d[20], l, k, n, v[1000009];
queue<int> q;
int main() {
  int a, o;
  cin >> a;
  for (int i = a; i >= 1; i--) {
    o = i;
    l = 0;
    while (o > 0) {
      l++;
      d[l] = 0;
      if (o % 2 == 0) d[l] = 1;
      o /= 2;
    }
    k = 0;
    n = 1;
    for (int i = 1; i <= l; i++) {
      if (d[i] == 1) k += n;
      n *= 2;
    }
    if (v[k] == 0 and v[i] == 0) v[k] = i, v[i] = k;
  }
  for (int i = 0; i <= a; i++) {
    f += v[i] ^ i;
  }
  cout << f << endl;
  for (int i = 0; i <= a; i++) cout << v[i] << " ";
  return 0;
}
