#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
double tick() {
  static clock_t oldt;
  clock_t newt = clock();
  double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC;
  oldt = newt;
  return diff;
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin.ignore(0);
  long long n;
  cin >> n;
  vector<long long> st;
  st.push_back(0);
  long long a = 1;
  while (a <= 1000100) {
    a *= 2;
    st.push_back(a);
  }
  long long lo = lower_bound(st.begin(), st.end(), n) - st.begin();
  ;
  if (st[lo] == n) lo++;
  map<long long, long long> m;
  cout << n * (n + 1) << endl;
  long long arr[n + 10];
  if (n % 2 == 0) arr[0] = 0;
  if (n % 2 == 0) {
    for (long long i = n; i > 0; i--) {
      if (st[lo] - i - 1 <= n && m[st[lo] - i - 1] == 0) {
        arr[i] = st[lo] - i - 1;
        m[arr[i]] = 1;
        continue;
      }
      while (true) {
        long long f = 0;
        if (st[lo] - i - 1 > n) {
          lo--;
          f = 1;
        }
        if (m[st[lo] - i - 1] == 1) {
          if (f == 0) {
            lo--;
          }
          continue;
        } else if (st[lo] - i - 1 <= n) {
          arr[i] = st[lo] - i - 1;
          m[arr[i]] = 1;
          break;
        }
      }
    }
  } else {
    for (long long i = n; i >= 0; i--) {
      if (st[lo] - i - 1 <= n && m[st[lo] - i - 1] == 0) {
        arr[i] = st[lo] - i - 1;
        m[arr[i]] = 1;
        continue;
      }
      while (true) {
        long long f = 0;
        if (st[lo] - i - 1 > n) {
          lo--;
          f = 1;
        }
        if (m[st[lo] - i - 1] == 1) {
          if (f == 0) {
            lo--;
          }
          continue;
        } else if (st[lo] - i - 1 <= n) {
          arr[i] = st[lo] - i - 1;
          m[arr[i]] = 1;
          break;
        }
      }
    }
  }
  for (long long i = 0; i <= n; i++) cout << arr[i] << " ";
}
