#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int mod = 1e9 + 7;
const int B = 317;
long long n;
long long sum;
long long p[N];
long long foo(long long x) {
  long long g = 0;
  for (int i = 0; i < 22; i++) {
    if ((x & (1 << i))) g = i;
  }
  return (x ^ ((1 << (g + 1)) - 1));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = n; i >= 1; i--) {
    if (!p[i]) {
      long long j = foo(i);
      p[j] = i;
      p[i] = j;
    }
  }
  for (int i = 0; i <= n; i++) sum += (i ^ p[i]);
  cout << sum << "\n";
  for (int i = 0; i <= n; i++) cout << p[i] << " ";
}
