#include<bits/stdc++.h>

using namespace std;
const int maxn =  1e6 + 10;
#define ll long long
#define ios std::ios::sync_with_stdio(false)
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
/*

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod

ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;}
bool CH(ll a,ll n,ll x,ll t)
{ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;}
bool Miller_Rabin(ll n)
{if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL));
ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;}

*/

#define int long long
#define sc(t) scanf("%lld",&t)
#define pr(ans) printf("%lld\n",ans)
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
typedef unsigned long long ULL;
const double pi = 3.1415926535897932384626;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
#define debug(x) cout << #x << " : " << x << '\n';
#define sb cout << "sb\n";
#define fi first
#define se second
#define pb(a) push_back(a)
#define cn cout << '\n'
#define mp(a , b) make_pair(a , b)
const double ep = 1e-6;

vector<int> one , two;
int l[maxn] , r[maxn];
signed main()
{
    ios;///60 10 50
    cin.tie(0);
    int n , k;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i ++){
        cin >> l[i] >> r[i];
    }
    sort(l + 1 , l + 1 + n);
    sort(r + 1 , r + 1 + n);
    int  cnt = 0 , cntl = 1 , cntr = 1;
    for(int i = 1 ; i <= 2 * n ; i ++){
        if(cntl >= n + 1 && cntr >= n + 1)break;
        if(cntl >= n + 1){
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
        else if(cntr >= n + 1){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else if(l[cntl] == r[cntr]){
            if(cnt == k - 1){
                one.pb(l[cntl]);
                two.pb(r[cntr]);
            }
            cntl ++ , cntr ++;
            i ++;
            continue;
        }
        else if(l[cntl] < r[cntr]){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else {
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
    }
    if(one.size() < two.size()){
        two.pb(r[n]);
    }
    cout << one.size() << '\n';
    for(int i = 0 ; i < one.size() ; i ++){
        cout << one[i] << ' ' <<two[i] << '\n';
    }
	return 0;
}
/*
18
2 3 3
*/


#include<bits/stdc++.h>

using namespace std;
const int maxn =  1e6 + 10;
#define ll long long
#define ios std::ios::sync_with_stdio(false)
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
/*

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod

ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;}
ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;}
bool CH(ll a,ll n,ll x,ll t)
{ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;}
bool Miller_Rabin(ll n)
{if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL));
ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;}

*/

#define int long long
#define sc(t) scanf("%lld",&t)
#define pr(ans) printf("%lld\n",ans)
ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;}//xµÄn´Î·½mod
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
typedef unsigned long long ULL;
const double pi = 3.1415926535897932384626;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int lcm(int a,int b){return a/gcd(a,b)*b;}
#define debug(x) cout << #x << " : " << x << '\n';
#define sb cout << "sb\n";
#define fi first
#define se second
#define pb(a) push_back(a)
#define cn cout << '\n'
#define mp(a , b) make_pair(a , b)
const double ep = 1e-6;

vector<int> one , two;
int l[maxn] , r[maxn];
map<int , int >ma;
signed main()
{
    ios;///60 10 50
    cin.tie(0);
    int n , k;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i ++){
        cin >> l[i] >> r[i];
        if(l[i] == r[i]){
            ma[l[i]] ++;
        }
    }
    sort(l + 1 , l + 1 + n);
    sort(r + 1 , r + 1 + n);
    int  cnt = 0 , cntl = 1 , cntr = 1;
    for(int i = 1 ; i <= 2 * n ; i ++){
        if(cntl >= n + 1 && cntr >= n + 1)break;
        if(cntl >= n + 1){
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
        else if(cntr >= n + 1){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else if(l[cntl] == r[cntr]){
            if(cnt + ma[l[cntl]] >= k &&  cnt < k){
                one.pb(l[cntl]);
                two.pb(r[cntr]);
            }
            if(ma[l[cntl]] > 0){
                cntl += ma[l[cntl]] , cntr += ma[l[cntl]];
            }
            else  cntl ++ , cntr ++;
            if(2 * ma[l[cntl]] - 1 > 0) i += 2 * ma[l[cntl]] - 1;
            continue;
        }
        else if(l[cntl] < r[cntr]){
            cnt ++;
            cntl ++;
            if(cnt == k){
                one.pb(l[cntl - 1]);
            }
        }
        else {
            cnt --;
            cntr ++;
            if(cnt == k - 1){
                two.pb(r[cntr - 1]);
            }
        }
    }
    if(one.size() < two.size()){
        two.pb(r[n]);
    }
    cout << one.size() << '\n';
    for(int i = 0 ; i < one.size() ; i ++){
        cout << one[i] << ' ' <<two[i] << '\n';
    }
	return 0;
}
/*
18
2 3 3
*/


#include <cstdio>
#include <cmath>
#include <map>
#include <sstream>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define ff first
#define ss second
#define clr(_x,_y) memset(_x,_y,sizeof (_x))
#define pt(y) push_back(y)
#define pu system("pause")
#define mk(x,y) make_pair(x,y)
#define pof() pop_front()
#define pob() pop_back()
#define puf(x) push_front(x)
#define ls p << 1
#define rs p << 1 | 1
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 0X3f3f3f3f;
const ll linf = 1e18;
const double pi = acos(-1.0);
const ll MAXN = 1e6 + 10;
const ll MAXM = 1e5 + 10;
const double eps = 1e-7;
const ll mod = 1000000007;

struct Line
{
    int l,r;
    bool operator < (const Line & a) const
    {
        return l < a.l;
    }
}a[MAXN],ans[MAXN];

map <int,int> mp;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0;i < n; ++ i)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
    }
    int p = 0,sum = 0;
    for (int i = 0;i < n; ++ i)
    {
        ++ mp[a[i].l];
        -- mp[a[i].r];
    }
    for (map <int,int> :: iterator it = mp.begin();it != mp.end(); ++ it)
    {
        sum += (it -> ss);
//        printf("ff = %d sum = %d ss = %d\n",it -> ff,sum,it -> ss);
        if ((it -> ss) == -1 && sum == k - 1)
        {
            ans[p].r = it -> ff;
            ++ p;
        }
        else if (sum == k && (it -> ss) == 1)
        {
            ans[p].l = it -> ff;
//            puts("asd");
        }
    }
    printf("%d\n",p);
    for (int i = 0;i < p; ++ i)
        printf("%d %d\n",ans[i].l,ans[i].r);
    return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
#include <sstream>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define ff first
#define ss second
#define clr(_x,_y) memset(_x,_y,sizeof (_x))
#define pt(y) push_back(y)
#define pu system("pause")
#define mk(x,y) make_pair(x,y)
#define pof() pop_front()
#define pob() pop_back()
#define puf(x) push_front(x)
#define ls p << 1
#define rs p << 1 | 1
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 0X3f3f3f3f;
const ll linf = 1e18;
const double pi = acos(-1.0);
const ll MAXN = 1e6 + 10;
const ll MAXM = 1e5 + 10;
const double eps = 1e-7;
const ll mod = 1000000007;

struct Line
{
    int l,r;
    bool operator < (const Line & a) const
    {
        return l < a.l;
    }
}a[MAXN],ans[MAXN];

map <int,int> mp;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0;i < n; ++ i)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
    }
    int p = 0,sum = 0;
    for (int i = 0;i < n; ++ i)
    {
        mp[a[i].l] = 1;
        mp[a[i].r] = -1;
    }
    for (map <int,int> :: iterator it = mp.begin();it != mp.end(); ++ it)
    {
        sum += (it -> ss);
//        printf("ff = %d sum = %d ss = %d\n",it -> ff,sum,it -> ss);
        if ((it -> ss) == -1 && sum == k - 1)
        {
            ans[p].r = it -> ff;
            ++ p;
        }
        else if (sum == k && (it -> ss) == 1)
        {
            ans[p].l = it -> ff;
//            puts("asd");
        }
    }
    printf("%d\n",p);
    for (int i = 0;i < p; ++ i)
        printf("%d %d\n",ans[i].l,ans[i].r);
    return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
#include <sstream>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define ff first
#define ss second
#define clr(_x,_y) memset(_x,_y,sizeof (_x))
#define pt(y) push_back(y)
#define pu system("pause")
#define mk(x,y) make_pair(x,y)
#define pof() pop_front()
#define pob() pop_back()
#define puf(x) push_front(x)
#define ls p << 1
#define rs p << 1 | 1
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 0X3f3f3f3f;
const ll linf = 1e18;
const double pi = acos(-1.0);
const ll MAXN = 1e6 + 10;
const ll MAXM = 1e5 + 10;
const double eps = 1e-7;
const ll mod = 1000000007;

struct Line
{
    int l,r;
    bool operator < (const Line & a) const
    {
        return l < a.l;
    }
}a[MAXN],ans[MAXN];

map <int,int> mp;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0;i < n; ++ i)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
    }
    int p = 0,sum = 0;
    for (int i = 0;i < n; ++ i)
    {
        ++ mp[a[i].l];
        -- mp[a[i].r];
    }
    for (map <int,int> :: iterator it = mp.begin();it != mp.end(); ++ it)
    {
        sum += (it -> ss);
//        printf("ff = %d sum = %d ss = %d\n",it -> ff,sum,it -> ss);
        if ((it -> ss) <= -1 && sum == k - 1)
        {
            ans[p].r = it -> ff;
            ++ p;
        }
        else if (sum == k && (it -> ss) >= 1)
        {
            ans[p].l = it -> ff;
//            puts("asd");
        }
    }
    printf("%d\n",p);
    for (int i = 0;i < p; ++ i)
        printf("%d %d\n",ans[i].l,ans[i].r);
    return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
#include <sstream>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define ff first
#define ss second
#define clr(_x,_y) memset(_x,_y,sizeof (_x))
#define pt(y) push_back(y)
#define pu system("pause")
#define mk(x,y) make_pair(x,y)
#define pof() pop_front()
#define pob() pop_back()
#define puf(x) push_front(x)
#define ls p << 1
#define rs p << 1 | 1
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 0X3f3f3f3f;
const ll linf = 1e18;
const double pi = acos(-1.0);
const ll MAXN = 1e6 + 10;
const ll MAXM = 1e5 + 10;
const double eps = 1e-7;
const ll mod = 1000000007;

struct Line
{
    int l,r;
}ans[MAXN];

pair <int,int> a[MAXN << 1];

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0;i < n; ++ i)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        a[i << 1].ff = l;
        a[i << 1].ss = -1;
        a[i << 1 | 1].ff = r;
        a[i << 1 | 1].ss = 1;
    }
    int p = 0,sum = 0;
    n <<= 1;
    sort(a,a + n);
    for (int i = 0;i < n; ++ i)
    {
        sum += -a[i].ss;
//        printf("ff = %d sum = %d ss = %d\n",it -> ff,sum,it -> ss);
        if (a[i].ss == 1 && sum == k - 1)
        {
            ans[p].r = a[i].ff;
            ++ p;
        }
        else if (sum == k && a[i].ss == -1)
        {
            ans[p].l = a[i].ff;
//            puts("asd");
        }
    }
    printf("%d\n",p);
    for (int i = 0;i < p; ++ i)
        printf("%d %d\n",ans[i].l,ans[i].r);
    return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
#include <sstream>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define ff first
#define ss second
#define clr(_x,_y) memset(_x,_y,sizeof (_x))
#define pt(y) push_back(y)
#define pu system("pause")
#define mk(x,y) make_pair(x,y)
#define pof() pop_front()
#define pob() pop_back()
#define puf(x) push_front(x)
#define ls p << 1
#define rs p << 1 | 1
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 0X3f3f3f3f;
const ll linf = 1e18;
const double pi = acos(-1.0);
const ll MAXN = 1e6 + 10;
const ll MAXM = 1e5 + 10;
const double eps = 1e-7;
const ll mod = 1000000007;

struct Line
{
    int l,r;
}ans[MAXN];

pair <int,int> a[MAXN << 1];

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0;i < n; ++ i)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        a[i << 1].ff = l;
        a[i << 1].ss = 1;
        a[i << 1 | 1].ff = r;
        a[i << 1 | 1].ss = -1;
    }
    int p = 0,sum = 0;
    n <<= 1;
    sort(a,a + n);
    for (int i = 0;i < n; ++ i)
    {
        sum += a[i].ss;
//        printf("ff = %d sum = %d ss = %d\n",it -> ff,sum,it -> ss);
        if (a[i].ss == -1 && sum == k - 1)
        {
            ans[p].r = a[i].ff;
            ++ p;
        }
        else if (sum == k && a[i].ss == 1)
        {
            ans[p].l = a[i].ff;
//            puts("asd");
        }
    }
    printf("%d\n",p);
    for (int i = 0;i < p; ++ i)
        printf("%d %d\n",ans[i].l,ans[i].r);
    return 0;
}


#include <cstdio>
#include <cmath>
#include <map>
#include <sstream>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#define ff first
#define ss second
#define clr(_x,_y) memset(_x,_y,sizeof (_x))
#define pt(y) push_back(y)
#define pu system("pause")
#define mk(x,y) make_pair(x,y)
#define pof() pop_front()
#define pob() pop_back()
#define puf(x) push_front(x)
#define ls p << 1
#define rs p << 1 | 1
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int inf = 0X3f3f3f3f;
const ll linf = 1e18;
const double pi = acos(-1.0);
const ll MAXN = 1e6 + 10;
const ll MAXM = 1e5 + 10;
const double eps = 1e-7;
const ll mod = 1000000007;

struct Line
{
    int l,r;
    bool operator < (const Line & a) const
    {
        return l < a.l;
    }
}a[MAXN],ans[MAXN];

map <int,int> mp;

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for (int i = 0;i < n; ++ i)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
    }
    int p = 0,sum = 0;
    for (int i = 0;i < n; ++ i)
    {
        mp[a[i].l] = 1;
        mp[a[i].r] = -1;
    }
    bool flag = 0;
    for (map <int,int> :: iterator it = mp.begin();it != mp.end(); ++ it)
    {
        sum += (it -> ss);
        if ((it -> ss) == -1 && flag)
        {
            flag = 0;
            ans[p].r = it -> ff;
            ++ p;
        }
        else if (sum == k)
        {
            flag = 1;
            ans[p].l = it -> ff;
        }
    }
    printf("%d\n",p);
    for (int i = 0;i < p; ++ i)
        printf("%d %d\n",ans[i].l,ans[i].r);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
vector<pii> arr;
vector<pii> res;
map<ll, ll> mp;
int main() {
	ll n, k;
	scanf("%lld %lld", &n, &k);
	for(ll i = 0 ; i < n ; i++){
		ll l, r;
		scanf("%lld %lld", &l, &r);
		arr.push_back({l, 0});
		arr.push_back({r, 1});
	}
	sort(arr.begin(), arr.end());
	ll icount = 0;
	ll start = 0;
	for(auto it : arr){
		ll val = it.second;
		if(val == 0){
			icount++;
			if(icount == k){
				start = it.first;
			}
		}else{
			if(icount == k){
				res.push_back({start, it.first});
			}
			icount--;
		}
	}

	cout << res.size() << endl;
	for(auto it = res.begin() ; it != res.end() ; it++){
		cout << it->first << " " << it->second << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
int main() {
	ll n, k;
	scanf("%lld %lld", &n, &k);
	//cin >> n >> k;
	ll o = 1;
	ll c = -1;
	for(ll i = 0 ; i < n ; i++){
		ll l, r;
		scanf("%lld %lld", &l, &r);
		//cin >> l >> r;
		mp[l]++;
		mp[r]--;
	}
	vector<pair<ll, ll>> res;
	int sum = 0;
	for(auto it = mp.begin() ; it != mp.end() ; it++){
		int v = it->second;
		sum += v;
		if(sum >= k){
			ll first = it->first;
			while(it != mp.end() && sum >= k){
				it++;
				sum += it->second;
			}
			ll second = it->first;
			res.push_back({first, second});
		}
	}
	cout << res.size() << endl;
	for(auto it = res.begin() ; it != res.end() ; it++){
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll, ll> mp;
int main() {
	ll n, k;
	cin >> n >> k;
	ll o = 1;
	ll c = -1;
	for(ll i = 0 ; i < n ; i++){
		ll l, r;
		cin >> l >> r;
		mp[l]++;
		mp[r]--;
	}
	vector<pair<ll, ll>> res;
	int sum = 0;
	for(auto it = mp.begin() ; it != mp.end() ; it++){
		int v = it->second;
		sum += v;
		if(sum >= k){
			ll first = it->first;
			while(it != mp.end() && sum >= k){
				it++;
				sum += it->second;
			}
			ll second = it->first;
			res.push_back({first, second});
		}
	}
	cout << res.size() << endl;
	for(auto it = res.begin() ; it != res.end() ; it++){
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll offset = 100000000;
ll size = 2*offset + 1;
vector<ll>arr(size);
int main() {
	ll n, k;
	cin >> n >> k;
	//cout << "here" << endl;
	for(int i = 0 ; i < n ; i++){
		int l, r;
		cin >> l >> r;
		//cout << (l + offset) << " " << (r + 1 + offset) << endl;
		arr[l + offset]++;
		arr[r + offset]--;
	}
	// for(int i = -4 + offset ; i < 9 + offset ; i++){
	// 	cout << arr[i] << " ";
	// }
	// cout << endl;
	for(int i = 1 ; i < 2 * offset + 1 ; i++) arr[i] += arr[i - 1];
	// for(int i = -4 + offset ; i < 9 + offset ; i++){
	// 	cout << arr[i] << " ";
	// }
	// cout << endl;
	vector<pair<ll, ll>> res;
	for(int i = 0 ; i < 2 * offset + 1 ; i++){
		if(arr[i] >= k){
			ll start = (i - offset);
			//cout << (i - offset) << " ";
			while(i < size && arr[i] >= k) i++;
			//cout << (i - offset) << endl;
			ll end = (i - offset);
			res.push_back({start, end});
		}
	}
	cout << res.size() << endl;
	for(auto it = res.begin() ; it != res.end() ; it++){
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll offset = 100000000;
ll a_size = 2*offset + 1;
vector<ll>arr(a_size);
int main() {
	ll n, k;
	cin >> n >> k;
	for(ll i = 0 ; i < n ; i++){
		ll l, r;
		cin >> l >> r;
		arr[l + offset]++;
		arr[r + offset]--;
	}

	for(ll i = 1 ; i < 2 * offset + 1 ; i++) arr[i] += arr[i - 1];

	vector<pair<ll, ll>> res;
	for(ll i = 0 ; i < 2 * offset + 1 ; i++){
		if(arr[i] >= k){
			ll start = (i - offset);
			while(i < a_size && arr[i] >= k) i++;
			ll end = (i - offset);
			res.push_back({start, end});
		}
	}

	cout << res.size() << endl;
	for(auto it = res.begin() ; it != res.end() ; it++){
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll offset = 100000000;
ll a_size = 2*offset + 1;
vector<ll>arr(a_size);
int main() {
	ll n, k;
	cin >> n >> k;
	for(ll i = 0 ; i < n ; i++){
		int l, r;
		cin >> l >> r;
		arr[l + offset]++;
		arr[r + offset]--;
	}

	for(ll i = 1 ; i < 2 * offset + 1 ; i++) arr[i] += arr[i - 1];

	vector<pair<ll, ll>> res;
	for(int i = 0 ; i < 2 * offset + 1 ; i++){
		if(arr[i] >= k){
			ll start = (i - offset);
			while(i < a_size && arr[i] >= k) i++;
			ll end = (i - offset);
			res.push_back({start, end});
		}
	}

	cout << res.size() << endl;
	for(auto it = res.begin() ; it != res.end() ; it++){
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

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

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        v.pb(mp(x,-1));
        v.pb(mp(y,1));
    }
    sort(v.begin(),v.end());
    int cnt = 0;
    int l,r;
    for(int i=0;i<v.size();i++)
    {
        // cout<<v[i].first<<" "<<v[i].second<<endl;
        int prev = cnt;
        cnt -= v[i].second;
        // cout<<cnt<<endl;
        if(cnt==k&&prev==k-1)
        {
            l = v[i].first;
        }
        if(cnt==k-1&&prev==k)
        {
            r = v[i].first;
            ans.pb(mp(l,r));
        }
    }
    pfi(ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        if(st.find(mp(x,-y))==st.end())
            st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    int p = 0;
    while(!st.empty())
    {
        // cout<<endl;
        p++;
        if(p==10)
            break;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;
        st.erase(p);
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<q.first)
            continue;
        if((-p.second)==q.first)
        {
            // cout<<"here"<<endl;
            m[mp(q.first,-q.first)]++;
            st.insert(mp(q.first,-q.first));
            continue;
        }
        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(q.first,p.second));
        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(-q.second,p.second));
            st.erase(q);
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

unordered_map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        if(x==y)
        {
            m[mp(x,-x)]++;
            continue;
        }
        else
        {
            m[mp(x,-x)]++;
            m[mp(y,-y)]++;
        }
        st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    while(!st.empty())
    {
        // cout<<m[mp(3,-3)]<<endl;
        // cout<<endl;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;
        st.erase(p);
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<=q.first)
            continue;
        // if((-p.second)==q.first)
        // {
        //     // cout<<"here"<<endl;
        //     m[mp(q.first,-q.first)]++;
        //     st.insert(mp(q.first,-q.first));
        //     continue;
        // }
        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,-q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(q.first,p.second));

        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(-q.second,p.second));
            // st.insert(mp(p.first,q.second));
            // st.insert(mp(p.first,q.second));
            // cout<<"fff= "<<p.first<<" "<<q.second<<endl;
            // if(p.first==abs(q.second))
            //     st.erase(q);
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        if(x==y)
        {
            m[mp(x,-x)]++;
            continue;
        }
        else
        {
            m[mp(x,-x)]++;
            m[mp(y,-y)]++;
        }
        st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    while(!st.empty())
    {
        // cout<<m[mp(3,-3)]<<endl;
        // cout<<endl;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;
        st.erase(p);
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<=q.first)
            continue;
        // if((-p.second)==q.first)
        // {
        //     // cout<<"here"<<endl;
        //     m[mp(q.first,-q.first)]++;
        //     st.insert(mp(q.first,-q.first));
        //     continue;
        // }
        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(q.first,p.second));
        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(-q.second,p.second));
            // st.insert(mp(p.first,q.second));
            // cout<<"fff= "<<p.first<<" "<<q.second<<endl;
            // if(p.first==abs(q.second))
            //     st.erase(q);
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        if(st.find(mp(x,-y))==st.end())
            st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    while(!st.empty())
    {
        // cout<<endl;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;
        st.erase(p);
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<q.first)
            continue;
        if((-p.second)==q.first)
        {
            // cout<<"here"<<endl;
            m[mp(q.first,-q.first)]++;
            st.insert(mp(q.first,-q.first));
            continue;
        }
        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(q.first,p.second));
        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(-q.second,p.second));
            st.erase(q);
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    while(!st.empty())
    {
        // cout<<m[mp(3,-3)]<<endl;
        // cout<<endl;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;
        st.erase(p);
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<q.first)
            continue;
        if((-p.second)==q.first)
        {
            // cout<<"here"<<endl;
            m[mp(q.first,-q.first)]++;
            st.insert(mp(q.first,-q.first));
            continue;
        }
        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(q.first,p.second));
        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(-q.second,p.second));
            // st.insert(mp(p.first,q.second));
            // cout<<"fff= "<<p.first<<" "<<q.second<<endl;
            if(p.first==abs(q.second))
                st.erase(q);
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        if(st.find(mp(x,-y))==st.end())
            st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    // int p = 0;
    while(!st.empty())
    {
        // cout<<endl;
        // p++;
        // if(p==10)
        //     break;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;

        st.erase(st.begin());
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<=q.first)
            continue;

        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(p.first,q.first));
            st.insert(mp(q.first,p.second));
        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(p.first,q.second));
            st.insert(mp(-q.second,p.second));
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define here cout<<"here\n"
#define pb push_back
#define mp make_pair
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define pf printf
#define pfi(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
using namespace std;

const ll mod = 1e9+7;

const int MAX = 100005;

ll modexp(ll x, ll n)
{
    if(n==0)
        return 1LL;
    if(n%2==0)
    {
        ll y = modexp(x,n/2)%mod;
        return (y*y)%mod;
    }
    return (x*modexp(x,n-1)%mod)%mod;
}

ll powr(ll x, ll n)
{
    ll ans = 1;
    for(int i=1;i<=n;i++)
        ans=ans*x;
    return ans;
}

set<pair<int,int> > st;

map<pair<int,int>,int> m;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    int n,k;
    si(n),si(k);
    for(int i=0;i<n;i++)
    {
        int x,y;
        si(x),si(y);
        if(x==y)
        {
            m[mp(x,-x)]++;
            continue;
        }
        else
        {
            m[mp(x,-x)]++;
            m[mp(y,-y)]++;
        }
        st.insert(mp(x,-y));
        m[mp(x,-y)]++;
    }
    while(!st.empty())
    {
        // cout<<m[mp(3,-3)]<<endl;
        // cout<<endl;
        pair<int,int> p = *st.begin();

        // cout<<"up = "<<p.first<<" "<<p.second<<endl;
        st.erase(p);
        if(st.empty()==true)
            break;
        pair<int,int> q = *st.begin();

        if((-p.second)<=q.first)
            continue;
        // if((-p.second)==q.first)
        // {
        //     // cout<<"here"<<endl;
        //     m[mp(q.first,-q.first)]++;
        //     st.insert(mp(q.first,-q.first));
        //     continue;
        // }
        // cout<<"down = "<<q.first<<" "<<q.second<<endl;

        int cnt = m[p];
        m.erase(p);

        if(p.first<q.first)
        {
            m[mp(p.first,-q.first)] += cnt;
            m[mp(q.first,p.second)] += cnt;
            st.insert(mp(q.first,p.second));

        }
        else
        {
            m[mp(p.first,q.second)] += cnt;
            m[mp(-q.second,p.second)] += cnt;
            st.insert(mp(-q.second,p.second));
            // st.insert(mp(p.first,q.second));
            // st.insert(mp(p.first,q.second));
            // cout<<"fff= "<<p.first<<" "<<q.second<<endl;
            // if(p.first==abs(q.second))
            //     st.erase(q);
        }
        // cout<<"size = "<<st.size()<<endl;
    }
    vector<pair<int,int> > v;
    for(map<pair<int,int>,int>::iterator it=m.begin();it!=m.end();it++)
    {
        if(it->second>=k)
        {
            pair<int,int> p =it->first;
            v.pb(mp(p.first,(-p.second)));
        }
    }
    int x = v.size();
    if(x==0)
    {
        pfi(x);
        return 0;
    }
    if(x==1)
    {
        pfi(x);
        pf("%d %d\n",v[0].first,v[0].second);
        return 0;
    }
    int curr_s = v[0].first;
    int curr_e = v[0].second;
    vector<pair<int,int> > ans;
    for(int i=1;i<x;i++)
    {
        if(i==x-1)
        {
            if(curr_e<v[i].first)
            {
                ans.pb(mp(curr_s,curr_e));
                ans.pb(v[i]);
            }
            else
            {
                ans.pb(mp(curr_s,max(curr_e,v[i].second)));
            }
            break;
        }
        if(curr_e<v[i].first)
        {
            ans.pb(mp(curr_s,curr_e));
            curr_s = v[i].first;
            curr_e = v[i].second;
        }
        else
        {
            curr_e = max(curr_e,v[i].second);
        }
    }
    pf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++)
        pf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define ipair pair<int,int>
#define eps 1e-8
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#define ipair pair<int,int>
using namespace std;

bool cmp(ipair a,ipair b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,1));
        a.push_back(make_pair(y,-1));
    }
    sort(a.begin(),a.end(),cmp);
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt+=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt+=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#define ipair pair<int,int>
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1||i==2*n)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct seg{
    int i,j;
};

bool cmp(seg a,seg b){
    if(a.i==b.i)
        return a.j>b.j;
    return a.i<b.i;
}

struct cmp1{
    bool operator()(int a,int b){
        return a>b;
    }
};

void comseg(seg *a,int n,int k){
    priority_queue<int,vector<int>,cmp1 >en;
    vector<seg> ans,ans1;
    seg b;
    for(int i=0;i<k-1;i++)
        en.push(a[i].j);
    for(int i=k-1;i<n;i++){
        en.push(a[i].j);
        b.j=en.top();
        en.pop();
        b.i=a[i].i;
        if(b.j>=b.i)
            ans.push_back(b);
    }
    int z=ans.size(),e,s;
    if(ans.size()==0){
        cout<<"0\n";
        return;
    }
    e=ans[0].j;
    s=ans[0].i;/*
    cout<<"ans\n";
    for(int i=0;i<z;i++)
        cout<<ans[i].i<<" "<<ans[i].j<<endl;*/
    for(int i=1;i<z;i++){
        if(e<ans[i].i){
            b.i=s;
            b.j=e;
            ans1.push_back(b);
            s=ans[i].i;
            e=ans[i].j;
        }
        else{
            if(ans[i].j>e)
                e=ans[i].j;
        }
    }
    b.i=s;
    b.j=e;
    ans1.push_back(b);
    z=ans1.size();
    cout<<ans1.size()<<endl;
    for(int i=0;i<z;i++)
        cout<<ans1[i].i<<" "<<ans1[i].j<<endl;
}

int main() {
    int n,k;
    cin>>n>>k;
    seg a[n];
    for(int i=0;i<n;i++)
        cin>>a[i].i>>a[i].j;
    sort(a,a+n,cmp);
    comseg(a,n,k);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define eps 1e-8
#define bs 1000000007
#define bsize 512
using namespace std;

int main(){
    int n,k,x,y;
    scanf("%d %d",&n,&k);
    vector<pair<int,int> > a;
    for(int i=0;i<n;i++){
        scanf("%d %d",&x,&y);
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<pair<int,int> > ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    printf("%d\n",z);
    for(int i=0;i<z;i++)
        printf("%d %d",ans[i].first,ans[i].second);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-8
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512

const int N = 210000;

using namespace std;

int n, k;
vector<pair<int, int> > events;
vector<pair<int, int> > ans;
int cnt;
int op;

bool cmp(pair<int,int> a,pair<int,int> b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		int a, b;
		scanf("%d", &a);
		scanf("%d", &b);
		events.push_back(make_pair(a, -1));
		events.push_back(make_pair(b, 1));
	}
	sort(events.begin(), events.end());
	for (int i = 0; i < events.size(); i++)
	{
		cnt -= events[i].second;
		if (cnt == k && events[i].second == -1)//open
		{
			op = events[i].first;
		}
		if (cnt == k-1&&events[i].second == 1)//close
		{
			ans.push_back(make_pair(op, events[i].first));
		}
	}
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
	{
		printf("%d %d\n", ans[i].first, ans[i].second);
	}
	return 0;
}


#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct seg{
    int i,j;
};

bool cmp(seg a,seg b){
    if(a.i==b.i)
        return a.j>b.j;
    return a.i<b.i;
}

void comseg(seg *a,int n,int k){
    priority_queue<int,vector<int>,greater<int> >en;
    vector<seg> ans;
    seg b;
    for(int i=0;i<k;i++)
        en.push(a[i].j);
    int s=a[k-1].i,e=en.top();
    en.pop();
    for(int i=k;i<n;i++){
        en.push(a[i].j);
        if(e<s){
            e=en.top();
            s=a[i].i;
        }
        else if(a[i].i>e){
            b.i=s;
            b.j=e;
            ans.push_back(b);
            e=en.top();
            s=a[i].i;
        }
        else if(en.top()>e){
            e=en.top();
        }
        en.pop();
    }
    b.i=s;
    b.j=e;
    if(e>=s)
        ans.push_back(b);
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].i<<" "<<ans[i].j<<endl;
}

int main() {
    int n,k;
    cin>>n>>k;
    seg a[n];
    for(int i=0;i<n;i++)
        cin>>a[i].i>>a[i].j;
    sort(a,a+n,cmp);
    comseg(a,n,k);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-8
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512

const int N = 210000;

using namespace std;

int n, k;
vector<pair<int, int> > events;
vector<pair<int, int> > ans;
int cnt;
int op;

bool cmp(pair<int,int> a,pair<int,int> b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		int a, b;
		scanf("%d", &a);
		scanf("%d", &b);
		events.push_back(make_pair(a, -1));
		events.push_back(make_pair(b, 1));
	}
	sort(events.begin(), events.end(),cmp);
	for (int i = 0; i < events.size(); i++)
	{
		if (cnt == k - 1 && events[i].second == -1)//open
		{
			op = events[i].first;
		}
		if (cnt == k&&events[i].second == 1)//close
		{
			ans.push_back(make_pair(op, events[i].first));
		}
		cnt -= events[i].second;
	}
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
	{
		printf("%d %d\n", ans[i].first, ans[i].second);
	}
	return 0;
}


#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#define ipair pair<int,int>
using namespace std;

bool cmp(ipair a,ipair b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,1));
        a.push_back(make_pair(y,-1));
    }
    sort(a.begin(),a.end(),cmp);
    vector<ipair> ans;
    int cnt=0;
    for(int i=0;i<2*n;i++){
        cnt+=a[i].second;
        if(cnt==k){
            int temp=a[i].first;
            i++;
            while(i<2*n){
                cnt+=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define eps 1e-8
#define bs 1000000007
#define bsize 512
using namespace std;

int main(){
    int n,k,x,y;
    scanf("%d %d",&n,&k);
    vector<pair<int,int> > a;
    for(int i=0;i<n;i++){
        scanf("%d %d",&x,&y);
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<pair<int,int> > ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    printf("%d\n",z);
    for(int i=0;i<z;i++)
        printf("%d %d\n",ans[i].first,ans[i].second);
    return 0;
}


#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct seg{
    int i,j;
};

bool cmp(seg a,seg b){
    if(a.i==b.i)
        return a.j>b.j;
    return a.i<b.i;
}

struct cmp1{
    bool operator()(int a,int b){
        return a>b;
    }
};

void comseg(seg *a,int n,int k){
    priority_queue<int,vector<int>,cmp1 >en;
    vector<seg> ans,ans1;
    seg b;
    for(int i=0;i<k-1;i++)
        en.push(a[i].j);
    for(int i=k-1;i<n;i++){
        en.push(a[i].j);
        b.j=en.top();
        en.pop();
        b.i=a[i].i;
        if(b.j>=b.i)
            ans.push_back(b);
    }
    int z=ans.size(),e,s;
    if(ans.size()==0)
        return;
    e=ans[0].j;
    s=ans[0].i;/*
    cout<<"ans\n";
    for(int i=0;i<z;i++)
        cout<<ans[i].i<<" "<<ans[i].j<<endl;*/
    for(int i=1;i<z;i++){
        if(e<ans[i].i){
            b.i=s;
            b.j=e;
            ans1.push_back(b);
            s=ans[i].i;
            e=ans[i].j;
        }
        else{
            if(ans[i].j>e)
                e=ans[i].j;
        }
    }
    b.i=s;
    b.j=e;
    ans1.push_back(b);
    z=ans1.size();
    cout<<ans1.size()<<endl;
    for(int i=0;i<z;i++)
        cout<<ans1[i].i<<" "<<ans1[i].j<<endl;
}

int main() {
    int n,k;
    cin>>n>>k;
    seg a[n];
    for(int i=0;i<n;i++)
        cin>>a[i].i>>a[i].j;
    sort(a,a+n,cmp);
    comseg(a,n,k);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>
#define ipair pair<int,int>
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define ipair pair<int,int>
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-8
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512

const int N = 210000;

using namespace std;

int n, k;
vector<pair<int, int> > events;
vector<pair<int, int> > ans;
int cnt;
int op;

bool cmp(ipair a,ipair b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		int a, b;
		scanf("%d", &a);
		scanf("%d", &b);
		events.push_back(make_pair(a, -1));
		events.push_back(make_pair(b, 1));
	}
	sort(events.begin(), events.end(),cmp);
	for (int i = 0; i < events.size(); i++)
	{
		if (cnt == k - 1 && events[i].second == -1)//open
		{
			op = events[i].first;
		}
		if (cnt == k&&events[i].second == 1)//close
		{
			ans.push_back(make_pair(op, events[i].first));
		}
		cnt -= events[i].second;
	}
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
	{
		printf("%d %d\n", ans[i].first, ans[i].second);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define eps 1e-8
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<pair<int,int> > a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<pair<int,int> > ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#define ipair pair<int,int>
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#define ipair pair<int,int>
using namespace std;

bool cmp(ipair a,ipair b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
    int n,k,x,y;
    cin>>n>>k;
    ipair a[2*n];
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a[2*i]=make_pair(x,1);
        a[2*i+1]=make_pair(y,-1);
    }
    sort(a,a+2*n,cmp);
    vector<ipair> ans;
    int cnt=0,end[2*n];
    for(int i=0;i<2*n;i++){
        cnt+=a[i].second;
        end[i]=cnt;
        if(cnt==k){
            int temp=a[i].first;
            i++;
            while(i<2*n){
                cnt+=a[i].second;
                end[i]=cnt;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct seg{
    int i,j;
};

bool cmp(seg a,seg b){
    if(a.i==b.i)
        return a.j>b.j;
    return a.i<b.i;
}

void comseg(vector<seg> a,int n,int k){
    priority_queue<int,vector<int>,greater<int> >en;
    vector<seg> ans;
    seg b;
    for(int i=0;i<k;i++)
        en.push(a[i].j);
    int s=a[k-1].i,e=en.top();
    en.pop();
    for(int i=k;i<n;i++){
        en.push(a[i].j);
        if(e<s){
            e=en.top();
            s=a[i].i;
        }
        else if(a[i].i>e){
            b.i=s;
            b.j=e;
            ans.push_back(b);
            e=en.top();
            s=a[i].i;
        }
        else if(en.top()>e){
            e=en.top();
        }
        en.pop();
    }
    b.i=s;
    b.j=e;
    if(e>=s)
        ans.push_back(b);
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].i<<" "<<ans[i].j<<endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    int n,k,x,y;
    cin>>n>>k;
    vector<seg> a;
    seg b;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        b.i=x,b.j=y;
        a.push_back(b);
    }
    sort(a.begin(),a.end(),cmp);
    comseg(a,n,k);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-8
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#define ipair pair<int,int>
using namespace std;

bool cmp(ipair a,ipair b){
    if(a.first==b.first)
        return a.second>b.second;
    return a.first<b.first;
}

int main(){
    int n,k,x,y;
    cin>>n>>k;
    ipair a[2*n];
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a[2*i]=make_pair(x,1);
        a[2*i+1]=make_pair(y,-1);
    }
    sort(a,a+2*n,cmp);
    vector<ipair> ans;
    int cnt=0;
    for(int i=0;i<2*n;i++){
        cnt+=a[i].second;
        if(cnt==k){
            int temp=a[i].first;
            i++;
            while(i<2*n){
                cnt+=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define ipair pair<int,int>
using namespace std;

int main(){
    int n,k,x,y;
    cin>>n>>k;
    vector<ipair> a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<ipair> ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}


#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct seg{
    int i,j;
};

bool cmp(seg a,seg b){
    if(a.i==b.i)
        return a.j>b.j;
    return a.i<b.i;
}

void comseg(seg *a,int n,int k){
    priority_queue<int,vector<int>,greater<int> >en;
    vector<seg> ans;
    seg b;
    for(int i=0;i<k;i++)
        en.push(a[i].j);
    int s=a[k-1].i,e=en.top();
    en.pop();
    for(int i=k;i<n;i++){
        en.push(a[i].j);
        if(e<s){
            e=en.top();
            s=a[i].i;
        }
        else if(a[i].i>e){
            b.i=s;
            b.j=e;
            ans.push_back(b);
            e=en.top();
            s=a[i].i;
        }
        else if(en.top()>e){
            e=en.top();
        }
        en.pop();
    }
    b.i=s;
    b.j=e;
    if(e>=s)
        ans.push_back(b);
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].i<<" "<<ans[i].j<<endl;
}

int main() {
    int n,k;
    cin>>n>>k;
    seg a[n];
    for(int i=0;i<n;i++)
        cin>>a[i].i>>a[i].j;
    stable_sort(a,a+n,cmp);
    comseg(a,n,k);
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define eps 1e-8
#define bs 1000000007
#define bsize 512
using namespace std;

int main(){
    std::ios::sync_with_stdio(false);
    int n,k,x,y;
    cin>>n>>k;;
    vector<pair<int,int> > a;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        a.push_back(make_pair(x,-1));
        a.push_back(make_pair(y,1));
    }
    sort(a.begin(),a.end());
    vector<pair<int,int> > ans;
    int cnt=0,temp;
    for(int i=0;i<2*n;i++){
        cnt-=a[i].second;
        if(cnt==k){
            temp=a[i].first;
            i++;
            while(1){
                cnt-=a[i].second;
                if(cnt==k-1)
                    break;
                i++;
            }
            ans.push_back(make_pair(temp,a[i].first));
        }
    }
    int z=ans.size();
    cout<<z<<'\n';
    for(int i=0;i<z;i++)
        cout<<ans[i].first<<" "<<ans[i].second<<"\n";
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <ctime>
#include <assert.h>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash

#define eps 1e-8
//#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 512

const int N = 210000;

using namespace std;

int n, k;
vector<pair<int, int> > events;
vector<pair<int, int> > ans;
int cnt;
int op;
int main(){
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		int a, b;
		scanf("%d", &a);
		scanf("%d", &b);
		events.push_back(make_pair(a, -1));
		events.push_back(make_pair(b, 1));
	}
	sort(events.begin(), events.end());
	for (int i = 0; i < events.size(); i++)
	{
		if (cnt == k - 1 && events[i].second == -1)//open
		{
			op = events[i].first;
		}
		if (cnt == k&&events[i].second == 1)//close
		{
			ans.push_back(make_pair(op, events[i].first));
		}
		cnt -= events[i].second;
	}
	cout << ans.size() << endl;
	for (int i = 0; i < ans.size(); i++)
	{
		printf("%d %d\n", ans[i].first, ans[i].second);
	}
	return 0;
}


#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

struct seg{
    int i,j;
};

bool cmp(seg a,seg b){
    if(a.i==b.i)
        return a.j>b.j;
    return a.i<b.i;
}

void comseg(vector<seg> a,int n,int k){
    priority_queue<int,vector<int>,greater<int> >en;
    vector<seg> ans;
    seg b;
    for(int i=0;i<k;i++)
        en.push(a[i].j);
    int s=a[k-1].i,e=en.top();
    en.pop();
    for(int i=k;i<n;i++){
        en.push(a[i].j);
        if(e<s){
            e=en.top();
            s=a[i].i;
        }
        else if(a[i].i>e){
            b.i=s;
            b.j=e;
            ans.push_back(b);
            e=en.top();
            s=a[i].i;
        }
        else if(en.top()>e){
            e=en.top();
        }
        en.pop();
    }
    b.i=s;
    b.j=e;
    if(e>=s)
        ans.push_back(b);
    int z=ans.size();
    cout<<z<<endl;
    for(int i=0;i<z;i++)
        cout<<ans[i].i<<" "<<ans[i].j<<endl;
}

int main() {
    int n,k,x,y;
    cin>>n>>k;
    vector<seg> a;
    seg b;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        b.i=x,b.j=y;
        a.push_back(b);
    }
    sort(a.begin(),a.end(),cmp);
    comseg(a,n,k);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 1e15
#define MOD 1000000007
#define EN '\n'
#define vc vector
#define pr pair <ll,ll>
#define m(s ,e) ((s) + (e)) / 2;
using namespace std;
vc <pr> v;
set <ll> val;
map <ll, ll> mp;
ll pre[4000005];
map <ll ,ll> back;
int main(){
    fastio;
    ll n , k;
    cin >> n >> k;
    ll l , r;
    for (ll i = 0 ; i < n ; i++){
        cin >> l >> r;
        v.push_back({l,r});
        val.insert(l);
        val.insert(r);
    }
    ll cnt = 0;
    for (auto it : val){
        mp[it] = cnt;
        back[cnt] = it;
        cnt+=2;
    }
    for (auto p : v){
        pre[mp[p.first]]++;
        pre[mp[p.second]+1]--;
    }
    vc <ll> va;
    if(pre[0] >= k)
        va.push_back(0);
    for (ll i = 1 ; i <= cnt ; i++){
        pre[i] += pre[i-1];
        if (pre[i] >= k && pre[i] > 0)
            va.push_back(i);
    }
    vc <pr> ans;
    for (ll i = 0 ; i < va.size() ; i++){
        ll l = va[i];
        while (va[i] + 1 == va[i+1] && i < va.size() - 1){
            cout << i << " " << va[i] << EN;
            i++;
        }
        ll r = va[i];
        ans.push_back({back[l] , back[r]});
    }
    cout << ans.size() << EN;
    for (auto x : ans)
        cout << x.first << " " << x.second << EN;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 1e15
#define MOD 1000000007
#define EN '\n'
#define vc vector
#define pr pair <ll,ll>
#define m(s ,e) ((s) + (e)) / 2;
using namespace std;
vc <pr> v;
set <ll> val;
map <ll, ll> mp;
ll pre[4000005];
map <ll ,ll> back;
int main(){
    fastio;
    ll n , k;
    cin >> n >> k;
    ll l , r;
    for (ll i = 0 ; i < n ; i++){
        cin >> l >> r;
        v.push_back({l,r});
        val.insert(l);
        val.insert(r);
    }
    ll cnt = 0;
    for (auto it : val){
        mp[it] = cnt;
        back[cnt] = it;
        cnt+=2;
    }
    for (auto p : v){
        pre[mp[p.first]]++;
        pre[mp[p.second]+1]--;
    }
    vc <ll> va;
    if(pre[0] >= k)
        va.push_back(0);
    for (ll i = 1 ; i <= cnt ; i++){
        pre[i] += pre[i-1];
        if (pre[i] >= k && pre[i] > 0)
            va.push_back(i);
    }
    vc <pr> ans;
    for (ll i = 0 ; i < va.size() ; i++){
        ll l = va[i];
        while (va[i] + 1 == va[i+1] && i < va.size() - 1){
            i++;
        }
        ll r = va[i];
        ans.push_back({back[l] , back[r]});
    }
    cout << ans.size() << EN;
    for (auto x : ans)
        cout << x.first << " " << x.second << EN;
    return 0;
}


#include <bits/stdc++.h>
#define ll int
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 1e15
#define MOD 1000000007
#define EN '\n'
#define vc vector
#define pr pair <ll,ll>
#define m(s ,e) ((s) + (e)) / 2;
using namespace std;
vc <pr> v;
set <ll> val;
map <ll, ll> mp;
ll pre[4000005];
map <ll ,ll> back;
int main(){
    fastio;
    ll n , k;
    cin >> n >> k;
    ll l , r;
    for (ll i = 0 ; i < n ; i++){
        cin >> l >> r;
        v.push_back({l,r});
        val.insert(l);
        val.insert(r);
    }
    ll cnt = 0;
    for (auto it : val){
        mp[it] = cnt;
        back[cnt] = it;
        cnt+=2;
    }
    for (auto p : v){
        pre[mp[p.first]]++;
        pre[mp[p.second]+1]--;
    }
    vc <ll> va;
    if(pre[0] >= k)
        va.push_back(0);
    for (ll i = 1 ; i <= cnt ; i++){
        pre[i] += pre[i-1];
        if (pre[i] >= k && pre[i] > 0)
            va.push_back(i);
    }
    vc <pr> ans;
    for (ll i = 0 ; i < va.size() ; i++){
        ll l = va[i];
        while (va[i] + 1 == va[i+1] && i < va.size() - 1){
            i++;
        }
        ll r = va[i];
        ans.push_back({back[l] , back[r]});
    }
    cout << ans.size() << EN;
    for (auto x : ans)
        cout << x.first << " " << x.second << EN;
    return 0;
}


#include <bits/stdc++.h>
#define ll int
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 1e15
#define MOD 1000000007
#define EN '\n'
#define vc vector
#define pr pair <ll,ll>
#define m(s ,e) ((s) + (e)) / 2;
using namespace std;
set <ll> val;
map <ll, ll> mp;
ll pre[4000005];
ll back[4000005];
ll va[4000002];
vc <pr> ans;
pr v[1000002];
int main(){
    fastio;
    ll n , k;
    scanf("%d%d", &n , &k);
    ll l , r;
    for (ll i = 0 ; i < n ; i++){
       scanf("%d%d", &l , &r);
       v[i] = {l ,r};
        val.insert(l);
        val.insert(r);
    }
    ll cnt = 0;
    ll ind = 0;
    for (auto it : val){
        mp[it] = cnt;
        back[cnt] = it;
        cnt+=2;
    }
    for (ll i = 0 ; i < n ; i++){
        pre[mp[v[i].first]]++;
        pre[mp[v[i].second]+1]--;
    }
    if(pre[0] >= k)
        va[ind++] = 0;
    for (ll i = 1; i <= cnt ; i++){
        pre[i] += pre[i-1];
        if (pre[i] >= k && pre[i] > 0)
            va[ind++] = i;
    }
    for (ll i = 0 ; i < ind ; i++){
        ll l = va[i];
        while (va[i] + 1 == va[i+1] && i < ind - 1){
            i++;
        }
        ll r = va[i];
        ans.push_back({back[l] , back[r]});
    }
    printf("%d\n", ans.size());
    for (auto x : ans)
        printf("%d %d\n", x.first , x.second);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 1e15
#define MOD 1000000007
#define EN '\n'
#define vc vector
#define pr pair <ll,ll>
#define m(s ,e) ((s) + (e)) / 2;
using namespace std;
vc <pr> v;
ll itr(pr x ,pr y){
    ll x1 = x.first;
    ll y1 = x.second;
    ll x2 = y.first;
    ll y2 = y.second;
    if (y1 < x2 || y2 < x1)
        return 0;
    if (x1 <= x2 && x2 <= y1 && y2 >= x1 && y2 <= y1)
        return 2;
    return 1;
}
set <ll> val;
map <ll, ll> mp;
ll pre[2000003];
map <ll ,ll> back;
int main(){
    fastio;
    ll n , k;
    cin >> n >> k;
    ll l , r;
    for (ll i = 0 ; i < n ; i++){
        cin >> l >> r;
        v.push_back({l,r});
        val.insert(l);
        val.insert(r);
    }
    ll cnt = 0;
    for (auto it : val){
        mp[it] = cnt;
        back[cnt] = it;
        cnt+=2;
    }
    for (auto p : v){
        pre[mp[p.first]]++;
        pre[mp[p.second]+1]--;
    }
    vc <ll> va;
    if(pre[0] >= k)
        va.push_back(back[0]);
    for (ll i = 1 ; i <= mp[*(prev(val.end() , 1))] ; i++){
        pre[i] += pre[i-1];
        if (pre[i] >= k)
            va.push_back(i);
    }
    vc <pr> ans;
    for (ll i = 0 ; i < va.size() ; i++){
        ll l = va[i];
        while (va[i] + 1 == va[i+1] && i < va.size() - 1){
            i++;
        }
        ll r = va[i];
        ans.push_back({back[l] , back[r]});
    }
    cout << ans.size() << EN;
    for (auto x : ans)
        cout << x.first << " " << x.second << EN;
    return 0;
}


#include <bits/stdc++.h>
#define ll int
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 1e15
#define MOD 1000000007
#define EN '\n'
#define vc vector
#define pr pair <ll,ll>
#define m(s ,e) ((s) + (e)) / 2;
using namespace std;
set <ll> val;
map <ll, ll> mp;
ll pre[4000005];
ll back[4000005];
ll va[4000002];
vc <pr> ans;
pr v[1000002];
int main(){
    fastio;
    ll n , k;
    scanf("%d%d", &n , &k);
    ll l , r;
    for (ll i = 0 ; i < n ; i++){
       scanf("%d%d", &l , &r);
       v[i] = {l ,r};
        val.insert(l);
        val.insert(r);
    }
    ll cnt = 0;
    ll ind = 0;
    for (auto it : val){
        mp[it] = cnt;
        back[cnt] = it;
        cnt+=2;
    }
    for (auto p : v){
        pre[mp[p.first]]++;
        pre[mp[p.second]+1]--;
    }
    if(pre[0] >= k)
        va[ind++] = 0;
    for (ll i = 1; i <= cnt ; i++){
        pre[i] += pre[i-1];
        if (pre[i] >= k && pre[i] > 0)
            va[ind++] = i;
    }
    for (ll i = 0 ; i < ind ; i++){
        ll l = va[i];
        while (va[i] + 1 == va[i+1] && i < ind - 1){
            i++;
        }
        ll r = va[i];
        ans.push_back({back[l] , back[r]});
    }
    printf("%d\n", ans.size());
    for (auto x : ans)
        printf("%d %d\n", x.first , x.second);
    return 0;
}


/**
 * @Author: harshkhajuria
 * @Date:   01-Feb-2019 10:37:03 pm
 * @Email:  khajuriaharsh729@gmail.com
 * @Filename: The Union of k-Segments.cpp
 * @Last modified by:   harshkhajuria
 * @Last modified time: 03-Jun-2019 02:28:46 pm
 */

#include <bits/stdc++.h>
#define ll long long
#define mp(a, b) make_pair(a, b)
#define newline cout << endl;
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
using namespace std;

template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << '(' << p.ff << ", " << p.ss << ')';
  return out;
}

template <typename T> ostream &operator<<(ostream &out, vector<T> const &v) {
  int l = v.size();
  for (int i = 0; i < l - 1; i++)
    out << v[i] << ' ';
  if (l > 0)
    out << v[l - 1];
  return out;
}

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&... args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

/*Input*/
int n, k;
vector<pair<int, int>> seg, ans;
/*Input*/

int main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  clock_t tStart = clock();
  int test;
  test = 1;
  // cin >> test;
  while (test--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      seg.push_back(mp(a, -1));
      seg.push_back(mp(b, 1));
    }
    sort(seg.begin(), seg.end());
    // trace("seg",seg);
    int balance = 0, st = -1;
    for (int i = 0; i < seg.size(); i++) {
      if (balance == k - 1 && seg[i].ss == -1) {
        st = seg[i].ff;
      } else if (balance == k && seg[i].ss == 1) {
        ans.push_back(mp(st, seg[i].ff));
      }
      balance -= seg[i].ss;
      // trace("balance,k",balance,k);
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i].ff << ' ' << ans[i].ss << endl;
    }
  }
#ifdef LOCAL_DEFINE
  printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}


/**
 * @Author: harshkhajuria
 * @Date:   01-Feb-2019 10:37:03 pm
 * @Email:  khajuriaharsh729@gmail.com
 * @Filename: The Union of k-Segments.cpp
 * @Last modified by:   harshkhajuria
 * @Last modified time: 03-Jun-2019 02:49:31 pm
 */

#include <bits/stdc++.h>
#define ll long long
#define mp(a, b) make_pair(a, b)
#define newline cout << endl;
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
using namespace std;

template <typename S, typename T>
ostream &operator<<(ostream &out, pair<S, T> const &p) {
  out << '(' << p.ff << ", " << p.ss << ')';
  return out;
}

template <typename T> ostream &operator<<(ostream &out, vector<T> const &v) {
  int l = v.size();
  for (int i = 0; i < l - 1; i++)
    out << v[i] << ' ';
  if (l > 0)
    out << v[l - 1];
  return out;
}

template <typename T> void trace(const char *name, T &&arg1) {
  cout << name << " : " << arg1 << endl;
}

template <typename T, typename... Args>
void trace(const char *names, T &&arg1, Args &&... args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  trace(comma + 1, args...);
}

/*Input*/
int n, k;
vector<pair<int, int>> seg, ans;
/*Input*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  clock_t tStart = clock();
  int test;
  test = 1;
  // cin >> test;
  while (test--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      seg.push_back(mp(a, -1));
      seg.push_back(mp(b, 1));
    }
    sort(seg.begin(), seg.end());
    // trace("seg",seg);
    int balance = 0, st = -1;
    for (int i = 0; i < seg.size(); i++) {
      if (balance == k - 1 && seg[i].ss == -1) {
        st = seg[i].ff;
      } else if (balance == k && seg[i].ss == 1) {
        ans.push_back(mp(st, seg[i].ff));
      }
      balance -= seg[i].ss;
      // trace("balance,k",balance,k);
    }
    cout << ans.size() << endl;
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i].ff << ' ' << ans[i].ss << endl;
    }
  }
#ifdef LOCAL_DEFINE
  printf("Time taken: %.2fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC);
#endif
  return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#define ll long long
#define mp(a,b) make_pair(a,b)
#define si(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
using namespace std;

bool cmp(pair<ll,ll> a,pair<ll,ll> b){
  if(a.first!=b.first)
    return a.first<b.first;
  return a.second<b.second;
}

int main(){
  ll n,k;
  cin >> n >> k;
  std::vector<pair<ll,ll> > v;
  for(ll i=0;i<n;i++){
    ll a,b;
    cin >> a  >> b ;
    v.push_back(mp(a,b));
  }
  sort(v.begin(),v.end(),cmp);
  std::vector<pair<ll,ll> > ans;
  for(ll i=0;i<=v.size()-k;i++){
    ll left = v[i].first;
    ll right = v[i].second;
    // cout << left << ' ' << right << endl;
    bool f = 0;
    for(ll j=i+1;j<i+k;j++){
      ll tleft = v[j].first;
      ll tright = v[j].second;
      // cout << tleft << ' ' << tright << endl;
      if(tleft<=right){
        right=min(right,tright);
        left=max(left,tleft);
      }
      else{
        f=1;
        break;
      }
    }
    // cout << f << endl;
    if(!f){
      ans.push_back(mp(left,right));
    }
  }
  // cout << endl;
  // for(int i=0;i<ans.size();i++){
  //   cout << ans[i].first << ' ' << ans[i].second << endl;
  // }
  std::vector<pair<ll,ll> > tans;
  for(ll i=0;i<ans.size();i++){
    ll left = ans[i].first;
    ll right = ans[i].second;
    if(i!=ans.size()-1){
      ll tleft = ans[i+1].first;
      if(tleft<=right){
        while(i+1<ans.size() && ans[i+1].first<=ans[i].second){
          right = max(right,ans[i+1].second);
          i+=1;
        }
        tans.push_back(mp(left,right));
      }
      else{
        tans.push_back(mp(left,right));
      }
    }
    else{
      tans.push_back(mp(left,right));
    }
  }
  cout << tans.size() << endl;
  for(ll i=0;i<tans.size();i++){
    cout << tans[i].first << ' ' << tans[i].second << endl;
  }
}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#define ll long long
#define mp(a,b) make_pair(a,b)
#define si(n) scanf("%d",&n)
#define sl(n) scanf("%lld",&n)
using namespace std;

bool cmp(pair<ll,ll> a,pair<ll,ll> b){
  if(a.first!=b.first)
    return a.first<b.first;
  return a.second<b.second;
}

int main(){
  ll n,k;
  cin >> n >> k;
  std::vector<pair<ll,ll> > v;
  for(ll i=0;i<n;i++){
    ll a,b;
    cin >> a  >> b ;
    v.push_back(mp(a,b));
  }
  sort(v.begin(),v.end(),cmp);
  std::vector<pair<ll,ll> > ans;
  for(ll i=0;i<=v.size()-k;i++){
    ll left = v[i].first;
    ll right = v[i].second;
    bool f = 0;
    for(ll j=i+1;j<i+k;j++){
      ll tleft = v[j].first;
      ll tright = v[j].second;
      if(tleft<=right && tright>=right){
        right=min(right,tright);
        left=max(left,tleft);
      }
      else{
        f=1;
        break;
      }
    }
    if(!f){
      ans.push_back(mp(left,right));
    }
  }
  std::vector<pair<ll,ll> > tans;
  for(ll i=0;i<ans.size();i++){
    ll left = ans[i].first;
    ll right = ans[i].second;
    if(i!=ans.size()-1){
      ll tleft = ans[i+1].first;
      if(tleft<=right){
        while(i+1<ans.size() && ans[i+1].first<=ans[i].second){
          right = max(right,ans[i+1].second);
          i+=1;
        }
        tans.push_back(mp(left,right));
      }
      else{
        tans.push_back(mp(left,right));
      }
    }
    else{
      tans.push_back(mp(left,right));
    }
  }
  cout << tans.size() << endl;
  for(ll i=0;i<tans.size();i++){
    cout << tans[i].first << ' ' << tans[i].second << endl;
  }
}


/*input

*/
#include<bits/stdc++.h>
#define ll        	long long
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define rrep(i,a,b) for(ll int i=a;i>=b;i--)
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define tr(cont, i) for(auto i=cont.begin();i!=cont.end();i++)
#define ios        	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
#define trace(x)                 cout << #x << ": " << x << endl;
#define trace1(x)                cout << #x << ": " << x << endl;
#define trace2(x, y)             cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
using namespace std;

int main(){
	ll n,k,x,y;
	cin>>n>>k;
	vi l,r;
	rep(i,0,n){
		cin>>x>>y;
		l.pb(x),r.pb(y);
	}
	sort(all(l));
	sort(all(r));
	vector<pii> ans;
	set<ll> start,last;
	rep(i,k-1,n){
		if(l[i]<r[i-k+1]){
			// ans.pb({l[i],r[i-k+1]});
			start.insert(l[i]),last.insert(r[i-k+1]);
		}
	}
	// for(auto i:ans){
	// 	cout<<i.F<<" "<<i.S<<endl;
	// }
	vi torem;
	for(auto i:start){
		if(last.find(i)!=last.end())
			torem.pb(i);
	}
	for(auto i:torem){
		start.erase(i);
		last.erase(i);
	}
	cout<<sz(start)<<endl;
	auto i=start.begin(),j=last.begin();
	while(i!=start.end()){
		cout<<(*(i))<<" "<<(*(j))<<endl;
		i++;
		j++;
	}
	return 0;
}


/*input

*/
#include<bits/stdc++.h>
#define ll        	long long
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define pb          push_back
#define mp          make_pair
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define rrep(i,a,b) for(ll int i=a;i>=b;i--)
#define all(a)      (a).begin(),(a).end()
#define hell        1000000007
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define tr(cont, i) for(auto i=cont.begin();i!=cont.end();i++)
#define ios        	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"
#define trace(x)                 cout << #x << ": " << x << endl;
#define trace1(x)                cout << #x << ": " << x << endl;
#define trace2(x, y)             cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
using namespace std;

int main(){
	ll n,k,x,y;
	cin>>n>>k;
	vi l,r;
	rep(i,0,n){
		cin>>x>>y;
		l.pb(x),r.pb(y);
	}
	sort(all(l));
	sort(all(r));
	vector<pii> ans,sol;
	set<ll> start,last;
	rep(i,k-1,n){
		if(l[i]<=r[i-k+1]){
			ans.pb({l[i],r[i-k+1]});
			start.insert(l[i]),last.insert(r[i-k+1]);
		}
	}
	if(sz(ans)>0)
		x=ans[0].F,y=ans[0].S;
	rep(i,1,sz(ans)){
		if(ans[i].F>y){
			sol.pb({x,y});
			x=ans[i].F;
			y=ans[i].S;
		}
		else{
			y=ans[i].S;
		}
	}
	if(sz(ans)>0)
		sol.pb({x,y});
	cout<<sz(sol)<<endl;
	for(auto i:sol){
		cout<<i.F<<" "<<i.S<<endl;
	}
	return 0;
}