#include <bits/stdc++.h>
using namespace std;
int A[1000001];
int U[1000001];
int cant_bits(int n) {
  int rst = 0;
  while (n) {
    n = n >> 1;
    rst++;
  }
  return rst;
}
int perf_number(int i) {
  int rst = 0;
  int bb = cant_bits(i);
  for (int j = 0; j < bb - 1; j++) {
    if (!(i & (1 << j))) {
      rst += 1 << j;
    }
  }
  return rst;
}
int main(void) {
  int n;
  cin >> n;
  int i = n, bb;
  unsigned long long rst = 0LL;
  bb = cant_bits(n);
  if (n & 1) {
    A[n] = 0;
    A[0] = n;
    U[n] = U[0] = 1;
    rst += 2 * n;
    i--;
  }
  for (; i > 0; i--) {
    if (U[i]) continue;
    int aux = perf_number(i);
    A[aux] = i;
    A[i] = aux;
    U[aux] = U[i] = 1;
    rst += 2 * (aux ^ i);
  }
  cout << rst << endl;
  for (int i = 0; i <= n; i++) {
    if (i) cout << " ";
    cout << A[i];
  }
  cout << endl;
  return 0;
}
