#include <bits/stdc++.h>
using namespace std;
int a[1000600];
int t[30], t2[30];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    memset(a, -1, sizeof(a));
    for (int i = n; i >= 0; --i)
      if (a[i] == -1) {
        int cnt = 0;
        int j = i;
        while (j) {
          t[cnt++] = j % 2;
          j /= 2;
        }
        for (int k = 0; k < cnt; ++k)
          if (t[k] == 1)
            t2[k] = 0;
          else
            t2[k] = 1;
        int obj = 0;
        for (int k = 0, tt = 1; k < cnt; ++k, tt *= 2) obj += t2[k] * tt;
        if (a[obj] != -1) {
          for (int k = cnt - 1; k >= 0; --k)
            if (t[k] == 0 && t2[k] == 1) {
              obj = 0;
              for (int kk = 0, tt = 1; kk < cnt; ++kk, tt *= 2)
                obj += t2[kk] * tt;
              if (a[obj] == -1) {
                a[obj] = i;
                a[i] = obj;
                break;
              }
            }
        } else {
          a[obj] = i;
          a[i] = obj;
        }
      }
    long long ans = 0;
    for (int i = 0; i <= n; ++i) ans += i ^ a[i];
    printf("%I64d\n", ans);
    for (int i = 0; i <= n; ++i) {
      if (i < n)
        printf("%d ", a[i]);
      else
        printf("%d\n", a[i]);
    }
  }
  return 0;
}
