Language: C++

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define reg register
#define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
#define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
#define drep(a,b,c) for(reg int a=(b),a##_end_=(c); a>=a##_end_; --a )
#define debug(x) cerr<<#x<<" = "<<x<<endl;

template<class T>inline bool Max(T &a,T b) {
	return a<b?a=b,1:0;
}
template<class T>inline bool Min(T &a,T b) {
	return a>b?a=b,1:0;
}

inline int Read(void) {
	int res=0,flag=1;
	char c;
	while(c=getchar(),c<48||c>57)if(c=='-')flag=0;
	do res=(res<<1)+(res<<3)+(c^48);
	while(c=getchar(),c>=48&&c<=57);
	return flag?res:-res;
}

const int N=8e8+5,M=1e6+5,mod=1e9+7;
const int dx[4]= {1,-1,0,0},dy[4]= {0,0,1,-1};

int n,k,m,A[N],B[N];

int main(void) {
	n=Read(),k=Read();
	if(k>(n-1)*(n-2)/2)return puts("-1");
	int z;
	if(k==n) {
		printf("%d\n",n);
		ret(i,1,n)printf("%d %d\n",i,i+1);
		printf("%d %d",n,1);
	} else if(k+2<=n) {
		rep(i,1,k)m++,A[m]=i,B[m]=k+1;
		ret(i,1,k)rep(j,i+1,k)m++,A[m]=i,B[m]=j;
		A[++m]=k+1,B[m]=k+2;
		rep(i,k+3,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
		printf("%d\n",m);
		rep(i,1,m)printf("%d %d\n",A[i],B[i]);
	} else {
		for(reg int i=1; i<=n; i++) {
			if(i*(i-1)/2<=k&&(i+1)*i/2>k) {
				z=i;
				break;
			}
		}
		k=k-(z-1)*z/2;
		rep(i,2,z+1)m++,A[m]=i,B[m]=1;
		rep(i,z+2,z+1+k)m++,A[m]=i,B[m]=i-z;
		rep(i,z+2+k,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
		printf("%d\n",m);
		rep(i,1,m)printf("%d %d\n",A[i],B[i]);
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:39:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   39 |                 ret(i,1,n)printf("%d %d\n",i,i+1);
      |                     ^
a.cc:8:32: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                ^
a.cc:39:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   39 |                 ret(i,1,n)printf("%d %d\n",i,i+1);
      |                     ^
a.cc:8:38: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                      ^
a.cc:42:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   42 |                 rep(i,1,k)m++,A[m]=i,B[m]=k+1;
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:42:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   42 |                 rep(i,1,k)m++,A[m]=i,B[m]=k+1;
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:43:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   43 |                 ret(i,1,k)rep(j,i+1,k)m++,A[m]=i,B[m]=j;
      |                     ^
a.cc:8:32: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                ^
a.cc:43:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   43 |                 ret(i,1,k)rep(j,i+1,k)m++,A[m]=i,B[m]=j;
      |                     ^
a.cc:8:38: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                      ^
a.cc:43:31: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   43 |                 ret(i,1,k)rep(j,i+1,k)m++,A[m]=i,B[m]=j;
      |                               ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:43:31: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   43 |                 ret(i,1,k)rep(j,i+1,k)m++,A[m]=i,B[m]=j;
      |                               ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:45:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   45 |                 rep(i,k+3,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:45:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   45 |                 rep(i,k+3,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:45:33: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   45 |                 rep(i,k+3,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                                 ^
a.cc:8:32: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                ^
a.cc:45:33: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   45 |                 rep(i,k+3,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                                 ^
a.cc:8:38: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                      ^
a.cc:47:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 |                 rep(i,1,m)printf("%d %d\n",A[i],B[i]);
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:47:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   47 |                 rep(i,1,m)printf("%d %d\n",A[i],B[i]);
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:49:29: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   49 |                 for(reg int i=1; i<=n; i++) {
      |                             ^
a.cc:56:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   56 |                 rep(i,2,z+1)m++,A[m]=i,B[m]=1;
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:56:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   56 |                 rep(i,2,z+1)m++,A[m]=i,B[m]=1;
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:57:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   57 |                 rep(i,z+2,z+1+k)m++,A[m]=i,B[m]=i-z;
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:57:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   57 |                 rep(i,z+2,z+1+k)m++,A[m]=i,B[m]=i-z;
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:58:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   58 |                 rep(i,z+2+k,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:58:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   58 |                 rep(i,z+2+k,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
a.cc:58:35: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   58 |                 rep(i,z+2+k,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                                   ^
a.cc:8:32: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                ^
a.cc:58:35: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   58 |                 rep(i,z+2+k,n)ret(j,1,i)m++,A[m]=i,B[m]=j;
      |                                   ^
a.cc:8:38: note: in definition of macro 'ret'
    8 | #define ret(a,b,c) for(reg int a=(b),a##_end_=(c); a<a##_end_; ++a )
      |                                      ^
a.cc:60:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   60 |                 rep(i,1,m)printf("%d %d\n",A[i],B[i]);
      |                     ^
a.cc:7:32: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                ^
a.cc:60:21: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   60 |                 rep(i,1,m)printf("%d %d\n",A[i],B[i]);
      |                     ^
a.cc:7:38: note: in definition of macro 'rep'
    7 | #define rep(a,b,c) for(reg int a=(b),a##_end_=(c); a<=a##_end_; ++a )
      |                                      ^
/tmp/ccOGkynQ.o: in function `main':
a.cc:(.text+0x150): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x1b6): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x21d): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x27d): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x2ce): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x3cb): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x438): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x4a6): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
a.cc:(.text+0x4f7): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccOGkynQ.o
collect2: error: ld returned 1 exit status