#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, cnt = 0, ans = 0;
  cin >> n;
  long long m = n;
  long long a[n + 1];
  for (long long i = 0; i <= n; i++) a[i] = -1;
  while (n) {
    cnt += 1;
    n /= 2;
  }
  long long d = pow(2, cnt) - 1;
  for (long long i = m; i >= 2; i--) {
    if (a[i] == -1) {
      long long p = d - i;
      if (p <= m && a[p] == -1)
        a[i] = p, a[p] = i, ans += 2 * d;
      else {
        d = (d + 1) / 2 - 1;
        i += 1;
      }
    }
  }
  if (a[0] == -1 && a[1] == -1)
    a[0] = 1, a[1] = 0, ans += 2;
  else if (a[0] == -1)
    a[0] = 0;
  cout << ans << "\n";
  for (long long i = 0; i <= m; i++) cout << a[i] << " ";
}
