#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#define ff first
#define ss second
#define pb emplace_back
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(),(x).rend()
#define re return
#define mp make_pair
#define sz(x) ((int)(x).size())
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define repx(i, x, n) for(int i = x; i < n; i++)
#define repd(i, x, n, d) for(int i = x; i < n; i += d)
#define rrepx(i, x, n) for(int i = n - 1; i >= x; i--)
#define rrepd(i, x, n, d) for(int i = n - 1; i >= x; i -= d)
#define unq(x); { sort(x.begin(), x.end()); x.resize(unique(x.begin(), x.end()) - x.begin()); }
#define sqr(x) ((x)*(x))

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<vl> vvl;

#define DEBUG 1
//#define FILES 1

#ifdef DEBUG
#define debug(x)   cerr << #x << " " << x << endl;
#define debug_p(x) cerr << #x << " " << x.ff << " " << x.ss << endl;
#define debug_v(x) {cerr << #x << " "; for (auto i : x) cerr << i << " "; cerr << endl;}
#define debug_vp(x) {cerr << #x << " "; for (auto i : x) cerr << '[' << i.ff << " " << i.ss << ']'; cerr << endl;}
#define debug_v_v(x) {cerr << #x << "/*\n"; for (auto i : x) { for (auto i2 : i) cerr << i2 << " "; cerr << '\n';} cerr << "*/" << #x << endl;}
#else
#define debug(x) 0;
#define debug_p(x) 0;
#define debug_v(x) 0;
#define debug_vp(x) 0;
#define debug_v_v(x) 0;
#endif

#ifdef FILES
#define files(in, out); { freopen(in, "r", stdin); freopen(out, "w", stdout);}
#else
#define files(in, out) 0;
#endif

const int INF = 1e9 + 10;
const ll INFll = 2e18;
const int BASE1 = 179;
const int BASE2 = 653;
const ll MOD = 998244353;
const int MAXA = 2000001;
const int MAXN = 2e5 + 1;
const int MAXLOG = 22;
const ll COST = 1000 * 1000 * 1000;
const ld PI = 3.1415926535;
const ld EPS = 1e-15;

struct edge {
    ll u, v, cost;

    edge(ll a, ll b, ll c) {
        u = a;
        v = b;
        cost = c;
    }
};

bool cmp(edge& a, edge& b) {
    return a.cost < b.cost;
}

struct dsu {
    vi p, d;

    dsu(int n) {
        p.resize(n);
        d.resize(n);
        rep(i, n)
            p[i] = i;
    }

    int getp(int v) {
        if (p[v] == v)
            return v;
        return p[v] = getp(p[v]);
    }

    void unite(int u, int v) {
        u = getp(u), v = getp(v);
        if (d[u] < d[v])
            swap(u, v);
        p[v] = u;
        if (d[u] == d[v])
            d[u]++;
    }
};

void solve() {
    ll m, n;
    cin >> m >> n;
    vl a(m), b(n);
    rep(i, m)
        cin >> a[i];
    rep(j, n)
        cin >> b[j];
    vector<edge> edges;
    ll allcost = 0;
    rep(v, m) {
        ll s;
        cin >> s;
        rep(i, s) {
            ll u;
            cin >> u;
            u--;
            allcost += a[v] + b[u];
            edges.pb(edge(u, v + n, a[v] + b[u]));
        }
    }
    sort(rall(edges), cmp);
    dsu d(n + m);
    ll res = 0;
    for (edge e : edges) {
        if (d.getp(e.u) != d.getp(e.v)) {
            res += e.cost;
            d.unite(e.u, e.v);
        }
    }
    cout << allcost - res << "\n";
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    //cin >> t;
    while (t--)
        solve();
    return 0;
}


#include<iostream>
using namespace std;
int a1[1000005],a2[1000005],cnt;
void solve(int l,int r){
	if(l==r) return ;
	int m=(l+r)/2;
	solve(l,m);
	solve(m+1,r);
	for(int i=l;i<=m;i++){
		a1[++cnt]=i;
		a2[cnt]=r-i+l;
	}
}
int main(){
	int n;
	cin>>n;
	int now=1;
	while(now*2<n){
		now=now*2;
		solve(1,now);
		solve(n-now+1,n);
	}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++){
		cout<<a1[i]<<" "<<a2[i]<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int a1[100000],a2[100000],cnt;
void solve(int l,int r){
	if(l==r) return ;
	int m=(l+r)/2;
	solve(l,m);
	solve(m+1,r);
	for(int i=l;i<=m;i++){
		a1[++cnt]=i;
		a2[cnt]=r-i+l;
	}
}
int main(){
	int n;
	cin>>n;
	int now=1;
	while(now*2<n){
		now=now*2;
		solve(1,now);
		solve(n-now+1,n);
	}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++){
		cout<<a1[i]<<" "<<a2[i]<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int a[100000],b[100000],cnt;
void solve(int l,int r){
	if(l==r) return ;
	int m=(l+r)/2;
	solve(l,m);
	solve(m+1,r);
	for(int i=l;i<=m;i++){
		a[++cnt]=i;
		b[cnt]=2*m-l+1;
	}
}
int main(){
	int n;
	cin>>n;
	int now=1;
	while(now*2<=n){
		now=now*2;
		solve(1,now);
		solve(n-now+1,n);
	}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++){
		cout<<a[i]<<" "<<b[i]<<endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int a[100000],b[100000],cnt;
void solve(int l,int r){
	if(l==r) return ;
	int m=(l+r)/2;
	solve(l,m);
	solve(m+1,r);
	for(int i=l;i<=m;i++){
		a[++cnt]=i;
		b[cnt]=2*m-l+1;
	}
}
int main(){
	int n;
	cin>>n;
	int now=1;
	while(now*2<n){
		now=now*2;
		solve(1,now);
		solve(n-now+1,n);
	}
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++){
		cout<<a[i]<<" "<<b[i]<<endl;
	}
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long int n,a[1000005],cnt,b[1000005],bits[100005];

int solve(int s,int e)
{
	if(s>=e)return 0;
	int mid=(s+e)/2;
	solve(s,mid);
	solve(mid+1,e);
	int p=mid+1;
	for(int i=s;i<=mid;i++)
	{
		cnt++;
		a[cnt]=i;
		b[cnt]=p;
		p++;
	}
}
int main(){
	cin>>n;
	long long int cnt=1;
	while(cnt<=n)cnt*=2;
	cnt/=2;
	solve(1,cnt);
	solve(n-cnt+1,n);
	for(int i=1;i<=cnt;i++)cout<<a[i]<<" "<<b[i]<<endl;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long int n,a[1000005],cnt,b[1000005];

int solve(int s,int e)
{
	if(s>=e)return 0;
	int mid=(s+e)/2;
	solve(s,mid);
	solve(mid+1,e);
	int p=mid+1;
	for(int i=s;i<=mid;i++)
	{
		cnt++;
		a[cnt]=i;
		b[cnt]=min(p,e);
		p++;
	}
}
int main(){
	cin>>n;
	solve(1,n);
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++)cout<<a[i]<<" "<<b[i]<<endl;
}


#include <iostream>
#include <algorithm>
using namespace std;
long long int n,a[1000005],cnt,b[1000005],bits[100005];

int solve(int s,int e)
{
	if(s>=e)return 0;
	int mid=(s+e)/2;
	solve(s,mid);
	solve(mid+1,e);
	int p=mid+1;
	for(int i=s;i<=mid;i++)
	{
		cnt++;
		a[cnt]=i;
		b[cnt]=p;
		p++;
	}
}
int main(){
	cin>>n;
	long long int count=1;
	while(count<=n)count*=2;
	count/=2;
	solve(1,count);
	solve(n-count+1,n);
	cout<<cnt<<endl;
	for(int i=1;i<=cnt;i++)cout<<a[i]<<" "<<b[i]<<endl;
}


#include<bits/stdc++.h>
using namespace std;
struct apple
{
	int l,r;
	apple(int l=0,int r=0):l(l),r(r){}
}ans[1000005];
int n,tot=0,lg[1000005];
void gds(int l,int r)
{
	if(l==r)return;
	int mid=l+r>>1;
	gds(l,mid);
	gds(mid+1,r);
	for(int i=l;i<=mid;i++)ans[++tot]=apple(i,i-l+mid+1);
}
int main()
{
	lg[1]=1;
	for(int i=2;i<=1000000;i++)lg[i]=lg[i>>1]<<1;
	int n;
	cin>>n;
	gds(1,lg[n]);
	gds(n-lg[n]+1,n);
	cout<<tot<<endl;
	for(int i=1;i<=tot;i++)printf("%d %d\n",ans[i].l,ans[i].r);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define sc(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ll long long
#define pb push_back
vector<pair<int,int> >ans;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void merge(int l,int r,int lev)
{
	if(lev==0)
	{
		ans.pb({l,r});
		return;
	}
	int mid=(l+r)>>1;
	merge(l,mid,lev-1);
	merge(mid+1,r,lev-1);
	int j=mid+1;
	for(int i=l;i<=mid;i++)
	{
		ans.pb({i,j});
		j++;
	}
}

int main(int argc, char** argv) {
	int n;
	sc(n);
	if(n==1)
	{
	printf("0");
	return 0;
	}
	int t=1;
	int lev=-1;
	while(t*2<=n)t*=2,lev++;
	//cout<<t<<endl;
	//cout<<t<<endl;
	merge(1,t,lev);
	merge(n-t+1,n,lev);
	rep(i,0,ans.size()-1)
	{
		printf("%d %d\n",ans[i].first,ans[i].second);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T>
inline void chkmax(T &x, T y) { if (x < y) x = y; }

template <typename T>
inline void chkmin(T &x, T y) { if (x > y) x = y; }

inline int read() {
  #define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48) c = nc;
  while (c > 47) x = x * 10 + (c ^ 48), c = nc;
  return x;
  #undef nc
}

typedef long long ll;
typedef pair <int, int> pii;
const int maxn = 15010;
int n;

vector <pii> ans;

void run(int k, int de) {
  rep(i, 0, k - 1) {
    rep(j, 1, 1 << k) {
      rep(t, 0, (1 << i) - 1) {
        int x = j + t + de, y = j + t + (1 << i) + de;
        ans.push_back({x, y});
      }
      j += (1 << (i + 1)) - 1;
    }
  }
}

vector <int> vec[16];

void unite(vector <int> &v1, vector <int> &v2) {
  vector <int> tmp;
  for (int y : v1) {
    int x = v2.back();
    v2.pop_back(), ans.push_back({x, y}), tmp.push_back(x);
  }
  for (int x : tmp) v1.push_back(x);
}

void solve() {
  cin >> n;
  int cur = 1;
  per(i, 15, 0) if (n >> i & 1) {
    run(i, cur - 1);
    rep(j, 1, (1 << i)) vec[i].push_back(cur++);
  }
  static int id[16];
  rep(i, 0, 15) id[i] = i;
  sort(id, id + 16, [] (int x, int y) {
    return (int) vec[x].size() > (int) vec[y].size();
  });
  while (1) {
    sort(id + 1, id + 16, [] (int x, int y) {
      return (int) vec[x].size() > (int) vec[y].size();
    });
    int p1 = id[0], p2 = 0, p3 = 0;
    rep(i, 1, 15) if (!vec[id[i]].empty()) p2 = p3, p3 = id[i];
    if (!p2) break;
    while (vec[p2].size() > vec[p3].size()) {
      unite(vec[p3], vec[p1]);
    }
    unite(vec[p3], vec[p2]);
  }
  printf("%d\n", (int) ans.size());
  for (pii p : ans) printf("%d %d\n", p.first, p.second);
}

signed main() {
//  int T = read();
//  while (T--) solve();
  solve();
  return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
using namespace std;
int x,n;
vector<pii> v;
void solve(int l,int r){
	if(r-l+1==2){
		v.pb(mp(l,r));
		return;
	}
	int mid = (l+r)>>1;
	solve(l,mid);solve(mid+1,r);
	rep(i,1,mid-l+1){
		//cout
		v.pb(mp(l+i-1,r-i+1));
	}
}
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
 	cin>>n;
 	per(i,0,20){
 		if((1<<i)&n){
 			//cout << "fuckA:" << i << endl;
 			//cout << n-(1<<i)+1 << '#' << n << endl;
 			solve(1,(1<<i));
 			solve(n-(1<<i)+1,n);
 			break;
 		}
 	}
 	cout << v.size() << endl;;
 	for(auto u:v) cout << u.fi << ' ' << u.se << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
using namespace std;
int x,n;
vector<pii> v;
void solve(int l,int r){
	if(l>=r) return;
	int mid = (l+r)>>1;
	rep(i,1,mid-l+1){
		//cout
		v.pb(mp(l+i-1,r-i+1));
	}
	solve(l,mid);solve(mid+1,r);
}
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
 	cin>>n;
 	per(i,0,20){
 		if((1<<i)&n){
 			//cout << "fuckA:" << i << endl;
 			//cout << n-(1<<i)+1 << '#' << n << endl;
 			solve(1,(1<<i));
 			solve(n-(1<<i)+1,n);
 			break;
 		}
 	}
 	cout << v.size() << endl;;
 	for(auto u:v) cout << u.fi << ' ' << u.se << endl;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define N
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define lowbit(i) ((i)&(-i))
#define VI vector<int>
using namespace std;
int x,n;
vector<pii> v;
void solve(int l,int r){
	if(r-l+1==2){
		v.pb(mp(l,r));
		return;
	}
	int mid = (l+r)>>1;
	solve(l,mid);solve(mid+1,r);
	rep(i,1,mid-l+1){
		//cout
		v.pb(mp(l+i-1,r-i+1));
	}
}
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
 	cin>>n;
 	if(n==1){
 		puts("0");
 		return 0;
 	}
 	per(i,0,20){
 		if((1<<i)&n){
 			//cout << "fuckA:" << i << endl;
 			//cout << n-(1<<i)+1 << '#' << n << endl;
 			solve(1,(1<<i));
 			solve(n-(1<<i)+1,n);
 			break;
 		}
 	}
 	cout << v.size() << endl;;
 	for(auto u:v) cout << u.fi << ' ' << u.se << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <utility>

int n;
std::vector<std::pair<int, int>> ans;

void solve(int l, int r) {
    if (l == r) return;
    int mid = l + r >> 1;
    int k = r - l + 1 >> 1;
    solve(l, mid);
    solve(mid + 1, r);
    for (int i = l; i <= mid; ++i) ans.emplace_back(i, i + k);
}

int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    std::cin >> n;
    int k = 1;
    while (k << 1 < n) k <<= 1;
    solve(1, k);
    solve(n - k + 1, n);
    std::cout << ans.size() << '\n';
    for (auto i : ans) std::cout << i.first << ' ' << i.second << '\n';
    return 0;
}


#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<list>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<limits>
#include<cctype>
#include<fstream>
#include<unordered_map>
#include<complex>
#pragma warning(disable:4996)
using namespace std;

#define debug(i) cout<<"<debug> "<<#i<<" "<<i<<" <\\debug>"<<endl
#define mfor(i,a,b) for(int i=(a);i<=(b);i++)
#define mrep(i,a,b) for(int i=(a);i>=(b);i--)
#define lll __int128
#define Re register
#define mem(a,b) memset(a,(b),sizeof(a))
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

vector<intpair>ans;

void dfs(int l, int r)
{
	if (l == r) return;
	int mid = (l + r) >> 1;
	dfs(l, mid);
	dfs(mid + 1, r);
	mfor(i, l, mid) ans.emplace_back(i, i - l + 1 + mid);
}

int main()
{
	Design;
	int n;
	cin >> n;
	dfs(1, 1 << (int)log2(n));
	dfs(n - (1 << (int)log2(n) | 1), n);
	cout << ans.size() << endl;
	for (auto it : ans) cout << it.first << " " << it.second << endl;
}


#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<list>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<limits>
#include<cctype>
#include<fstream>
#include<unordered_map>
#include<complex>
#pragma warning(disable:4996)
using namespace std;

#define debug(i) cout<<"<debug> "<<#i<<" "<<i<<" <\\debug>"<<endl
#define mfor(i,a,b) for(int i=(a);i<=(b);i++)
#define mrep(i,a,b) for(int i=(a);i>=(b);i--)
#define lll __int128
#define Re register
#define mem(a,b) memset(a,(b),sizeof(a))
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

vector<intpair>ans;

void dfs(int l, int r)
{
	if (l == r) return;
	int mid = (l + r) >> 1;
	dfs(l, mid);
	dfs(mid + 1, r);
	mfor(i, l, mid) ans.emplace_back(i, i - l + 1 + mid);
}

int main()
{
	Design;
	int n;
	cin >> n;
	dfs(1, 1 << (int)log2(n));
	dfs(1 << (int)log2(n) | 1, n);
	cout << ans.size() << endl;
	for (auto it : ans) cout << it.first << " " << it.second << endl;
}


#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<list>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<limits>
#include<cctype>
#include<fstream>
#include<unordered_map>
#include<complex>
#pragma warning(disable:4996)
using namespace std;

#define debug(i) cout<<"<debug> "<<#i<<" "<<i<<" <\\debug>"<<endl
#define mfor(i,a,b) for(int i=(a);i<=(b);i++)
#define mrep(i,a,b) for(int i=(a);i>=(b);i--)
#define lll __int128
#define Re register
#define mem(a,b) memset(a,(b),sizeof(a))
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

vector<intpair>ans;

void dfs(int l, int r)
{
	if (l == r) return;
	int mid = (l + r) >> 1;
	dfs(l, mid);
	dfs(mid + 1, r);
	mfor(i, l, mid) ans.emplace_back(i, i - l + 1 + mid);
}

int main()
{
	Design;
	int n;
	cin >> n;
	dfs(1, 1 << (int)log2(n));
	dfs(n - (1 << (int)log2(n)) + 1, n);
	cout << ans.size() << endl;
	for (auto it : ans) cout << it.first << " " << it.second << endl;
}


#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<list>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<limits>
#include<cctype>
#include<fstream>
#include<unordered_map>
#include<complex>
#pragma warning(disable:4996)
using namespace std;

#define debug(i) cout<<"<debug> "<<#i<<" "<<i<<" <\\debug>"<<endl
#define mfor(i,a,b) for(int i=(a);i<=(b);i++)
#define mrep(i,a,b) for(int i=(a);i>=(b);i--)
#define lll __int128
#define Re register
#define mem(a,b) memset(a,(b),sizeof(a))
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

vector<intpair>ans;

void dfs(int l, int r)
{
	if (l == r) return;
	int mid = (l + r) >> 1;
	dfs(l, mid);
	dfs(mid + 1, r);
	mfor(i, 1, mid) ans.emplace_back(i, i - l + 1 + mid);
}

int main()
{
	Design;
	int n;
	cin >> n;
	dfs(1, 1 << (int)log2(n));
	dfs(n - (1 << (int)log2(n)) + 1, n);
	cout << ans.size() << endl;
	for (auto it : ans) cout << it.first << " " << it.second << endl;
}


#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<list>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<limits>
#include<cctype>
#include<fstream>
#include<unordered_map>
#include<complex>
#pragma warning(disable:4996)
using namespace std;

#define debug(i) cout<<"<debug> "<<#i<<" "<<i<<" <\\debug>"<<endl
#define mfor(i,a,b) for(int i=(a);i<=(b);i++)
#define mrep(i,a,b) for(int i=(a);i>=(b);i--)
#define lll __int128
#define Re register
#define mem(a,b) memset(a,(b),sizeof(a))
#define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
typedef pair<ll, ll> llpair;
typedef long double ld;
const double pi = acos(-1);
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3f;

vector<intpair>ans;

void dfs(int l, int r)
{
	if (l == r) return;
	int mid = (l + r) >> 1;
	dfs(l, mid);
	dfs(mid + 1, r);
	mfor(i, 1, mid) ans.emplace_back(i, i - l + 1 + mid);
}

int main()
{
	Design;
	int n;
	cin >> n;
	dfs(1, 1 << (int)log2(n));
	dfs(1 << (int)log2(n) | 1, n);
	cout << ans.size() << endl;
	for (auto it : ans) cout << it.first << " " << it.second << endl;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=500003;
int ANS[maxn][2],CNT;
void solve(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	solve(l,mid),solve(mid+1,r);
	if(((mid-l+1)&1)==((r-mid)&1)){
		for(int i=l;i<=mid;i++)ANS[++CNT][0]=i,ANS[CNT][1]=i+mid-l+1;
	}
	else{
		for(int i=l+1;i<=mid;i++)ANS[++CNT][0]=i,ANS[CNT][1]=i+mid-l;
	}
}
int main(){
	int n;
	scanf("%d",&n);
	solve(1,n);
	printf("%d\n",CNT);
	for(int i=1;i<=CNT;i++)printf("%d %d\n",ANS[i][0],ANS[i][1]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
vector<P> ANS;
void solve(int l,int r){
	if(l==r)return;
	int mid=(l+r)>>1;
	solve(l,mid),solve(mid+1,r);
	for(int i=l;i<=mid;i++)ANS.emplace_back(i,i+mid-l+1);
}
int main(){
	int n;
	scanf("%d",&n);
	int m=1<<__lg(n);
	solve(1,m),solve(n-m+1,n);
	printf("%d\n",int(ANS.size()));
	for(P i:ANS)printf("%d %d\n",i.first,i.second);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 15004;
const int INF = 0x3f3f3f3f;

pii p[MAXN];

int main() {
    int n; scanf("%d", &n);
    int tot = 0;
    int m = log(n) / log(2);
    int up = 1 << m;
    for(int i = 0; i < m; ++i) {
        int gap = 1 << i;
        for(int j = 1; j < up; j += gap << 1) {
            for(int k = 0; k < gap; ++k)
                p[tot++] = {j + k, j + k + gap};
        }
    }
    int now = n - up + 1;
    for(int i = 0; i < m; ++i) {
        int gap = 1 << i;
        for(int j = now; j < n; j += gap << 1) {
            for(int k = 0; k < gap; ++k)
                p[tot++] = {j + k, j + k + gap};
        }
    }
    printf("%d\n", tot);
    for(int i = 0; i < tot; ++i)
        printf("%d %d\n", p[i].first, p[i].second);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 500004;
const int INF = 0x3f3f3f3f;

pii p[MAXN];

int main() {
    int n; scanf("%d", &n);
    int tot = 0;
    int m = log(n) / log(2);
    int up = 1 << m;
    for(int i = 0; i < m; ++i) {
        int gap = 1 << i;
        for(int j = 1; j < up; j += gap << 1) {
            for(int k = 0; k < gap; ++k)
                p[tot++] = {j + k, j + k + gap};
        }
    }
    int now = n - up + 1;
    for(int i = 0; i < m; ++i) {
        int gap = 1 << i;
        for(int j = now; j < n; j += gap << 1) {
            for(int k = 0; k < gap; ++k)
                p[tot++] = {j + k, j + k + gap};
        }
    }
    printf("%d\n", tot);
    for(int i = 0; i < tot; ++i)
        printf("%d %d\n", p[i].first, p[i].second);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int bel[30010];
int cnt,n;
int siz(int k) {
	int ret = 0;
	for(int i=1;i<=n;++i) ret += bel[i]==k;
	return ret;
}

struct pac {
	int l,r;
}; vector<pac>op;

void init() {
	int nloc = 20;
	int nplu = 0;
	while(nloc>=0) {
		if(n&(1<<nloc)) {
			for(int ly = 1;ly <= nloc;++ly) {
				for(int nw = nplu + 1;nw <= nplu + (1<<nloc);nw += (1<<ly)) {
					for(int i=0;i<(1<<(ly-1));++i) {
						op.push_back({nw+i,nw + (1<<(ly-1))+i});
					}
				}
			} ++cnt;
			for(int i=nplu + 1;i <= nplu + (1<<nloc);++i)
				bel[i] = cnt;
			nplu += 1<<nloc;
		} nloc --;
	}
}
// p1 > p2

inline void tak(int k,queue<int>&a) {
	for(int i=1;i<=n;++i)
		if(bel[i]==k)
			a.push(i);
}

void merge(int p1,int p2) {
	queue<int>sth;
	tak(p2,sth);
	int nloc = 0;
	++cnt;
	while(!sth.empty()) {
		int nw = sth.front(); sth.pop();
		while(bel[nloc]!=p1) ++nloc;
		op.push_back({nw,nloc});
		bel[nloc]=bel[nw]=cnt;
	} return;
}

void opp() {
	printf("%d\n",op.size());
	for(int i=0;i<op.size();++i) {
		printf("%d %d\n",op[i].l,op[i].r);
	}
	/*
	for(int i=1;i<=n;++i) {
		printf("%d ",bel[i]);
	}
	*/
}

void solve() {
	init();
	int lef = cnt-1,rig = cnt;
	while(lef > 1) {
		if(siz(lef) == siz(rig)) {
			merge(lef,rig);
			rig ++, lef --;
		}
		else {
			merge(1,rig);
			rig++;
		}
	}
	opp();
}

int main() {
	cin>>n;
	solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int bel[30010];
int cnt,n;
int siz(int k) {
	int ret = 0;
	for(int i=1;i<=n;++i) ret += bel[i]==k;
	return ret;
}

struct pac {
	int l,r;
}; vector<pac>op;

void init() {
	int nloc = 20;
	int nplu = 0;
	while(nloc>=0) {
		if(n&(1<<nloc)) {
			for(int ly = 1;ly <= nloc;++ly) {
				for(int nw = nplu + 1;nw <= nplu + (1<<nloc);nw += (1<<ly)) {
					for(int i=0;i<(1<<(ly-1));++i) {
						op.push_back({nw+i,nw + (1<<(ly-1))+i});
					}
				}
			} ++cnt;
			for(int i=nplu + 1;i <= nplu + (1<<nloc);++i)
				bel[i] = cnt;
			nplu += 1<<nloc;
		} nloc --;
	}
}
// p1 > p2

inline void tak(int k,queue<int>&a) {
	for(int i=1;i<=n;++i)
		if(bel[i]==k)
			a.push(i);
}

void merge(int p1,int p2) {
	queue<int>sth;
	tak(p2,sth);
	int nloc = 0;
	++cnt;
	while(!sth.empty()) {
		int nw = sth.front(); sth.pop();
		while(bel[nloc]!=p1) ++nloc;
		op.push_back({nw,nloc});
		bel[nloc]=bel[nw]=cnt;
	} return;
}

void opp() {
	printf("%d\n",op.size());
	for(int i=0;i<op.size();++i) {
		printf("%d %d\n",op[i].l,op[i].r);
	}
	/*
	for(int i=1;i<=n;++i) {
		printf("%d ",bel[i]);
	}
	*/
}

void solve() {
	init();
	int lef = cnt-1,rig = cnt;
	while(lef != 1) {
		if(siz(lef) == siz(rig)) {
			merge(lef,rig);
			rig ++, lef --;
		}
		else {
			merge(1,rig);
			rig++;
		}
	}
	opp();
}

int main() {
	cin>>n;
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;

int n;
vector<pair<int,int>> ans;

void f(int a, int b)
{
    if(a==b)
        return;
    f(a,(a+b)/2);
    f(1+(a+b)/2,b);
    for(int i = 0; i<(b-a+1)/2; i++)
        ans.push_back({a+i+1,(a+b)/2+i+2});
}

int main()
{
    cin >> n;
    int k = 0;
    while((1<<(k+1))<=n)
        k++;
    f(0,(1<<k)-1);
    f(n-(1<<k),n-1);
    cout << ans.size() << endl;
    for(auto x: ans)
        cout << x.f << " " << x.s << endl;
}


#include <bits/stdc++.h>
#define f first
#define s second

using namespace std;

int n;
vector<pair<int,int>> ans;

void f(int a, int b)
{
    if(a==b)
        return;
    f(a,(a+b)/2);
    f(1+(a+b)/2,b);
    for(int i = 0; i<(b-a+1)/2; i++)
        ans.push_back({a+i+1,(a+b)/2+i+2});
}

int main()
{
    cin >> n;
    int k = 0;
    while((1<<(k+1))<=n)
        k++;
    f(0,(1<<k)-1);
    for(int i = 0; i<n-(1<<k); i++)
        ans.push_back({i+1,+(1<<k)+1});
    cout << ans.size() << endl;
    for(auto x: ans)
        cout << x.f << " " << x.s << endl;
}


#include <bits/stdc++.h>
#define MAXR 500000
int r1[MAXR], r2[MAXR], dr;

int main(){
//  freopen("in.in", "r", stdin);
//  freopen("out.out", "w", stdout);
  int n, i, j, j2, ci, a, b;
  scanf("%d", &n);
  for(j = 0; (1 << j) < n; j++){
    i = 0;
    j2 = (1 << j);
    while(i + j2 < n){
      for(ci = 0; ci < j2; ci++){
        if(i + j2 + ci < n){
          r1[dr] = i + ci;
          r2[dr] = i + ci + j2;
          dr++;
        }
      }
      i += 2 * j2;
    }
  }
  printf("%d\n", dr);
  for(i = 0; i < dr; i++){
    printf("%d %d\n", r1[i] + 1, r2[i] + 1);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
const int N=1e6+10,M=2e3+10;
int n,c,mx,mn,kd;
bool ok;
P ans[N];
void solve(int l,int r){
	if(l==r)return;
	int mid=(l+r)/2;
	solve(l,mid);
	solve(mid+1,r);
	for(int i=l,j=mid+1;i<=mid;i++,j++){
		ans[++c]=P(i,j);
	}
}
int main(){
	scanf("%d",&n);
	for(int i=14;i>=0;--i){
		if(n>>i&1){
			solve(1,1<<i);
			solve(n-(1<<i)+1,n);
			break;
		}
	}
	printf("%d\n",c);
	for(int i=1;i<=c;++i){
		P x=ans[i];
		printf("%d %d\n",x.first,x.second);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
const int N=1e6+10,M=2e3+10;
int n,c,mx,mn,kd;
bool ok;
vector<int>now[15];
P ans[N];
void solve(int l,int r){
	if(l==r)return;
	int mid=(l+r)/2;
	solve(l,mid);
	solve(mid+1,r);
	for(int i=l,j=mid+1;i<=mid;i++,j++){
		ans[++c]=P(i,j);
	}
}
int main(){
	scanf("%d",&n);
	mn=15;
	for(int i=14;i>=0;--i){
		if(n>>i&1){
			kd++;
			solve(n-(1<<i)+1,n);
			for(int j=n-(1<<i)+1;j<=n;++j){
				now[i].push_back(j);
			}
			mx=max(mx,i);
			mn=min(mn,i);
			n-=(1<<i);
		}
	}
	//printf("kd:%d\n",kd);
	while(kd>2){
		for(int i=0;i<(1<<mn);++i){
			int v=now[mx].back();
			now[mx].pop_back();
			now[mn].push_back(v);
			ans[++c]=P(v,now[mn][i]);
		}
		while(kd>2 && !now[mn+1].empty()){
			for(int i=0;i<(1<<(mn+1));i++){
				ans[++c]=P(now[mn+1][i],now[mn][i]);
				now[mn+1].push_back(now[mn][i]);
			}
			mn++;
			kd--;
			//printf("mn:%d kd:%d\n",mn,kd);
		}
		if(now[mn+1].empty()){
			for(int i=0;i<(1<<(mn+1));i++){
				now[mn+1].push_back(now[mn][i]);
			}
			mn++;
		}
	}
	printf("%d\n",c);
	for(int i=1;i<=c;++i){
		P x=ans[i];
		printf("%d %d\n",x.first,x.second);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
int n;
vector<pair<int,int> > ans,A;
vector<pair<int,int> > solve(int l,int r) {
	vector<pair<int,int> > res,A; res.clear();
	if (l+1==r) {
		res.push_back(make_pair(l,r));
		return res;
	}
	int mid=(l+r)/2;
	A=solve(l,mid);
	for (int i=0;i<(int)A.size();i++) res.push_back(A[i]);
	for (int i=0;i<(int)A.size();i++) res.push_back(make_pair(A[i].first+mid+1-l,A[i].second+mid+1-l));
	for (int i=l;i<=mid;i++) res.push_back(make_pair(i,i+mid+1-l));
	return res;
}

int main() {
	//freopen("1.txt","r",stdin);
	read(n);
	int x=1; while (x*2<=n) x*=2;
	ans=solve(1,x);
	if (x<n) {
		A=solve(n-x+1,n);
		for (int i=0;i<(int)A.size();i++) ans.push_back(A[i]);
	}
	printf("%d\n",(int)ans.size());
	for (int i=0;i<(int)ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second);
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/


#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
int n;
vector<pair<int,int> > ans,A;
vector<pair<int,int> > solve(int l,int r) {
	vector<pair<int,int> > res,A; res.clear();
	if (l==r) return res;
	if (l+1==r) {
		res.push_back(make_pair(l,r));
		return res;
	}
	int mid=(l+r)/2;
	A=solve(l,mid);
	for (int i=0;i<(int)A.size();i++) res.push_back(A[i]);
	for (int i=0;i<(int)A.size();i++) res.push_back(make_pair(A[i].first+mid+1-l,A[i].second+mid+1-l));
	for (int i=l;i<=mid;i++) res.push_back(make_pair(i,i+mid+1-l));
	return res;
}

int main() {
	//freopen("1.txt","r",stdin);
	read(n);
	int x=1; while (x*2<=n) x*=2;
	ans=solve(1,x);
	if (x<n) {
		A=solve(n-x+1,n);
		for (int i=0;i<(int)A.size();i++) ans.push_back(A[i]);
	}
	printf("%d\n",(int)ans.size());
	for (int i=0;i<(int)ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second);
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/


//This Code was made by Chinese_zjc_.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <ctime>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n;
vector<pair<int, int>> ans;
struct cmp
{
    bool operator()(const set<int> &A, const set<int> &B)
    {
        return A.size() < B.size();
    }
};
multiset<pair<int, set<int>>> g;
set<int> biggest;
void Merge(multiset<pair<int, set<int>>>::iterator A, multiset<pair<int, set<int>>>::iterator B)
{
    set<int> nxt;
    for (set<int>::iterator i = A->second.begin(), j = B->second.begin(); i != A->second.end() && j != B->second.end(); ++i, ++j)
    {
        nxt.insert({*i, *j});
        ans.push_back({*i, *j});
    }
    g.insert({nxt.size(), nxt});
    g.erase(A);
    g.erase(B);
}
void up(multiset<pair<int, set<int>>>::iterator A)
{
    set<int> nxt;
    for (set<int>::iterator i = A->second.begin(); i != A->second.end(); ++i)
    {
        nxt.insert({*i, *biggest.begin()});
        ans.push_back({*i, *biggest.begin()});
        biggest.erase(biggest.begin());
    }
    g.erase(A);
    g.insert({nxt.size(), nxt});
}
signed main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
        g.insert({1, set<int>({i})});
    }
    for (multiset<pair<int, set<int>>>::iterator i = g.begin(); i != g.end();)
    {
        if (i == g.begin())
        {
            ++i;
        }
        multiset<pair<int, set<int>>>::iterator j = i;
        --j;
        if (i->first == j->first)
        {
            Merge(i++, j);
        }
        else
        {
            ++i;
        }
    }
    biggest = g.rbegin()->second;
    g.erase(g.find({biggest.size(), biggest}));
    while (g.size() > 1)
    {
        up(g.begin());
        Merge(g.begin(), ++g.begin());
    }
    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}


//This Code was made by Chinese_zjc_.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <ctime>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n;
vector<pair<int, int>> ans;
struct cmp
{
    bool operator()(const set<int> &A, const set<int> &B)
    {
        return A.size() < B.size();
    }
};
multiset<pair<int, set<int>>> g;
set<int> biggest;
void Merge(multiset<pair<int, set<int>>>::iterator A, multiset<pair<int, set<int>>>::iterator B)
{
    set<int> nxt;
    for (set<int>::iterator i = A->second.begin(), j = B->second.begin(); i != A->second.end() && j != B->second.end(); ++i, ++j)
    {
        nxt.insert({*i, *j});
        ans.push_back({*i, *j});
    }
    g.insert({nxt.size(), nxt});
    g.erase(A);
    g.erase(B);
}
void up(multiset<pair<int, set<int>>>::iterator A)
{
    set<int> nxt;
    for (set<int>::iterator i = A->second.begin(); i != A->second.end(); ++i)
    {
        nxt.insert({*i, *biggest.begin()});
        ans.push_back({*i, *biggest.begin()});
        biggest.erase(biggest.begin());
    }
    g.erase(A);
    g.insert({nxt.size(), nxt});
}
signed main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    if (n == 1)
    {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 1; i <= n; ++i)
    {
        g.insert({1, set<int>({i})});
    }
    for (multiset<pair<int, set<int>>>::iterator i = g.begin(); i != g.end();)
    {
        if (i == g.begin())
        {
            ++i;
        }
        multiset<pair<int, set<int>>>::iterator j = i;
        --j;
        if (i->first == j->first)
        {
            Merge(i++, j);
        }
        else
        {
            ++i;
        }
    }
    biggest = g.rbegin()->second;
    g.erase(g.find({biggest.size(), biggest}));
    while (g.size() > 1)
    {
        up(g.begin());
        Merge(g.begin(), ++g.begin());
    }
    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}


//This Code was made by Chinese_zjc_.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <ctime>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n;
vector<pair<int, int>> ans;
struct cmp
{
    bool operator()(const set<int> &A, const set<int> &B)
    {
        return A.size() < B.size();
    }
};
multiset<pair<int, set<int>>> g;
set<int> biggest;
void Merge(multiset<pair<int, set<int>>>::iterator A, multiset<pair<int, set<int>>>::iterator B)
{
    set<int> nxt;
    for (set<int>::iterator i = A->second.begin(), j = B->second.begin(); i != A->second.end() && j != B->second.end(); ++i, ++j)
    {
        nxt.insert({*i, *j});
        ans.push_back({*i, *j});
    }
    g.insert({nxt.size(), nxt});
    g.erase(A);
    g.erase(B);
}
void up(multiset<pair<int, set<int>>>::iterator A)
{
    set<int> nxt;
    for (set<int>::iterator i = A->second.begin(); i != A->second.end(); ++i)
    {
        nxt.insert({*i, *biggest.begin()});
        ans.push_back({*i, *biggest.begin()});
        biggest.erase(biggest.begin());
    }
    g.erase(A);
    g.insert({nxt.size(), nxt});
}
signed main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    if (n == 1)
    {
        cout << 0 << endl;
        return 0;
    }
    for (int i = 1; i <= n; ++i)
    {
        g.insert({1, set<int>({i})});
    }
    for (multiset<pair<int, set<int>>>::iterator i = g.begin(); i != g.end();)
    {
        if (i == g.begin())
        {
            ++i;
        }
        multiset<pair<int, set<int>>>::iterator j = i;
        --j;
        if (i->first == j->first)
        {
            Merge(i++, j);
        }
        else
        {
            ++i;
        }
    }
    biggest = g.rbegin()->second;
    g.erase(g.find({biggest.size(), biggest}));
    while (g.size() > 1)
    {
        up(g.begin());
        while (g.size() > 1 && g.begin()->first == (++g.begin())->first)
        {
            Merge(g.begin(), ++g.begin());
        }
    }
    cout << ans.size() << endl;
    for (auto i : ans)
    {
        cout << i.first << ' ' << i.second << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<pair<int, int>> ans;

void do_job(int l, int r)
{
	if (r - l <= 1) return;
	for (int i = l; i < l + (r - l) / 2; i++)
	{
		ans.push_back({i + 1, i + (r - l) / 2 + 1});
	}
	do_job(l, l + (r - l) / 2);
	do_job(l + (r - l) / 2, r);
	return;
}

int main()
{
	int n;
	cin >> n;
	int pw = 1;
	while (pw <= n) pw *= 2;
	pw /= 2;
	do_job(0, pw);
	do_job(pw, n);
	cout << ans.size() << '\n';
	for (pair<int, int> p : ans) cout << p.first << " " << p.second << '\n';
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

vector<pair<int, int>> ans;

void do_job(int l, int r)
{
	if (r - l <= 1) return;
	for (int i = l; i < l + (r - l) / 2; i++)
	{
		ans.push_back({i + 1, i + (r - l) / 2 + 1});
	}
	do_job(l, l + (r - l) / 2);
	do_job(l + (r - l) / 2, r);
	return;
}

int main()
{
	int n;
	cin >> n;
	int pw = 1;
	while (pw <= n) pw *= 2;
	pw /= 2;
	do_job(0, pw);
	do_job(n - pw, n);
	cout << ans.size() << '\n';
	for (pair<int, int> p : ans) cout << p.first << " " << p.second << '\n';
	return 0;
}


#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
using namespace std;
ll n;
vector<pair<ll,ll> > ans;
void rep(ll l,ll r)
{
	if(l==r)
	{
		return;
	}
	ll mid=(l+r)>>1,i;
	rep(l,mid);
	rep(mid+1,r);
	for(i=l;i<=mid&&i+mid-l<=r;i++)
	{
		ans.push_back(make_pair(i,mid+i-l+1));
	}
	return;
}
int main(){
	ll i;
	scanf("%lld",&n);
	for(i=30;i>=0;i--)
	{
		if(n&((ll)1<<i))
		{
			break;
		}
	}
	rep(1,((ll)1<<i));
	rep(n-((ll)1<<i)+1,n);
	printf("%lld\n",(ll)ans.size());
	for(i=0;i<ans.size();i++)
	{
		printf("%lld %lld\n",ans[i].F,ans[i].S);
	}
	return 0;
}


#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
using namespace std;
ll n;
vector<pair<ll,ll> > ans;
void rep(ll l,ll r)
{
	if(l==r)
	{
		return;
	}
	ll mid=(l+r)>>1,i;
	rep(l,mid);
	rep(mid+1,r);
	for(i=l;i<=mid&&i+mid-l<=r;i++)
	{
		ans.push_back(make_pair(i,mid+i-l+1));
	}
	return;
}
int main(){
	ll i;
	scanf("%lld",&n);
	for(i=30;i>=0;i--)
	{
		if(n&((ll)1<<i))
		{
			break;
		}
	}
	rep(1,((ll)1<<i));
	rep(n-((ll)1<<i)+1,n);
	printf("%lld\n",ans.size());
	for(i=0;i<ans.size();i++)
	{
		printf("%lld %lld\n",ans[i].F,ans[i].S);
	}
	return 0;
}


/*

*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstdlib>
#include<stack>
using namespace std;
#define ll long long
#define N 500003
#define INF 0x3f3f3f3f
#define FOR(i, st, en) for(i=st; i<en; i++)
#define printD(n) printf("%d", n)
#define printS(s) printf("%s", s)
#define readD(n) scanf("%d", &n)
#define readS(s) scnaf("%s", &s)
#define lson rt<<1, l, mid
#define rson (rt<<1)|1, mid+1, r
#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
#define mkp make_pair
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
//#define _DEBUG_
int n, cnt=0, num;
pair<int, int>ret[N];
void go(int l, int r, int d){
	int i, j;
	if(d==0 || r+l<2*d || num<=2)
		return;
	for(i=l; i+2*d-1<=r; i+=2*d){
		for(j=0; j<d; j++){
			ret[cnt++] = mkp(i+j, i+j+d);
		}
	}
	go(l, r, d*2);
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int i, j, t, k;

	#ifdef _DEBUG_
	freopen("F Two Different.in", "r", stdin);
	//freopen(".out", "w", stdout);
	#endif

	cin>>n;
	k=2;
	while(k<=n)
		k*=2;
	k/=2;
	cnt = 0, num=n;
	go(1, k, 1);
	if(n>k)
		go(n-k+1, n, 1);
	cout<<cnt<<endl;
	for(i=0; i<cnt; i++)
		cout<<ret[i].first<<' '<<ret[i].second<<endl;
	return 0;
}


/*
AC 265ms
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstdlib>
#include<stack>
using namespace std;
#define ll long long
#define N 500003
#define INF 0x3f3f3f3f
#define FOR(i, st, en) for(i=st; i<en; i++)
#define printD(n) printf("%d", n)
#define printS(s) printf("%s", s)
#define readD(n) scanf("%d", &n)
#define readS(s) scnaf("%s", &s)
#define lson rt<<1, l, mid
#define rson (rt<<1)|1, mid+1, r
#define max(a, b) ((a)>(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
#define mkp make_pair
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
//#define _DEBUG_
int n, cnt=0, num;
pair<int, int>ret[N];
void go(int l, int r, int d){
	int i, j;
	if(d==0 || r+l<2*d || num<=2)
		return;
	for(i=l; i+2*d-1<=r; i+=2*d){
		for(j=0; j<d; j++){
			ret[cnt++] = mkp(i+j, i+j+d);
		}
	}
	go(l, r, d*2);
}
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int i, j, t, k;

	#ifdef _DEBUG_
	freopen("F Two Different.in", "r", stdin);
	//freopen(".out", "w", stdout);
	#endif

	cin>>n;
	k=2;
	while(k<=n)
		k*=2;
	k/=2;
	cnt = 0, num=n;
	go(1, k, 1);
	if(n>k)
		go(n-k+1, n, 1);
	cout<<cnt<<endl;
	for(i=0; i<cnt; i++)
		cout<<ret[i].first<<' '<<ret[i].second<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int read()
{
    int a=0;char b=1,c;
    do if((c=getchar())==45)b=-1;while(!(c&16));
    do a=(a<<3)+(a<<1)+(c&15);while((c=getchar())&16);
    return a*b;
}
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    char a[10],s=0;
    do a[s++]=x%10|48;while(x/=10);
    do putchar(a[--s]);while(s);
}
int a[N],b[N],c[N];
int main()
{
    int n=read(),m=n>>1;
    printf("%d\n",m);
	for(int i=1;i<=m;++i)printf("%d %d\n",i,n+1-i);
}


#include<bits/stdc++.h>
using namespace std;
const int N=1E5+1;
int read()
{
    int a=0;char b=1,c;
    do if((c=getchar())==45)b=-1;while(!(c&16));
    do a=(a<<3)+(a<<1)+(c&15);while((c=getchar())&16);
    return a*b;
}
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    char a[10],s=0;
    do a[s++]=x%10|48;while(x/=10);
    do putchar(a[--s]);while(s);
}
void work(int l,int r,int n)
{
	for(int i=1,m=n>>1;i<=m;++i)
	{
		int k=i<<1;
		for(int j=l;j<=r;j+=k)for(int z=0;z<i;++z)printf("%d %d\n",j+z,j+z+i);
	}
}
int main()
{
    int n=read(),k=0;
 	while(1<<k+1<=n)++k;
	if(1<<k==n)printf("%d\n",n/2*k),work(1,n,n);
	else printf("%d\n",(1<<k)*k),work(1,1<<k,1<<k),work(n-(1<<k)+1,n,1<<k);
}


#include<bits/stdc++.h>
using namespace std;
const int N=1E5+1;
int read()
{
    int a=0;char b=1,c;
    do if((c=getchar())==45)b=-1;while(!(c&16));
    do a=(a<<3)+(a<<1)+(c&15);while((c=getchar())&16);
    return a*b;
}
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    char a[10],s=0;
    do a[s++]=x%10|48;while(x/=10);
    do putchar(a[--s]);while(s);
}
void work(int l,int r,int n)
{
	for(int i=1,m=n>>1;i<=m;i<<=1)
	{
		int k=i<<1;
		for(int j=l;j<=r;j+=k)for(int z=0;z<i;++z)printf("%d %d\n",j+z,j+z+i);
	}
}
int main()
{
    int n=read(),k=0;
 	while(1<<k+1<=n)++k;
	if(1<<k==n)printf("%d\n",n/2*k),work(1,n,n);
	else printf("%d\n",(1<<k)*k),work(1,1<<k,1<<k),work(n-(1<<k)+1,n,1<<k);
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
vector<pair<int,int> >ans;

void sl(int x,int kk){
    int len=(1<<kk);
    for(int i=1;i<len;i*=2){
        for(int j=0;j<len;j+=i*2){
            for(int k=0;k<i;k++){
                ans.push_back({x+j+k,x+j+k+i});
            }
        }
    }
}

int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int n;
    scanf("%d",&n);
    int k=__lg(n);
    sl(1,k);
    sl(n-(1<<k)+1,k);
    printf("%d\n",(int)ans.size());
    for(auto it: ans){
        printf("%d %d\n",it.first,it.second);
    }
    return 0;
}


#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define lowbit(x) x & -x
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+5;
vector<pair<int,int> >ans;

void sl(int x,int kk){
    int len=(1<<kk);
    for(int i=1;i<len;i*=2){
        for(int j=0;j<len;j+=i*2){
            for(int k=0;k<=i;k++){
                ans.push_back({x+j+k,x+j+k+i});
            }
        }
    }
}

int main(){
#ifdef Mizp
    freopen("in.txt","r",stdin);
#endif
    int n;
    scanf("%d",&n);
    int k=__lg(n);
    sl(1,k);
    sl(n-(1<<k)+1,k);
    printf("%d\n",(int)ans.size());
    for(auto it: ans){
        printf("%d %d\n",it.first,it.second);
    }
    return 0;
}


#include"bits/stdc++.h"
using namespace std;
#define ri register int
#define ll long long
#define N 500010
int n,tot,x[N],y[N];
inline void solve(int l,int r){
	if(l==r-1){tot++,x[tot]=l,y[tot]=r;return;}
	int mid=(l+r)>>1;
	solve(l,mid),solve(mid+1,r);
	for(ri i=l,j=mid+1;i<=mid&&j<=r;++i,++j) tot++,x[tot]=i,y[tot]=j;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	if(n==1||n==2){cout<<0<<endl;return 0;}int bas=1;
	while(bas<=n) bas*=2;
	bas/=2;
	solve(1,bas);
	solve(n-bas+1,n);
	cout<<tot<<endl;
	for(ri i=1;i<=tot;++i) cout<<x[i]<<" "<<y[i]<<endl;
	cout<<tot<<endl;
	return 0;
}


#include"bits/stdc++.h"
using namespace std;
#define ri register int
#define ll long long
#define N 500010
int n,tot,x[N],y[N];
inline void solve(int l,int r){
	if(l==r-1){tot++,x[tot]=l,y[tot]=r;return;}
	int mid=(l+r)>>1;
	solve(l,mid),solve(mid+1,r);
	for(ri i=l,j=mid+1;i<=mid&&j<=r;++i,++j) tot++,x[tot]=i,y[tot]=j;
}
int main(){
	ios::sync_with_stdio(false);
	cin>>n;
	if(n==1||n==2){cout<<0<<endl;return 0;}int bas=1;
	while(bas<=n) bas*=2;
	bas/=2;
	solve(1,bas);
	solve(n-bas+1,n);
	cout<<tot<<endl;
	for(ri i=1;i<=tot;++i) cout<<x[i]<<" "<<y[i]<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , tot , x[550000] , y[550000] , k;
queue< int > q[30];
inline void match( int a , int b )
{
	for(int k = 2 ; k <= b - a + 1 ; k <<= 1 )
		for(int i = a ; i <= b ; i += k )
			for(int j = i ; j < i + k / 2 ; j++ )
				x[++tot] = i , y[tot] = i + k / 2;
}
int main()
{
	scanf("%d" , &n);
	int k = 1 << (int)log2(n - 1);
	match(1 , k); match(n - k + 1 , n);
	printf("%d\n" , tot);
	for(int i = 1 ; i <= tot ; i++ ) printf("%d %d\n" , x[i] , y[i]);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n , tot , x[550000] , y[550000] , k;
queue< int > q[30];
inline void match( int a , int b )
{
	for(int k = 2 ; k <= b - a + 1 ; k <<= 1 )
		for(int i = a ; i <= b ; i += k )
			for(int j = i ; j < i + k / 2 ; j++ )
				x[++tot] = j , y[tot] = j + k / 2;
}
int main()
{
	scanf("%d" , &n);
	int k = 1 << (int)log2(n - 1);
	match(1 , k); match(n - k + 1 , n);
	printf("%d\n" , tot);
	for(int i = 1 ; i <= tot ; i++ ) printf("%d %d\n" , x[i] , y[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int x[500100],y[500100],tot;
int q;
void deal(int l,int r)
{
    if(l==r)  return ;
    if(l==r-1)
    {
        x[++tot]=l;y[tot]=r;
        q++;return;
    }
    int mid=l+r>>1;
    deal(l,mid);
    deal(mid+1,r);
    int ll=mid-l+1,rr=r-mid;
    if(ll==rr)
    {
        q+=ll;
        for(int i=0;i<ll;i++)  x[++tot]=l+i,y[tot]=mid+i+1;
    }
    else
    {
        q+=min(ll,rr)+ll+rr-1;
        ll=mid,rr=r;
        while(ll>=l && rr>mid)
        {
            x[++tot]=ll,y[tot]=rr;ll--;rr--;
        }
        for(int i=l;i<r;i++)  x[++tot]=i,y[tot]=i+1;

    }
}
int main()
{
    int n;
    cin>>n;
    if(n<3)  cout<<0<<endl;
    else
    {
        deal(1,n/2);
        deal(n/2+1,n);
        cout<<q<<endl;
        for(int i=1;i<=tot;i++)
        {
            printf("%d %d\n",x[i],y[i]);
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
int x[500100],y[500100],tot;
int q;
void deal(int l,int r)
{
    if(l==r)  return ;
    if(l==r-1)
    {
        x[++tot]=l;y[tot]=r;
        q++;return;
    }
    int mid=l+r>>1;
    deal(l,mid);
    deal(mid+1,r);
    int ll=mid-l+1,rr=r-mid;
    q+=ll;
    for(int i=0;i<ll;i++)  x[++tot]=l+i,y[tot]=mid+i+1;
}
int main()
{
    int n;
    cin>>n;
    if(n<3)  cout<<0<<endl;
    else
    {
        long long k=1;
        while((k<<1)<n)
        {
            k<<=1;
        }
        deal(1,k);
        deal(n-k+1,n);
        cout<<q<<endl;
        for(int i=1;i<=tot;i++)
        {
            printf("%d %d\n",x[i],y[i]);
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N=15000,M=500000;

int n;

void into(){
  scanf("%d",&n);
}

int ca,ans[M+9][2];

void Divide_ans(int l,int r){
  if (l==r) return;
  int mid=l+r>>1;
  Divide_ans(l,mid);
  Divide_ans(mid+1,r);
  for (int i=l,j=mid+1;i<=mid;++i,++j) ans[++ca][0]=i,ans[ca][1]=j;
}

void Get_ans(){
  int len=1;
  for (;len<=n;len<<=1);
  len>>=1;
  Divide_ans(1,len);
  Divide_ans(n-len+1,n);
}

void work(){
  Get_ans();
}

void outo(){
  printf("%d\n",ca);
  for (int i=1;i<=ca;++i)
	printf("%d %d\n",ans[i][0],ans[i][1]);
}

int main(){
  int T=1;
  for (;T--;){
    into();
    work();
    outo();
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N=15000;

int n;

void into(){
  scanf("%d",&n);
}

void Divide_ans(int l,int r){
  if (l==r) return;
  int mid=l+r>>1;
  Divide_ans(l,mid);
  Divide_ans(mid+1,r);
  for (int i=l,j=mid+1;i<=mid;++i,++j)
	printf("%d %d\n",i,j);
}

void Get_ans(){
  int len=1;
  for (;len<=n;len<<=1);
  len>>=1;
  Divide_ans(1,len);
  Divide_ans(n-len+1,n);
}

void work(){
  Get_ans();
}

void outo(){
}

int main(){
  int T=1;
  for (;T--;){
    into();
    work();
    outo();
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	cout<<n<<endl;
	int num = 0;
	while(n>=(1<<num)) num++;
	--num;  //这样子保证每一边都有
	// cout<<num<<endl;
// 一颗高度是K的完全二叉树
// 叶子节点的个数是2^(k-1)
	// k+1的高度，只需要K次合并
	cout<<(1<<num)*num<<endl;
	// int cnt = 0;
	rep(i,0,num){
		for(int j=0; j<(1<<num)-1; j+=2*(1<<i)){
			for(int k=j; k<(j+(1<<i)); k++){
				cout<<k<<" "<<k+(1<<i)<<endl;
				// cnt++;
			}
		}
	}
	rep(i,0,num){
		for(int j=0; j<(1<<num)-1; j+=2*(1<<i)){
			for(int k=j; k<(j+(1<<i)); k++){
				cout<<n-k-1<<" "<<n-k-1-(1<<i)<<endl;
				// cnt++;
			}
		}
	}
	// cout<<(1<<num)*num<<" "<<cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	cout<<n<<endl;
	int num = 0;
	while(n>=(1<<num)) num++;
	--num;  //这样子保证每一边都有
	// cout<<num<<endl;
// 一颗高度是K的完全二叉树
// 叶子节点的个数是2^(k-1)
	// k+1的高度，只需要K次合并
	cout<<(1<<num)*num<<endl;
	// int cnt = 0;
	rep(i,0,num){
		for(int j=0; j<(1<<num)-1; j+=2*(1<<i)){
			for(int k=j; k<(j+(1<<i)); k++){
				cout<<k+1<<" "<<k+1+(1<<i)<<endl;
				// cnt++;
			}
		}
	}
	rep(i,0,num){
		for(int j=0; j<(1<<num)-1; j+=2*(1<<i)){
			for(int k=j; k<(j+(1<<i)); k++){
				cout<<n-k<<" "<<n-k-(1<<i)<<endl;
				// cnt++;
			}
		}
	}
	// cout<<(1<<num)*num<<" "<<cnt<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(LL i=(j); i<(k); ++i)
#define pb push_back
#define PII pair<int,int>
#define PLL pair<long long, long long>
#define ini(a,j) memset(a,j,sizeof a)
#define rrep(i,j,k) for(LL i=j; i>=k; --i)
#define fi first
#define se second
#define LL long long
#define beg begin()
#define ed end()
#define all(x) x.begin(),x.end()

LL mod = 1e9+7;
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	int num = 0;
	while(n>=(1<<num)) num++;
	--num;  //这样子保证每一边都有
	// cout<<num<<endl;
// 一颗高度是K的完全二叉树
// 叶子节点的个数是2^(k-1)
	// k+1的高度，只需要K次合并
	cout<<(1<<num)*num<<endl;
	// int cnt = 0;
	rep(i,0,num){
		for(int j=0; j<(1<<num)-1; j+=2*(1<<i)){
			for(int k=j; k<(j+(1<<i)); k++){
				cout<<k+1<<" "<<k+1+(1<<i)<<endl;
				// cnt++;
			}
		}
	}
	rep(i,0,num){
		for(int j=0; j<(1<<num)-1; j+=2*(1<<i)){
			for(int k=j; k<(j+(1<<i)); k++){
				cout<<n-k<<" "<<n-k-(1<<i)<<endl;
				// cnt++;
			}
		}
	}
	// cout<<(1<<num)*num<<" "<<cnt<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAXN = 5e5+10;
int a[MAXN];

int gainHigh(int x){
    int ans = 0;
    while(x){
        x >>= 1;
        ans++;
    }
    return ans-1;
}

void solve(){
    int n;   scanf("%d", &n);
    int high = gainHigh(n);
    int t = 1 << high;
    printf("%d\n", t * high);
    for(int i = 1; i <= high; i++){
        int d = 1<<(i-1);
        for(int nows = 1; nows <= t; nows+= d*2){
            for(int k = 0; k < d; k++){
                printf("%d %d\n", nows+k, nows+k+d);
            }
        }
    }
    for(int i = 1; i <= high; i++){
        int d = 1<<(i-1);
        for(int nows = 1; nows <= t; nows+= d*2){
            for(int k = 0; k < d; k++){
                printf("%d %d\n", n+1-(nows+k), n+1-(nows+k+d));
            }
        }
    }
}

int main(){
    int casen = 1; //scanf("%d", &casen);
    while(casen--)  solve();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAXN = 3e6;

int n, cnt;
int x[MAXN], y[MAXN];

void dfs(int xx){
    for(int i = 1; i <= n; i += xx*2){
        if(i+xx+xx-1>n) break;
        for(int j = 0; j < xx; j++){
            x[++cnt] = i+j;
            y[cnt] = i+j+xx;
        }
    }
}

void solve(){
    scanf("%d", &n);
    cnt = 0;
    for(int i = 1; i <= n; i*=2){
        dfs(i);
    }
    printf("%d\n", 2*cnt);
    for(int i = 1; i <= cnt; i++){
        printf("%d %d\n", x[i], y[i]);
    }
    for(int i = 1; i <= cnt; i++){
        printf("%d %d\n", n+1-x[i], n+1-y[i]);
    }
}

int main(){
    int casen = 1; //scanf("%d", &casen);
    while(casen--)  solve();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAXN = 5e5+10;
int a[MAXN];

int gainHigh(int x){
    int ans = 0;
    while(x){
        x >>= 1;
        ans++;
    }
    return ans;
}

void solve(){
    int n;   scanf("%d", &n);
    int high = gainHigh(n)-1;
    int t = 1 << high;
    int cnt = 1 << (high);
    cnt *= (high);
    printf("%d\n", cnt);
    for(int i = 1; i <= high; i++){
        int d = 1<<(i-1);
        for(int nows = 1; nows <= t; nows+= d*2){
            for(int k = 0; k < d; k++){
                printf("%d %d\n", nows+k, nows+k+d);
                cnt++;
            }
        }
    }
    for(int i = 1; i <= high; i++){
        int d = 1<<(i-1);
        for(int nows = 1; nows <= t; nows+= d*2){
            for(int k = 0; k < d; k++){
                printf("%d %d\n", n+1-(nows+k), n+1-(nows+k+d));
                cnt++;
            }
        }
    }
}

int main(){
    int casen = 1; //scanf("%d", &casen);
    while(casen--)  solve();
    return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

int a[500005];
int b[500005];

int main()
{
    int n;
    cin >> n;
    if (n <= 1)
        cout << 0 << endl;
    else
    {
        a[0] = 1;
        b[0] = 2;
        int num = 1;
        int x = 2;
        while (x < n)
        {
            for (int i = 0; i < num; i++)
            {
                a[num + i] = a[i] + x;
                b[num + i] = b[i] + x;
            }
            num += num;
            for (int i = 1; i <= x; i++)
            {
                a[num] = i;
                b[num] = i + x;
                num++;
            }
            x*=2;
        }
        x /= 2;
        x = n - x;
        cout << 2 * num << endl;
        for (int i = 0; i < num; i++)
        {
            cout << a[i] << " " << b[i] << endl;
        }
        for (int i = 0; i < num; i++)
        {
            cout << a[i] + x << " " << b[i] + x << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

const int MAXN = 3e6;

int n, cnt;
int x[MAXN], y[MAXN];

void dfs(int xx){
    for(int i = 1; i <= n; i += xx*2){
        if(i+xx+xx-1>n) break;
        for(int j = 0; j < xx; j++){
            x[++cnt] = i+j;
            y[cnt] = i+j+xx;
        }
    }
}

void solve(){
    scanf("%d", &n);
    cnt = 0;
    for(int i = 1; i <= 2*n; i*=2){
        dfs(i);
    }
    printf("%d\n", 2*cnt);
    for(int i = 1; i <= cnt; i++){
        printf("%d %d\n", x[i], y[i]);
    }
    for(int i = 1; i <= cnt; i++){
        printf("%d %d\n", n+1-x[i], n+1-y[i]);
    }
}

int main(){
    int casen = 1; //scanf("%d", &casen);
    while(casen--)  solve();
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#define pii pair<int,int>
#define mp  make_pair
#define fi  first
#define se  second
using namespace std;

const int MAXN = 5e5+10;
pii a[MAXN];

void solve(){
    int n;   scanf("%d", &n);
    if(n <= 1){
        printf("0\n");
        return ;
    }
    a[1] = mp(1, 2);
    int cnt = 1;
    int d = 2;
    while(d*2 <= n){
        // 复制一份
        for(int i = 1; i <= cnt; i++){
            a[cnt+i] = mp(a[i].fi+d, a[i].se+d);
        }
        // 两份两两合并
        cnt <<= 1;
        for(int i = 1; i <= d; i++){
            a[++cnt] = mp(i, i+d);
        }
        d <<= 1;
    }
    printf("%d\n", cnt*2);
    for(int i = 1; i <= cnt; i++){
        printf("%d %d\n", a[i].fi, a[i].se);
    }
    for(int i = 1; i <= cnt; i++){
        printf("%d %d\n", n+1-a[i].fi, n+1-a[i].se);
    }
}

int main(){
    int casen = 1; //scanf("%d", &casen);
    while(casen--)  solve();
    return 0;
}


#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
namespace ran{
const int mod=1e9+7,k1=17,k2=53,mak=2147483647;
const int mak1=0x57b8ce1d,mak2=0x2af4c7e1;
ll sa,sb;
inline ll kasumi(ll a,ll b,bool bo=1){
  ll s=1;
  for (;b;b>>=1,a=bo?a*a%mod:a*a&mak)
    if (b&1) s=bo?s*a%mod:s*a&mak;
  return s;
  }
inline ll rnd(){
  sa=(sa*k1+k2)%mod*kasumi(sa+2,sb+2)%mod;
  sb=(sb*kasumi(sb+2,sa+2)%mod*k2+k1)%mod;
  return (sa+sb)%mod;
  }
inline ll rndll(){
  sa=((kasumi(sa^mak1,sb^mak2,0)^rnd())*k1+k2)&mak;
  sb=((kasumi(sb^mak1,sa^mak2,0)^rnd())*k2+k1)&mak;
  return sa<<30|sb;
  }
inline ll rnd(ll l,ll r){
  return l+rnd()%(r-l+1);
  }
inline ll rndll(ll l,ll r){
  return l+rndll()%(r-l+1);
  }
inline void init(){
  srand(time(0));
  sa=rand()^mak1,sb=rand()^mak2;
  rnd(),rnd(),rnd();
  }
}using ran::rnd;using ran::rndll;
const int yuzu=5e5;
typedef int fuko[yuzu|10];
fuko a,q1,q2;
typedef pair<int,int> pii;
vector<pii> lxy;
void dfs(int l,int r) {
  if (l==r) return;
  int mid=l+r>>1,i,j;
  dfs(l,mid),dfs(mid+1,r);
  for (i=l,j=mid+1;i<=mid;++i,++j)
    lxy.push_back(pii(i,j));
}
int main() {
  int n=read(),i,j,k=0,l;
  for (i=0;(1<<i)<=n;++i); --i;
  dfs(1,1<<i),dfs(n-(1<<i)+1,n);
  printf("%d\n",(int)lxy.size());
  for (pii x:lxy) printf("%d %d\n",x.first,x.second);
}


#include<bits/stdc++.h> //Ithea Myse Valgulious
namespace chtholly{
typedef long long ll;
#define re0 register int
#define rel register ll
#define rec register char
#define gc getchar
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?-1:*p1++)
#define pc putchar
#define p32 pc(' ')
#define pl puts("")
/*By Citrus*/
char buf[1<<23],*p1=buf,*p2=buf;
inline int read(){
  int x=0,f=1;char c=gc();
  for (;!isdigit(c);c=gc()) f^=c=='-';
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return f?x:-x;
  }
template <typename mitsuha>
inline bool read(mitsuha &x){
  x=0;int f=1;char c=gc();
  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';
  if (!~c) return 0;
  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');
  return x=f?x:-x,1;
  }
template <typename mitsuha>
inline int write(mitsuha x){
  if (!x) return 0&pc(48);
  if (x<0) pc('-'),x=-x;
  int bit[20],i,p=0;
  for (;x;x/=10) bit[++p]=x%10;
  for (i=p;i;--i) pc(bit[i]+48);
  return 0;
  }
inline char fuhao(){
  char c=gc();
  for (;isspace(c);c=gc());
  return c;
  }
}using namespace chtholly;
using namespace std;
namespace ran{
const int mod=1e9+7,k1=17,k2=53,mak=2147483647;
const int mak1=0x57b8ce1d,mak2=0x2af4c7e1;
ll sa,sb;
inline ll kasumi(ll a,ll b,bool bo=1){
  ll s=1;
  for (;b;b>>=1,a=bo?a*a%mod:a*a&mak)
    if (b&1) s=bo?s*a%mod:s*a&mak;
  return s;
  }
inline ll rnd(){
  sa=(sa*k1+k2)%mod*kasumi(sa+2,sb+2)%mod;
  sb=(sb*kasumi(sb+2,sa+2)%mod*k2+k1)%mod;
  return (sa+sb)%mod;
  }
inline ll rndll(){
  sa=((kasumi(sa^mak1,sb^mak2,0)^rnd())*k1+k2)&mak;
  sb=((kasumi(sb^mak1,sa^mak2,0)^rnd())*k2+k1)&mak;
  return sa<<30|sb;
  }
inline ll rnd(ll l,ll r){
  return l+rnd()%(r-l+1);
  }
inline ll rndll(ll l,ll r){
  return l+rndll()%(r-l+1);
  }
inline void init(){
  srand(time(0));
  sa=rand()^mak1,sb=rand()^mak2;
  rnd(),rnd(),rnd();
  }
}using ran::rnd;using ran::rndll;
const int yuzu=5e5;
typedef int fuko[yuzu|10];
fuko a,q1,q2;
typedef pair<int,int> pii;
map<pii,int> mp;
int main() {
  int n=read(),i,j,k=0,l;
  for (i=1;i<=n;i<<=1) {
    for (j=1;j<=n;j+=i<<1) {
      for (l=j;l<j+i&&l+i<=n;++l) q1[++k]=l,q2[k]=l+i;
    }
  }
  for (i=1;i<=n;i<<=1) {
    for (j=n;j>0;j-=i<<1) {
      for (l=j;l>j-i&&l-i>0;--l) q1[++k]=l,q2[k]=l-i;
    }
  }
  printf("%d\n",k);
  for (i=1;i<=k;++i) printf("%d %d\n",q1[i],q2[i]);
}


#include<bits/stdc++.h>
using namespace std;

#define int long long

#define pii pair<int,int>  /**/
#define fs first
#define sc second
//#define x first
//#define y second
#define mpr make_pair
#define pb push_back
#define vi vector<int>  /**/
#define vp vector<pii>  /**/
#define si set<int>  /**/
#define sp set<pii>  /**/
#define mii map<int,int>  /**/
#define mpi map<pii,int>  /**/
#define mip map<int,pii>  /**/
#define mpp map<pii,pii>  /**/
#define iter iterator
#define all(V) V.begin(),V.end()
#define srt(V) sort(all(V))
#define sau(V) srt(V),V.resize(unique(all(V))-V.begin())
#define rvs(V) reverse(all(V))
#define ginit(R,C,val) vector<vi>(R,vi(C,val))
#define deb(V) cout<<#V<<"="<<V<<endl
#define de1(V,ZO,TEND) 													\
	{ 																	\
		cout<<#V<<"=["; 												\
		for(int cmFpbnk9=ZO;cmFpbnk9<TEND+ZO;cmFpbnk9++)				\
			cout<<V[cmFpbnk9]<<","; 									\
		cout<<"]"<<endl; 												\
	}
#define de2(V,ZO,ROW,COLUMN)											\
	{																	\
		int WIDTH=1;													\
		for(int lmssb_=ZO;lmssb_<ROW+ZO;lmssb_++)						\
		{																\
			for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)						\
			{															\
				int LWIDTH=0,TVAL=V[lmssb_][lmgw];						\
				while(TVAL)												\
				{														\
					LWIDTH++;											\
					TVAL/=10;											\
				}														\
				if(V[lmssb_][lmgw]<0)									\
					WIDTH=max(WIDTH,LWIDTH+1);							\
				else													\
					WIDTH=max(WIDTH,LWIDTH);							\
			}															\
		}																\
		int TWIDTH=max((int)2,(int)strlen(#V));							\
		cout<<#V;														\
		for(int _23367M=1;_23367M<=TWIDTH-strlen(#V);_23367M++)			\
			cout<<" ";													\
		cout<<"|";														\
		for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)							\
			cout<<setw(WIDTH+1)<<left<<lmgw;							\
		cout<<endl;														\
		for(int _23367M=1;_23367M<=TWIDTH;_23367M++)					\
			cout<<"-";													\
		cout<<"+";														\
		for(int _23367M=1;_23367M<=(WIDTH+1)*COLUMN;_23367M++)			\
			cout<<"-";													\
		cout<<endl;														\
		for(int lmssb_=ZO;lmssb_<ROW+ZO;lmssb_++)						\
		{																\
			cout<<setw(TWIDTH)<<lmssb_<<"|";							\
			for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)						\
				cout<<setw(WIDTH+1)<<left<<V[lmssb_][lmgw];				\
			cout<<endl;													\
		}																\
	}
#define de3(V,ZO,ROW,COLUMN)											\
	{																	\
		cout<<#V<<"[][]="<<endl;										\
		for(int lmssb_=ZO;lmssb_<ROW+ZO;lmssb_++)						\
		{																\
			for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)						\
				cout<<V[lmssb_][lmgw]<<" ";								\
			cout<<endl;													\
		}																\
	}

template<typename T1,typename T2>
istream& operator >> (istream& is,pair<T1,T2>& V)
{
	is>>V.fs>>V.sc;
	return is;
}

template<typename T>
istream& operator >> (istream& is,vector<T>& V)
{
	for(int i=0;i<V.size();i++) is>>V[i];
	return is;
}

template<typename T1,typename T2>
ostream& operator << (ostream& os,pair<T1,T2>& V)
{
	os<<"("<<V.fs<<","<<V.sc<<")";
	return os;
}

template<typename T>
ostream& operator << (ostream& os,vector<T>& V)
{
	os<<"[";
	for(auto i:V) os<<i<<" ";
	os<<"]";
	return os;
}

template<typename T>
ostream& operator << (ostream& os,set<T>& V)
{
	os<<"{";
	for(auto i:V) os<<i<<" ";
	os<<"}";
	return os;
}

template<typename T1,typename T2>
ostream& operator << (ostream& os,map<T1,T2>& V)
{
	os<<"{";
	for(auto i:V) os<<i<<" ";
	os<<"}";
	return os;
}

template<typename T>
inline void cmin(T& x,T y)
{
	x=min(x,y);
}

template<typename T>
inline void cmax(T& x,T y)
{
	x=max(x,y);
}

const int inf=1000000007ll;
const int mod=1000000007ll;

signed main()
{
	ios::sync_with_stdio(false);
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);

	int N; cin>>N;
	int ML=1;
	while(ML<=N) ML<<=1;
	ML>>=1;
	vp Q;
	for(int L=2;L<=ML;L<<=1)
	{
		for(int i=1;i<=ML;i+=L)
		{
			for(int j=i;j<i+(L>>1);j++)
			{
				Q.pb(mpr(j,j+(L>>1)));
			}
		}
		for(int i=N-ML+1;i<=N;i+=L)
		{
			for(int j=i;j<i+(L>>1);j++)
			{
				Q.pb(mpr(j,j+(L>>1)));
			}
		}
	}
	cout<<Q.size()<<endl;
	for(auto i:Q) cout<<i.fs<<" "<<i.sc<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long

#define pii pair<int,int>  /**/
#define fs first
#define sc second
//#define x first
//#define y second
#define mpr make_pair
#define pb push_back
#define vi vector<int>  /**/
#define vp vector<pii>  /**/
#define si set<int>  /**/
#define sp set<pii>  /**/
#define mii map<int,int>  /**/
#define mpi map<pii,int>  /**/
#define mip map<int,pii>  /**/
#define mpp map<pii,pii>  /**/
#define iter iterator
#define all(V) V.begin(),V.end()
#define srt(V) sort(all(V))
#define sau(V) srt(V),V.resize(unique(all(V))-V.begin())
#define rvs(V) reverse(all(V))
#define ginit(R,C,val) vector<vi>(R,vi(C,val))
#define deb(V) cout<<#V<<"="<<V<<endl
#define de1(V,ZO,TEND) 													\
	{ 																	\
		cout<<#V<<"=["; 												\
		for(int cmFpbnk9=ZO;cmFpbnk9<TEND+ZO;cmFpbnk9++)				\
			cout<<V[cmFpbnk9]<<","; 									\
		cout<<"]"<<endl; 												\
	}
#define de2(V,ZO,ROW,COLUMN)											\
	{																	\
		int WIDTH=1;													\
		for(int lmssb_=ZO;lmssb_<ROW+ZO;lmssb_++)						\
		{																\
			for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)						\
			{															\
				int LWIDTH=0,TVAL=V[lmssb_][lmgw];						\
				while(TVAL)												\
				{														\
					LWIDTH++;											\
					TVAL/=10;											\
				}														\
				if(V[lmssb_][lmgw]<0)									\
					WIDTH=max(WIDTH,LWIDTH+1);							\
				else													\
					WIDTH=max(WIDTH,LWIDTH);							\
			}															\
		}																\
		int TWIDTH=max((int)2,(int)strlen(#V));							\
		cout<<#V;														\
		for(int _23367M=1;_23367M<=TWIDTH-strlen(#V);_23367M++)			\
			cout<<" ";													\
		cout<<"|";														\
		for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)							\
			cout<<setw(WIDTH+1)<<left<<lmgw;							\
		cout<<endl;														\
		for(int _23367M=1;_23367M<=TWIDTH;_23367M++)					\
			cout<<"-";													\
		cout<<"+";														\
		for(int _23367M=1;_23367M<=(WIDTH+1)*COLUMN;_23367M++)			\
			cout<<"-";													\
		cout<<endl;														\
		for(int lmssb_=ZO;lmssb_<ROW+ZO;lmssb_++)						\
		{																\
			cout<<setw(TWIDTH)<<lmssb_<<"|";							\
			for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)						\
				cout<<setw(WIDTH+1)<<left<<V[lmssb_][lmgw];				\
			cout<<endl;													\
		}																\
	}
#define de3(V,ZO,ROW,COLUMN)											\
	{																	\
		cout<<#V<<"[][]="<<endl;										\
		for(int lmssb_=ZO;lmssb_<ROW+ZO;lmssb_++)						\
		{																\
			for(int lmgw=ZO;lmgw<COLUMN+ZO;lmgw++)						\
				cout<<V[lmssb_][lmgw]<<" ";								\
			cout<<endl;													\
		}																\
	}

template<typename T1,typename T2>
istream& operator >> (istream& is,pair<T1,T2>& V)
{
	is>>V.fs>>V.sc;
	return is;
}

template<typename T>
istream& operator >> (istream& is,vector<T>& V)
{
	for(int i=0;i<V.size();i++) is>>V[i];
	return is;
}

template<typename T1,typename T2>
ostream& operator << (ostream& os,pair<T1,T2>& V)
{
	os<<"("<<V.fs<<","<<V.sc<<")";
	return os;
}

template<typename T>
ostream& operator << (ostream& os,vector<T>& V)
{
	os<<"[";
	for(auto i:V) os<<i<<" ";
	os<<"]";
	return os;
}

template<typename T>
ostream& operator << (ostream& os,set<T>& V)
{
	os<<"{";
	for(auto i:V) os<<i<<" ";
	os<<"}";
	return os;
}

template<typename T1,typename T2>
ostream& operator << (ostream& os,map<T1,T2>& V)
{
	os<<"{";
	for(auto i:V) os<<i<<" ";
	os<<"}";
	return os;
}

template<typename T>
inline void cmin(T& x,T y)
{
	x=min(x,y);
}

template<typename T>
inline void cmax(T& x,T y)
{
	x=max(x,y);
}

const int inf=1000000007ll;
const int mod=1000000007ll;

signed main()
{
	ios::sync_with_stdio(false);
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);

	int N; cin>>N;
	int ML=1;
	while(ML<=N) ML<<=1;
	ML>>=1;
	vp Q;
	for(int L=2;L<=ML;L<<=1)
	{
		for(int i=1;i<=ML;i+=L)
		{
			for(int j=i;j<i+(L>>1);j++)
			{
				Q.pb(mpr(j,j+(L>>1)));
			}
		}
	}
	for(int L=2;L<=ML;L<<=1)
	{
		for(int i=N-ML+1;i<=N;i+=L)
		{
			for(int j=i;j<i+(L>>1);j++)
			{
				Q.pb(mpr(j,j+(L>>1)));
			}
		}
	}
	cout<<Q.size()<<endl;
	for(auto i:Q) cout<<i.fs<<" "<<i.sc<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<vector>
#define INF 1e9+7
#define ll long long
using namespace std;
int T,n;
struct node
{
    int v,i;
	vector <int> a;
}a[65537];
struct Mdf
{
	int x,y;
}mdf[500001];
int ar,br,ansn,al,bl;
bool operator <(const node &T1,const node &T2){return T1.v>T2.v;}
priority_queue <node> b;
void solve()
{
    scanf("%d",&n);ar=br=0;
    for(int i=1;i<=n;i++)
    {
      ar++;
      a[ar].v=1;a[ar].i=i;
      a[ar].a.push_back(i);
	}
    node t1,t2,tn;al=bl=1;
    while(al<=ar)
    {
      if(al==ar)
      {
      	tn=a[al];
		b.push(a[al++]);
        break;
	  }
	  t1=a[al];t2=a[al+1];
	  if(t1.v==t2.v)
	  {
	    for(int i=0;i<t1.v;i++)mdf[++ansn]=(Mdf){t1.a[i],t2.a[i]};
	    ar++;
	    a[ar].v=t1.v*2;a[ar].i=t1.i;
	    for(int i=0;i<t1.v;i++){a[ar].a.push_back(t1.a[i]);a[ar].a.push_back(t2.a[i]);}
	    al+=2;
	  }
	  else
	  {
	  	tn=t1;
	    b.push(t1);
	    al++;
	  }
	}
	int anr=0;
    while(!b.empty())
    {
      if(b.top().i==tn.i)b.pop();
      if(b.empty())break;
      t1=b.top();b.pop();
      if(b.empty())break;
      if(b.top().i==tn.i)b.pop();
      if(b.empty())break;
      t2=b.top();b.pop();
      while(t1.v!=t2.v)
      {
      	for(int i=0;i<t1.v;i++)mdf[++ansn]=(Mdf){t1.a[i],tn.a[anr+i]};
		anr+=t1.v;
        t1.v*=2;
	  }
	  for(int i=0;i<t2.v;i++)t1.a.push_back(t2.a[i]);
	  t1.v*=2;
	  b.push(t1);
	}
	printf("%d\n",ansn);
	for(int i=1;i<=ansn;i++)printf("%d %d\n",mdf[i].x,mdf[i].y);
}
int main()
{
	//scanf("%d",&T);
	T=1;
	while(T--)
	{
	  solve();
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<vector>
#define INF 1e9+7
#define ll long long
using namespace std;
int T,n;
struct node
{
    int v,i;
	vector <int> a;
}a[65537];
struct Mdf
{
	int x,y;
}mdf[500001];
int ar,br,ansn,al,bl;
bool operator <(const node &T1,const node &T2){return T1.v>T2.v;}
priority_queue <node> b;
void solve()
{
    scanf("%d",&n);ar=br=0;
    for(int i=1;i<=n;i++)
    {
      ar++;
      a[ar].v=1;a[ar].i=i;
      a[ar].a.push_back(i);
	}
    node t1,t2,tn;al=bl=1;
    while(al<=ar)
    {
      if(al==ar)
      {
      	tn=a[al];
		b.push(a[al++]);
        break;
	  }
	  t1=a[al];t2=a[al+1];
	  if(t1.v==t2.v)
	  {
	    for(int i=0;i<t1.v;i++)mdf[++ansn]=(Mdf){t1.a[i],t2.a[i]};
	    ar++;
	    a[ar].v=t1.v*2;a[ar].i=t1.i;
	    for(int i=0;i<t1.v;i++){a[ar].a.push_back(t1.a[i]);a[ar].a.push_back(t2.a[i]);}
	    al+=2;
	  }
	  else
	  {
	  	tn=t1;
	    b.push(t1);
	    al++;
	  }
	}
	int anr=0;
    while(!b.empty())
    {
      if(b.top().i==tn.i)b.pop();
      if(b.empty())break;
      t1=b.top();b.pop();
      if(b.empty())break;
      if(b.top().i==tn.i)b.pop();
      if(b.empty())break;
      t2=b.top();b.pop();
      while(t1.v!=t2.v)
      {
      	for(int i=0;i<t1.v;i++)
		{
		  mdf[++ansn]=(Mdf){t1.a[i],tn.a[anr+i]};
		  t1.a.push_back(tn.a[anr+i]);
	    }
		anr+=t1.v;
        t1.v*=2;
	  }
	  for(int i=0;i<t2.v;i++)
	  {
	  	mdf[++ansn]=(Mdf){t1.a[i],t2.a[i]};
	    t1.a.push_back(t2.a[i]);
	  }
	  t1.v*=2;
	  b.push(t1);
	}
	printf("%d\n",ansn);
	for(int i=1;i<=ansn;i++)printf("%d %d\n",mdf[i].x,mdf[i].y);
}
int main()
{
	//scanf("%d",&T);
	T=1;
	while(T--)
	{
	  solve();
	}
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int n , cd;
vector <int> ruler;
void do_it(int l , int r)
{
	if (l == r)
	{
		return;
	}
	do_it(l , l + (-l + r  + 1) / 2 - 1);
	do_it(l + (r - l + 1) / 2 , r);
	int t = l , t1 = r;
	while (t <= t1)
	{
		ruler.push_back(t++);
		ruler.push_back(t1--);
	}
}
int main()
{
	cin >> cd;
	int kq = 1 , x = 1;
	for (int i = 1 ; i <= 20 ; i++)
	{
		x *= 2;
		if (x <= cd) kq = x;
	}
	do_it(1 , kq);
	do_it(cd - kq +1 , cd);
	cout << ruler.size() / 2 <<'\n';
	for (int i = 0 ; i < ruler.size() ; i += 2)
	{
		cout << ruler[i]<<" "<<ruler[i+1]<<'\n';
	}
}


#include <iostream>
#include <vector>
using namespace std;
int n , cd;
vector <int> ruler;
int main()
{
	cin >> cd;
	cd -= cd % 2;
	for (int i = cd ; i >= 1 ; i-=2)
	{
		ruler.push_back(i);
		ruler.push_back(i-1);
	}
	for (int i = 3 ; i <= cd ; i += 2)
	{
		if (i == cd)
		{
			break;
		}
		int x = ruler.back();
		int y = ruler[ruler.size() - 2];
		ruler.push_back(x);
		ruler.push_back(i);

		ruler.push_back(y);
		ruler.push_back(i+1);
	}
	 cout << ruler.size() / 2 <<'\n';
	 for (int i = 0 ; i < ruler.size() ; i += 2)
	 {
		cout << ruler[i]<<" "<<ruler[i+1]<<'\n';
	 }
}


#include <iostream>
#include <vector>
using namespace std;
int n , cd;
vector <int> ruler;
int main()
{
	cin >> cd;
	ruler.push_back(1);
	ruler.push_back(2);
	for (int i = 3 ; i <= cd ; i += 2)
	{
		int x = ruler.back();
		int y = ruler[ruler.size() - 2];
		ruler.push_back(x);
		ruler.push_back(i);
		ruler.push_back(y);
		ruler.push_back(i+1);
	}
	 cout << ruler.size() / 2 <<'\n';
	 for (int i = 0 ; i < ruler.size() ; i += 2)
	 {
		cout << ruler[i]<<" "<<ruler[i+1]<<'\n';
	 }
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

template <typename T>
inline void chkmax(T &x, T y) { if (x < y) x = y; }

template <typename T>
inline void chkmin(T &x, T y) { if (x > y) x = y; }

inline int read() {
  #define nc getchar()
  int x = 0;
  char c = nc;
  while (c < 48) c = nc;
  while (c > 47) x = x * 10 + (c ^ 48), c = nc;
  return x;
  #undef nc
}

typedef long long ll;
typedef pair <int, int> pii;
const int maxn = 15010;
int n;

vector <pii> ans;

void run(int k, int de) {
  rep(i, 0, k - 1) {
    rep(j, 1, 1 << k) {
      rep(t, 0, (1 << i) - 1) {
        int x = j + t + de, y = j + t + (1 << i) + de;
        ans.push_back({x, y});
      }
      j += (1 << (i + 1)) - 1;
    }
  }
}

vector <int> vec[16];

void unite(vector <int> &v1, vector <int> &v2) {
  vector <int> tmp;
  for (int y : v1) {
    int x = v2.back();
    v2.pop_back(), ans.push_back({x, y}), tmp.push_back(x);
  }
  for (int x : tmp) v1.push_back(x);
}

void solve() {
  cin >> n;
  int cur = 1;
  per(i, 15, 0) if (n >> i & 1) {
    run(i, cur - 1);
    rep(j, 1, (1 << i)) vec[i].push_back(cur++);
  }
  static int id[16];
  rep(i, 0, 15) id[i] = i;
  sort(id, id + 16, [] (int x, int y) {
    return (int) vec[x].size() > (int) vec[y].size();
  });
  while (1) {
    sort(id + 1, id + 16, [] (int x, int y) {
      return (int) vec[x].size() > (int) vec[y].size();
    });
    int p1 = id[0], p2 = 0, p3 = 0;
    rep(i, 1, 15) if (!vec[i].empty()) p2 = p3, p3 = id[i];
    if (!p2) break;
    while (vec[p2].size() > vec[p3].size()) {
      unite(vec[p3], vec[p1]);
    }
    unite(vec[p3], vec[p2]);
  }
  printf("%d\n", (int) ans.size());
  for (pii p : ans) printf("%d %d\n", p.first, p.second);
}

signed main() {
//  int T = read();
//  while (T--) solve();
  solve();
  return 0;
}


import java.util.Scanner;

public class solution {
	public static void main (String args[]){
	    Scanner sc = new Scanner(System.in);
	    int cases = sc.nextInt();
	    for (int i = 0; i < cases; i++) {
	    	double a = sc.nextInt();
		    double b = sc.nextInt();
		    System.out.println(a>b? Math.pow(2*b,2):Math.pow(2*a,2));
	    }
	    sc.close();
	}
}


import java.util.Scanner;

public class Main
{

	public static void main(String args[])
	{
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0)
        {
            int a=s.nextInt();
            int b=s.nextInt();
            if(a==1 && b==1)
            System.out.println(4);
            else if(a==1)
            {
                System.out.println(b*b);
            }
            else if(b==1)
            {
                System.out.println(a*a);
            }
            else
            {
                int x=Math.min(a, b);
                x=x*2;
                System.out.println(x*x);
            }

        }
	}
}

// this code is contributed by devilishvineeth


import java.util.Scanner;

public class Main
{

	public static void main(String args[])
	{
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0)
        {
            int a=s.nextInt();
            int b=s.nextInt();
            if(a==1 && b==1)
            System.out.println(4);
            else if(a==1)
            {
                System.out.println(b*b);
            }
            else if(b==1)
            {
                System.out.println(a*a);
            }
            else
            {
                int x=1;
                if(a>b)
                {
                x=b*2;
                if(x>=a)
                System.out.println(x*x);
                }
                else
                {
                    x=a*2;
                    System.out.println(x*x);
                }

            }

            }

	}
}

// this code is contributed by devilishvineeth


import java.util.Scanner;

public class Main
{

	public static void main(String args[])
	{
        Scanner s=new Scanner(System.in);
        int t=s.nextInt();
        while(t-->0)
        {
            int a=s.nextInt();
            int b=s.nextInt();
           int x=Math.min(a,b);
           int y=Math.max(a,b);
           int z=Math.max(x+x,y);
           System.out.println(z*z);

            }

	}
}

// this code is contributed by devilishvineeth


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();
            int max=0;
            if(a>b){
                int temp=a;
                a=b;
                b=temp;
            }
            System.out.println(Math.max(2*a,b)*Math.max(2*a,b));
        }
     }
}


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();

            if(a==1&&b==1||(a%10==0)&&(b%10==0)){
                System.out.println(4*a*b);
            }
            else if(a==1||b==1){
            System.out.println((int)Math.pow(Math.max(a,b),2));
            //continue;
            }
            else{
                int x=Math.min(a,b);
                System.out.println(2*x*2*x);
            }
        }
     }
}
/*16
16
4
9
64
9
64
40000*/


import java.util.*;
public class Solution{
     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();
            if(a==1||b==1){
            System.out.println((int)Math.pow(Math.max(a,b),2));
            //continue;
            }
            else if(a==1&&b==1||(a%10==0)&&(b%10==0)){
                System.out.println(4*a*b);
            }
            else{
                int x=Math.min(a,b);
                System.out.println(2*x*2*x);
            }
        }
     }
}


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();
            int max=0;
            if(a>b){
                int temp=a;
                a=b;
                b=a;
            }
            System.out.println(Math.max(2*a,b)*Math.max(2*a,b));
            }
        }
     }
}


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();

            if(a==1&&b==1||(a%10==0)&&(b%10==0)){
                System.out.println(4*a*b);
            }
            else if (a==b){
                System.out.println((a+b)*(a+b));
            }
            else if(a==1||b==1){
            System.out.println((int)Math.pow(Math.max(a,b),2));
            //continue;
            }
            else{
                int x=Math.min(a,b);
                System.out.println(2*x*2*x);
            }
        }
     }
}
/*16
16
4
9
64
9
64
40000*/


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();

            if(a==1&&b==1||(a%10==0)&&(b%10==0)){
                System.out.println(4*a*b);
            }
            else if (a==b){
                System.out.println((2*a)*(2*a));
            }
            else if(a==1||b==1){
            System.out.println((int)Math.pow(Math.max(a,b),2));
            //continue;
            }
            else{
                int x=Math.min(a,b);
                int y=Math.max(a,b);
                int res1=(2*x)*(y+(Math.abs(y-(2*x))));
                int res2=(2*y)*(x+(Math.abs(x-(2*y))));
                System.out.println(Math.min(res1,res2));
            }
        }
     }
}


import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();

            if(a==1&&b==1||(a%10==0)&&(b%10==0)){
                System.out.println(4*a*b);
            }
            else if (a==b){
                System.out.println((a+b)*(a+b));
            }
            else if(a==1||b==1){
            System.out.println((int)Math.pow(Math.max(a,b),2));
            //continue;
            }
            else{
                int x=Math.min(a,b);
                int y=Math.max(a,b);
                int res1=(2*x)*(y+(Math.abs(y-(2*x))));
                int res2=(2*y)*(x+(Math.abs(x-(2*y))));
                System.out.println(Math.min(res1,res2));
            }
        }
     }
}
/*16
16
4
9
64
9
64
40000*/


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class dsal2 {
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
//        Scan = new Scanner(System.in);
        FastReader fr=new FastReader();
        StringBuffer ans=new StringBuffer();
        int t = fr.nextInt();
        while (t-- > 0) {
           long a=fr.nextLong();
           long b=fr.nextLong();
           long p;
           p=(long)2*a*b;
           if(a==b)p=(long)4*a*b;
           while(Math.sqrt(p)!=Math.ceil(Math.sqrt(p)))p++;
           ans.append(p+"\n");
        }
        System.out.println(ans);
        }

    }

/*
*3--1*8+0
* 5--2*(5*2+3*2)+1*(3*2+1*2)+0
* 7--3*(7*2+5*2)+2*(5*2+3*2)+1(3*2+1*2)=
* */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class dsal2 {
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
//        Scan = new Scanner(System.in);
        FastReader fr=new FastReader();
        StringBuffer ans=new StringBuffer();
        int t = fr.nextInt();
        while (t-- > 0) {
           long a=fr.nextLong();
           long b=fr.nextLong();
           long p;
           p=(long)2*a*b;
           if(a==b)p=(long)4*a*b;
           while(true)
           {if(Math.sqrt(p)==Math.ceil(Math.sqrt(p))&&Math.max(a,b)*2<=p&&Math.min(a,b)*2<=p)
               break;

               p++;
           }
           ans.append(p+"\n");
        }
        System.out.println(ans);
        }

    }

/*
*3--1*8+0
* 5--2*(5*2+3*2)+1*(3*2+1*2)+0
* 7--3*(7*2+5*2)+2*(5*2+3*2)+1(3*2+1*2)=
* */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class dsal2 {
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
//        Scan = new Scanner(System.in);
        FastReader fr=new FastReader();
        StringBuffer ans=new StringBuffer();
        int t = fr.nextInt();
        while (t-- > 0) {
           long a=fr.nextLong();
           long b=fr.nextLong();
           long p=(long)Math.pow(Math.min(Math.max(2*a,b),Math.max(2*b,a)),2);
           ans.append(p+"\n");
        }
        System.out.println(ans);
        }

    }

/*
*3--1*8+0
* 5--2*(5*2+3*2)+1*(3*2+1*2)+0
* 7--3*(7*2+5*2)+2*(5*2+3*2)+1(3*2+1*2)=
* */


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class dsal2 {
    static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    public static void main(String[] args) {
//        Scan = new Scanner(System.in);
        FastReader fr=new FastReader();
        StringBuffer ans=new StringBuffer();
        int t = fr.nextInt();
        while (t-- > 0) {
           long a=fr.nextLong();
           long b=fr.nextLong();
           long p;
           p=(long)2*a*b;
           if(a==b)p=(long)4*a*b;
           while(true)
           {if(Math.sqrt(p)==Math.ceil(Math.sqrt(p))&&Math.pow(Math.max(a,b),2)<=p)
               break;

               p++;
           }
           ans.append(p+"\n");
        }
        System.out.println(ans);
        }

    }

/*
*3--1*8+0
* 5--2*(5*2+3*2)+1*(3*2+1*2)+0
* 7--3*(7*2+5*2)+2*(5*2+3*2)+1(3*2+1*2)=
* */


import java.util.Arrays;
import java.util.Scanner;

public class kh_A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        for (int cas = 0; cas < q; cas++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int min = Math.min(a, b);
            int max = a + b - min;
            System.out.println(Math.max(min * 2, max) * Math.max(min * 2, max));
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class kh_A {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int q = sc.nextInt();
        for (int cas = 0; cas < q; cas++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int min = Math.max(a, b);
            int max = a + b - min;
            System.out.println(Math.max(min * 2, max) * Math.max(min * 2, max));
        }
    }
}