#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
void O_o() {
  ios::sync_with_stdio(0);
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
bool prime(int n, int i) {
  if (i == n) return true;
  if (n % i == 0) return false;
  prime(n, i + 1);
}
int arr[1000005], arr1[1000005];
unsigned long long sum = 0;
int main() {
  O_o();
  int n, s, m;
  cin >> n;
  for (int i = 1;; i *= 2) {
    if (i < 1000005)
      arr1[i] = 1;
    else
      break;
  }
  for (int i = 1;; i *= 2) {
    if (i == n) {
      s = ((i * 2) - 1);
      break;
    } else if (i > n) {
      s = i - 1;
      break;
    }
  }
  m = n++;
  for (int i = 0; i < int(n); i++) {
    if (arr[s - m] == 0 && arr[m] == 0) arr[s - m] = m, arr[m] = s - m;
    if (arr1[m] == 1) s = m - 1;
    if (m == 0) break;
    m--;
  }
  for (int i = 0; i < int(n); i++) sum += (arr[i] ^ i);
  cout << sum << '\n';
  for (int i = 0; i < int(n); i++) cout << arr[i] << " ";
  cout << '\n';
}
