#include <bits/stdc++.h>
using namespace std;
int num[1000007];
int solve(int nn) {
  int m = 0;
  while (nn) {
    nn /= 2;
    m++;
  }
  return m;
}
int main() {
  int n;
  int cnt[25];
  int l, te;
  while (~scanf("%d", &n)) {
    memset(num, -1, sizeof(num));
    for (int i = n; i >= 0; i--) {
      memset(cnt, 0, sizeof(cnt));
      l = -1;
      if (num[i] != -1) continue;
      int m = solve(i);
      for (int j = 0; j < m; j++) {
        te = (i & (1 << j));
        if (te != 0) te = 1;
        cnt[++l] = (te ^ 1);
      }
      te = 0;
      for (int j = l; j >= 0; j--) te = te * 2 + cnt[j];
      num[i] = te;
      num[te] = i;
    }
    long long ans = 0;
    for (int i = 0; i <= n; i++) ans += (long long)(num[i] ^ i);
    cout << ans << endl;
    for (int i = 0; i <= n; i++) printf("%d ", num[i]);
    cout << endl;
  }
  return 0;
}
