#include <bits/stdc++.h>
long long int count;
int a[100];
int i = 1;
int n;
void fun(int n) {
  if (n < 0)
    return;
  else if (n == 0)
    a[i] = 0;
  else {
    int s = 2;
    while (n >= s) {
      s *= 2;
    }
    s--;
    a[i] = s ^ n;
    count += (long long int)s * (n - a[i] + 1);
    i++;
    fun(a[i - 1] - 1);
  }
}
void output() {
  printf("%I64d\n", count);
  while (i) {
    if (i == 1) {
      int cha = a[i - 1] - a[i] + 1;
      int z = a[i - 1];
      while (cha--) {
        printf("%d ", z--);
      }
      printf("\n");
      i--;
      break;
    }
    int cha = a[i - 1] - a[i];
    int z = a[i - 1] - 1;
    while (cha--) {
      printf("%d ", z--);
    }
    i--;
  }
}
int main() {
  while (scanf("%d", &n) != EOF) {
    count = 0;
    a[0] = n;
    i = 1;
    fun(n);
    a[i++] = 0;
    output();
  }
}
