#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1 << 20;
const int MaxNumber = 1 << 20;
int N, P[MaxN];
bool Mark[MaxNumber];
int main(void) {
  scanf("%d", &N);
  for (int i = N; i >= 0; i--)
    for (int j = MaxNumber; j >= 1; j >>= 1) {
      int Curr = i ^ (j - 1);
      if ((Curr <= N) && (!Mark[Curr])) {
        Mark[Curr] = true;
        P[i] = Curr;
        break;
      }
    }
  long long int MaxXOR = 0;
  for (int i = 0; i <= N; i++) MaxXOR += 1LL * (i ^ P[i]);
  printf("%I64d\n", MaxXOR);
  for (int i = 0; i <= N; i++) printf("%d%c", P[i], (i == N) ? '\n' : ' ');
  return 0;
}
