#include <bits/stdc++.h>
using namespace std;
inline bool ascending(int i, int j) { return (i < j); }
inline bool descending(int i, int j) { return (i > j); }
const int MN = 1000000 + 10;
int comp(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
bool cmp(int a, int b) { return a > b; }
bool use[MN];
int arr[MN];
int main() {
  int temp = 0;
  int num = 0, max1;
  long long res = 0;
  cin >> num;
  for (int i = num; i >= 0; --i) {
    if (use[i]) {
      continue;
    }
    max1 = 0;
    while (max1 < i) {
      max1 = max1 << 1;
      max1 = max1 | 1;
    }
    temp = max1 - i;
    if (use[temp]) {
      for (__typeof(0) j = 0; j != num; j++) {
        if (use[j] == 0) {
          temp = j;
          break;
        }
      }
    }
    res += ((temp ^ i) * 2);
    arr[i] = temp;
    arr[temp] = i;
    use[temp] = 1;
    use[i] = 1;
  }
  cout << res << endl;
  for (__typeof(0) i = 0; i != num + 1; i++) {
    cout << arr[i] << " ";
  }
  return 0;
}
