#include <bits/stdc++.h>
using namespace std;
int MAX = 1000000;
int MIN = -1000000;
int INF = 1000000000;
int x4[4] = {0, 1, 0, -1};
int y4[4] = {1, 0, -1, 0};
int x8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int y8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int i, j, k;
int maks_bit;
string parse(int A) {
  string ret = "00000000000000000000";
  int indx = 0, k;
  while (A > 0) {
    ret[indx++] = ((A % 2) + '0');
    A /= 2;
  }
  for (k = 0; k < 20; k++)
    if (ret[k] == '1') maks_bit = k;
  return ret;
}
int arr[1000010];
int pangkat[100];
bool udah[1000010];
int ubah(string temp) {
  int ret = 0;
  int k;
  for (k = 0; k <= maks_bit; k++) ret += ((temp[k] - '0') * pangkat[k]);
  return ret;
}
int main() {
  int n, te;
  long long ans = 0;
  pangkat[0] = 1;
  for (i = 1; i <= 20; i++) pangkat[i] = pangkat[i - 1] * 2;
  string temp;
  string pasangan;
  maks_bit = 0;
  memset((udah), (false), sizeof(udah));
  ;
  cin >> n;
  for (i = n; i >= 1; i--) {
    if (udah[i] == true) continue;
    temp = parse(i);
    pasangan = "00000000000000000000";
    for (j = 0; j <= maks_bit; j++)
      if (temp[j] == '0') pasangan[j] = '1';
    te = ubah(pasangan);
    arr[i] = te;
    arr[te] = i;
    udah[i] = udah[te] = true;
    ans += ((long long)(pangkat[maks_bit + 1] - 1) * 2ll);
  }
  cout << ans << endl;
  for (i = (0); i < (n + 1); i++) printf("%d ", arr[i]);
  cout << endl;
  {
    fflush(stdin);
    getchar();
  };
  return 0;
}
