#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E6 + 10;
int n;
int vis[MAXN];
int main() {
  while (~scanf("%d", &n)) {
    memset(vis, 0, sizeof vis);
    long long ans = 0;
    if (n % 2) {
      for (int i = n - 1; i >= 2; i -= 2) {
        if (vis[i]) {
          continue;
        }
        int j = 0, t = 0;
        ;
        int temp = i;
        while (temp) {
          if (temp % 2 == 0) {
            j += pow(2, t);
          }
          temp /= 2;
          t++;
        }
        vis[i] = j;
        vis[i - 1] = j + 1;
        vis[j] = i;
        vis[j + 1] = i - 1;
        if (i - 1 != j) {
          ans += (i ^ j) * 4;
        } else {
          ans += (i ^ j) * 2;
        }
      }
      vis[n] = 0;
      vis[0] = n;
      ans += n * 2;
    } else {
      for (int i = n; i >= 2; i -= 2) {
        if (vis[i]) {
          continue;
        }
        int j = 0, t = 0;
        ;
        int temp = i;
        while (temp) {
          if (temp % 2 == 0) {
            j += pow(2, t);
          }
          temp /= 2;
          t++;
        }
        vis[i] = j;
        vis[i - 1] = j + 1;
        vis[j] = i;
        vis[j + 1] = i - 1;
        if (i - 1 != j) {
          ans += (i ^ j) * 4;
        } else {
          ans += (i ^ j) * 2;
        }
      }
    }
    printf("%lld\n", ans);
    for (int i = 0; i <= n; i++) {
      printf("%d", vis[i]);
      if (i == n) {
        printf("\n");
      } else {
        printf(" ");
      }
    }
  }
  return 0;
}
