/*

                          *                     *
                           *   *           *   *
                            *        **       *
                             *     *    *    *
                              *             *
                          *    *           *    *
                      * * * * * * * * * * * * * * * *
                                 *       *
                                  *     *
                                   *   *
                                    * *
                                     *

    Listen! Don't you hear these mad symphonies of grievance and fear ?
    Melancholy and despair can be sensed when we draw near. Some hear a violin
    sound, others hear a man moaning in tears. These fields are haunted by
    nature's most sombre melodies. Suicidal white noise absorbing the essence
    from light, mirth and vitality.

*/
#include <bits/stdc++.h>
#define s scanf
#define p printf
#define pf push_front
#define pp push_back
#define mp make_pair
#define se second
#define fi first
#define OO 1e9
#define EPS 1e-9
#define __ 1000000007
#define V(a) vector <a>
#define P(a,b) pair <a,b>
#define II pair <int, int>
#define ALL(a) a.begin(),a.end()
#define mm(a) memset(a , -1 , sizeof a)
#define mm0(a) memset(a , 0 , sizeof a)
typedef unsigned long long int ull;
typedef long long int ll; using namespace std;
int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; }
ll MOD(ll a, ll b) { return a >= 0 ? a % b : (b - abs(a % b)) % b; }
void MN(int &a, int b) { if (a > b) a = b; }
void MX(int &a, int b) { if (a < b) a = b; }
int X[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int Y[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int main() { int n, t, cw = 0; s("%d", &n); V(int) rep(5001);
    for (int i = 0; i < n; i++) { s("%d", &t); rep[t]++; }
    for (int i = 1; i <= n; i++){
        if (rep[i] > 1) cw += (rep[i] - 1);
    } p("%d\n", cw);
    return 0;
}


/*

                          *                     *
                           *   *           *   *
                            *        **       *
                             *     *    *    *
                              *             *
                          *    *           *    *
                      * * * * * * * * * * * * * * * *
                                 *       *
                                  *     *
                                   *   *
                                    * *
                                     *

    Listen! Don't you hear these mad symphonies of grievance and fear ?
    Melancholy and despair can be sensed when we draw near. Some hear a violin
    sound, others hear a man moaning in tears. These fields are haunted by
    nature's most sombre melodies. Suicidal white noise absorbing the essence
    from light, mirth and vitality.

*/
#include <bits/stdc++.h>
#define s scanf
#define p printf
#define pf push_front
#define pp push_back
#define mp make_pair
#define se second
#define fi first
#define OO 1e9
#define EPS 1e-9
#define __ 1000000007
#define V(a) vector <a>
#define P(a,b) pair <a,b>
#define II pair <int, int>
#define ALL(a) a.begin(),a.end()
#define mm(a) memset(a , -1 , sizeof a)
#define mm0(a) memset(a , 0 , sizeof a)
typedef unsigned long long int ull;
typedef long long int ll; using namespace std;
int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; }
ll MOD(ll a, ll b) { return a >= 0 ? a % b : (b - abs(a % b)) % b; }
void MN(int &a, int b) { if (a > b) a = b; }
void MX(int &a, int b) { if (a < b) a = b; }
int X[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int Y[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int main() { int n , cw = 0; s("%d", &n); V(int) seq(n + 1); seq[0] = -OO;
    for (int i = 1; i < n; i++) s("%d", &seq[i]); sort(ALL(seq));
    for (int i = 1; i <= n; i++){
        if (seq[i] != i) cw++;
    } p("%d\n", cw);
    return 0;
}


/*

                          *                     *
                           *   *           *   *
                            *        **       *
                             *     *    *    *
                              *             *
                          *    *           *    *
                      * * * * * * * * * * * * * * * *
                                 *       *
                                  *     *
                                   *   *
                                    * *
                                     *

    Listen! Don't you hear these mad symphonies of grievance and fear ?
    Melancholy and despair can be sensed when we draw near. Some hear a violin
    sound, others hear a man moaning in tears. These fields are haunted by
    nature's most sombre melodies. Suicidal white noise absorbing the essence
    from light, mirth and vitality.

*/
#include <bits/stdc++.h>
#define s scanf
#define p printf
#define pf push_front
#define pp push_back
#define mp make_pair
#define se second
#define fi first
#define OO 1e9
#define EPS 1e-9
#define __ 1000000007
#define V(a) vector <a>
#define P(a,b) pair <a,b>
#define II pair <int, int>
#define ALL(a) a.begin(),a.end()
#define mm(a) memset(a , -1 , sizeof a)
#define mm0(a) memset(a , 0 , sizeof a)
typedef unsigned long long int ull;
typedef long long int ll; using namespace std;
int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; }
ll MOD(ll a, ll b) { return a >= 0 ? a % b : (b - abs(a % b)) % b; }
void MN(int &a, int b) { if (a > b) a = b; }
void MX(int &a, int b) { if (a < b) a = b; }
int X[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int Y[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int main() { int n, t, cw = 0; s("%d", &n); V(int) rep(5001);
    for (int i = 0; i < n; i++) { s("%d", &t); rep[t]++; }
    for (int i = 0; i < n; i++){
        if (rep[i] > 1) cw += (rep[i] - 1);
    } p("%d\n", cw);
    return 0;
}


/*

                          *                     *
                           *   *           *   *
                            *        **       *
                             *     *    *    *
                              *             *
                          *    *           *    *
                      * * * * * * * * * * * * * * * *
                                 *       *
                                  *     *
                                   *   *
                                    * *
                                     *

    Listen! Don't you hear these mad symphonies of grievance and fear ?
    Melancholy and despair can be sensed when we draw near. Some hear a violin
    sound, others hear a man moaning in tears. These fields are haunted by
    nature's most sombre melodies. Suicidal white noise absorbing the essence
    from light, mirth and vitality.

*/
#include <bits/stdc++.h>
#define s scanf
#define p printf
#define pf push_front
#define pp push_back
#define mp make_pair
#define se second
#define fi first
#define OO 1e9
#define EPS 1e-9
#define __ 1000000007
#define V(a) vector <a>
#define P(a,b) pair <a,b>
#define II pair <int, int>
#define ALL(a) a.begin(),a.end()
#define mm(a) memset(a , -1 , sizeof a)
#define mm0(a) memset(a , 0 , sizeof a)
typedef unsigned long long int ull;
typedef long long int ll; using namespace std;
int OneD(int row, int col, int t_n_o_r) { return row * t_n_o_r + col; }
ll MOD(ll a, ll b) { return a >= 0 ? a % b : (b - abs(a % b)) % b; }
void MN(int &a, int b) { if (a > b) a = b; }
void MX(int &a, int b) { if (a < b) a = b; }
int X[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int Y[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int main() { int n, t, cw = 0; s("%d", &n); V(int) rep(5001);
    for (int i = 0; i < n; i++) { s("%d", &t); rep[t]++; }
    for (int i = 1; i <= n; i++){
        if (rep[i] == 0) cw ++;
    } p("%d\n", cw);
    return 0;
}


#include <stdio.h>
#include <cstring>

using namespace std;

int main(){
	int n, a, count;
	int numbers[5002];

	memset(numbers, 0, 5002 * sizeof(int));

	scanf("%d", &n);
	count = n;
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &a);
		if (a <= n && numbers[a] == 0){
			count--;
			numbers[a] = 1;
		}
	}

	printf("%d", count);

	return 0;
}
// 1459346374334


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

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int& x : v) cin >> x;
  vector<int> mark(5001, 0);
  for (int x : v) mark[x] = 1;
  int cost = 0;
  for (int j = 1; j <= n; ++j) {
    if (mark[j] == 0) ++cost;
  }
  cout << cost << endl;
  return 0;
}
// 1459344893310


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

const int SIZE = 5005;

int qnt[SIZE];

int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i++) {
		int x;
		cin >> x;
		if(x <= n) {
			qnt[x]++;
		}
	}
	int t = 0;
	for(int i = 1; i <= n; i++) {
		if(!qnt[i]) t++;
	}
	cout << t << '\n';
}
// 1520008082541


#include <bits/stdc++.h>
#define fr(a, b, c) for(int a = b; a < c; ++a)

using namespace std;
typedef pair<int, int> ii;
typedef long long ll;

int v[51234];
int main() {
    ios::sync_with_stdio(0);
    int n;
    cin >> n;
    fr(i, 0, n) cin >> v[i];
    sort(v, v + n);
    int res = 0;
    fr(i, 1, n + 1) {
        res +=  1 - binary_search(v, v + n, i);
    }
    cout << res << endl;
    return 0;
}
// 1520008329680


#include <cstdio>
#include <vector>
using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    vector<bool> numbers(n, false);
    while (n--)
    {
        int i;
        scanf("%d", &i);
        numbers[i - 1] = true;
    }

    int changes = 0;
    const auto size = numbers.size();
    for (int i = 0; i < size; ++i)
        if (numbers[i])
            ++changes;

    printf("%d\n", changes);
    return 0;
}
// 1459425516046


#include <cstdio>
#include <vector>
using namespace std;

int main()
{
    int n, changes;
    scanf("%d", &n);

    changes = n;
    vector<bool> numbers(n, false);
    for (int i = 0, a; i < n; ++i)
    {
        scanf("%d", &a);

        if (a - 1 < 0 || a - 1 >= n)
            continue;

        if (!numbers[a - 1])
            --changes;

        numbers[a - 1] = true;
    }

    printf("%d\n", changes);
    return 0;
}
// 1459434216994


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a (n);
    vector<int> b (n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    for (int i = 0; i < n; ++i) {
        if(a[i] > 0 && a[i]<= n){
            b[i] = 1;
        }
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += b[i];
    }
    cout << n - sum << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n+1, 0);
    for (int i = 1; i <= n; ++i) {
        int x;
        cin >> x;
        if (x >= 1 && x <= n) {
            a[x] = 1;
        }
    }
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
        sum += a[i];
    }
    cout << n - sum << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n, 0);
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (x >= 1 && x <= n) {
            a[x] = 1;
        }
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += a[i];
    }
    cout << n - sum << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n, 0);
    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if (x >= 1 && x <= n) {
            a[i] = 1;
        }
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += a[i];
    }
    cout << n - sum << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a(n+1, 0);
    for (int i = 1; i <= n; ++i) {
        int x;
        cin >> x;
        if (x >= 1 && x <= n) {
            a[x] = 1;
        }
    }
    for(int i: a){
        cout << i << endl;
    }
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
        sum += a[i];
    }
    cout << n - sum << endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> a (n);
    vector<int> b (n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    for (int i = 0; i < n; ++i) {
        if(a[i] >= 1 && a[i]<= n){
            b[i] = 1;
        }
    }
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += b[i];
    }
    cout << n - sum << endl;
}


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

#define pb push_back
#define F first
#define S second
#define fo(i,a,b)  for(int i = a; i < b; i++)

using ll = long long;
using ld = long double;
using ul = unsigned long long;
using ii = pair<int, int>;
#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ReadFile            freopen("input.in","r",stdin)
#define WriteFile           freopen("output.txt","w",stdout)
const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;
const ld EPS = 1e-7;
double Pi=acos(-1);
const ld rad=Pi/180.0;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll nCr (ll n,ll m)
{
        return(m == 0)?1:n*nCr(n-1,m-1)/m;
}
/*****************************************************/

int main()
{
    int n;

    cin >> n;

    set<int> st;

    for(int i = 0; i < n; ++i){
        int x;
        cin >> x;
        st.insert(x);
    }
    int cnt = 0;
    for(auto it = st.begin(); it != st.end(); ++it){
        if(*it <= n)
            cnt++;
    }

    cout << n - cnt << "\n";

    return 0;
}


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

#define pb push_back
#define F first
#define S second
#define fo(i,a,b)  for(int i = a; i < b; i++)

using ll = long long;
using ld = long double;
using ul = unsigned long long;
using ii = pair<int, int>;
#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ReadFile            freopen("input.in","r",stdin)
#define WriteFile           freopen("output.txt","w",stdout)
const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;
const ld EPS = 1e-7;
double Pi=acos(-1);
const ld rad=Pi/180.0;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll nCr (ll n,ll m)
{
        return(m == 0)?1:n*nCr(n-1,m-1)/m;
}
/*****************************************************/

int main()
{
    int n;

    cin >> n;

    int a[n];

    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    int x = 0, mx=-1;

    for(int i = 0; i < n-1; ++i){
        if(a[i] == a[i+1])
            x++;
        if(a[i]>mx)
            mx = a[i];
    }

    cout << (mx > n ? x+1 : x) << "\n";

    return 0;
}


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

#define pb push_back
#define F first
#define S second
#define fo(i,a,b)  for(int i = a; i < b; i++)

using ll = long long;
using ld = long double;
using ul = unsigned long long;
using ii = pair<int, int>;
#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ReadFile            freopen("input.in","r",stdin)
#define WriteFile           freopen("output.txt","w",stdout)
const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;
const ld EPS = 1e-7;
double Pi=acos(-1);
const ld rad=Pi/180.0;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll nCr (ll n,ll m)
{
        return(m == 0)?1:n*nCr(n-1,m-1)/m;
}
/*****************************************************/

int main()
{
    int n;

    cin >> n;

    int a[n];

    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    int x = 0;

    for(int i = 0; i < n-1; ++i){
        if(a[i] == a[i+1])
            x++;
    }

    cout << x << "\n";

    return 0;
}


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

#define pb push_back
#define F first
#define S second
#define fo(i,a,b)  for(int i = a; i < b; i++)

using ll = long long;
using ld = long double;
using ul = unsigned long long;
using ii = pair<int, int>;
#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ReadFile            freopen("input.in","r",stdin)
#define WriteFile           freopen("output.txt","w",stdout)
const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;
const ld EPS = 1e-7;
double Pi=acos(-1);
const ld rad=Pi/180.0;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll nCr (ll n,ll m)
{
        return(m == 0)?1:n*nCr(n-1,m-1)/m;
}
/*****************************************************/

int main()
{
    int n;
    cin >> n;
    set<int> st;
    int mx = -1 , cnt = 0;
    for(int i = 1; i <= n; ++i){
        int x;
        cin >> x;
        mx = max(mx , x);
        st.insert(x);
    }
    int ans = ((int)st.size());
    cout << ((mx > n) ? (n-ans)+1 : (n - ans)) << "\n";

    return 0;
}


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

#define pb push_back
#define F first
#define S second
#define fo(i,a,b)  for(int i = a; i < b; i++)

using ll = long long;
using ld = long double;
using ul = unsigned long long;
using ii = pair<int, int>;
#define Mosta     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ReadFile            freopen("input.in","r",stdin)
#define WriteFile           freopen("output.txt","w",stdout)
const ll mod7 = 1000000007,mod = 1000000000,OO = 0x3f3f3f3f;
const ld EPS = 1e-7;
double Pi=acos(-1);
const ld rad=Pi/180.0;
ll power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}
ll nCr (ll n,ll m)
{
        return(m == 0)?1:n*nCr(n-1,m-1)/m;
}
/*****************************************************/

int main()
{
    int n;

    cin >> n;

    int a[n];

    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    int x = 0, mx=-1;
    sort(a , a+n);
    for(int i = 0; i < n-1; ++i){
        if(a[i] == a[i+1])
            x++;
        if(a[i]>mx)
            mx = a[i];
    }

    cout << (mx > n ? x+1 : x) << "\n";

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
 multiset<int> st;
int main(){
int N,check=0;
cin>>N;
int a[N];
set <int> st;
for(int i=0; i<N;i++){
    cin>>a[i];
    st.insert(a[i]);
}

int cont=N-st.size();
sort(a,a+N);/*

int j=1;
for(int i=1;i<N;i++)
{
    if(a[i]-a[i-1)!=1{
     a[i]=j;
     cont++;
    }
    j++;
}*/
cout<<cont<<endl;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
 multiset<int> st;
int main(){
int N;
cin>>N;
int a[N];
for(int i=0; i<N;i++){
    cin>>a[i];
    ///st.insert(a[i]);
}
int cont=0;
sort(a,a+N);
}
for(int i=1;i<N;i++)
{
    if(a[i]-a[i-1] !=1){
            cont++;
            a[i]=a[i-1]+1;

    }
}
cout<<cont<<endl;
return 0;
}


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
	<FileVersion major="1" minor="6" />
	<Project>
		<Option title="Modification" />
		<Option pch_mode="2" />
		<Option compiler="gcc" />
		<Build>
			<Target title="Debug">
				<Option output="bin/Debug/Modification" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Debug/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-g" />
				</Compiler>
			</Target>
			<Target title="Release">
				<Option output="bin/Release/Modification" prefix_auto="1" extension_auto="1" />
				<Option object_output="obj/Release/" />
				<Option type="1" />
				<Option compiler="gcc" />
				<Compiler>
					<Add option="-O2" />
				</Compiler>
				<Linker>
					<Add option="-s" />
				</Linker>
			</Target>
		</Build>
		<Compiler>
			<Add option="-Wall" />
			<Add option="-fexceptions" />
		</Compiler>
		<Unit filename="main.cpp" />
		<Extensions>
			<code_completion />
			<envvars />
			<debugger />
			<lib_finder disable_auto="1" />
		</Extensions>
	</Project>
</CodeBlocks_project_file>


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int N;
    set<int> st;
    cin>>N;
    int a[N];
    for(int i=0;i<N;i++)
    {
        cin>>a[i];
        st.insert(a[i]);
    }
    int cont=N;

   /// int cont=N-st.size();
    set<int>::iterator it=st.begin();
    while(it!=st.end() && *it<=N)
    {
     cont--;
     it++;
    }
    cout<<cont<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
 multiset<int> st;
int main(){
int N,check=0;
cin>>N;
int a[N];
for(int i=0; i<N;i++){
    cin>>a[i];
}
int cont=0;
sort(a,a+N);
for(int i=1;i<N;i++){
    if(a[i]==a[i-1])
        check++;
}
int j=1;
for(int i=0;i<N;i++)
{
    if(a[i]!=j){
     cont++;
    }
    j++;
}
cout<<cont<<endl;
return 0;
}


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

int main()
{
int n ;
cin >> n ;
int a[n] ;
for(int i = 0 ; i<n ; i++){
cin >> a[i] ;
}
sort(a,a + n);
int cnt = 0 ;
for(int i = 1 ; i<n ;i++){
for(int j = i-1 ; j<i ;j++)
{
    if(a[i]!=a[j]) ++cnt ;
}
}
cout << (n-1)-cnt << endl ;

    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

int main(){
int i,n,g,p[5000]={0},x;
cin>>n;
g = n ;
while(g--)
{
cin>>x;
p[x-1]++;
}

cout<<count(p,p+n,0); // good algorithm

}


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

int main()
{
int n ;
cin >> n ;
int a[n] ;
for(int i = 0 ; i<n ; i++){
cin >> a[i] ;
}
sort(a,a + n);
int cnt = 0 ;
for(int i = 1 ; i<n ;i++){
for(int j = i-1 ; j<i ;j++)
{
    if(a[i]==a[j]) ++cnt ;
}
}
cout << cnt << endl ;
    return 0;
}


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

int main()
{
int n ;
cin >> n ;
int a[n] ;
for(int i = 0 ; i<n ; i++){
cin >> a[i] ;
}
sort(a,a + n);
int cnt = 0 ;
for(int i = 1 ; i<n ;i++){
for(int j = i-1 ; j<i ;j++)
{
	if(a[i]==a[j]) ++cnt ;
}
}
cout << cnt << endl ;
    return 0;
}


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

int main()
{
int n ;
cin >> n ;
int x[n] , cnt = 0 ;
for(int i=0;i<n ;i++){
    x[i]=(i+1) ;
}

int a[n] ;
for(int i = 0 ; i<n ; i++){
cin >> a[i] ;
}

for(int i = 0 ; i<n ;i++){
for(int j = 0 ; j<n ;j++)
{
    if(a[i]==x[j]){
     x[j]=0 ;
     ++cnt ;
     }
}
}

cout << n-cnt << endl ;

    return 0;
}


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

int main()
{
    int n;
    cin>>n;

    set<int>s;
    int c=0;
    for(int i=0;i<n;++i){
        int x;
        cin>>x;
        if(x>n)c++;
        s.insert(x);
    }
    cout<<n-s.size()+c;

}


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

int main()
{
    int n;
    cin>>n;

    set<int>s;
    int c=0;
    for(int i=0;i<n;++i){
        int x;
        if(x>n)c++;
        cin>>x;
        s.insert(x);
    }
    cout<<n-s.size()+c;

}


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

int main()
{
    int n;
    cin>>n;

    set<int>s;
    int c=0;
    for(int i=0;i<n;++i){
        int x;
        cin>>x;
        s.insert(x);
    }
    for(int x:s){
        if(x>n)c++;
    }
    cout<<n-s.size()+c;

}


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

int main()
{
    int n;
    cin>>n;

    set<int>s;

    for(int i=0;i<n;++i){
        int x;
        cin>>x;
        s.insert(x);
    }
    cout<<n-s.size();

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e5+5,MOD=1e9+7;

int main()
{
    set<int> s;
    int n,x;
    cin>>n;
    int mx=1;
    for (int i=0;i<n;i++){
        cin>>x;
        mx=max(mx,x);
        s.insert(x);
    }
    cout<<max(n,mx)-s.size();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e5+5,MOD=1e9+7;

int main()
{
    set<int> s;
    int n,x;
    cin>>n;
    int mx=1;
    for (int i=0;i<n;i++){
        cin>>x;
        mx=max(mx,x);
        s.insert(x);
    }
    cout<<mx-s.size();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e5+5,MOD=1e9+7;

int main()
{
    set<int> s;
    int n,x;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>x;
        s.insert(x);
    }
    cout<<n-s.size();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e5+5,MOD=1e9+7;

int main()
{
    set<int> s;
    int n,x;
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>x;
        if (x && x<=n)s.insert(x);
    }
    cout<<n-s.size();
    return 0;
}


//CSDN博客：https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define ull unsigned long long
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
const int MAXN=5e3+10;
int n,book[MAXN],ans;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	int t;
	while(n--)
	{
		cin>>t;
		if(book[t]||t>n) ans++;
		else book[t]++;
		book[t]++;
	}
	cout<<ans<<endl;
}


//CSDN博客：https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define ull unsigned long long
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
const int MAXN=5e3+10;
int n,book[MAXN],ans;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	int t;
	while(n--)
	{
		cin>>t;
		if(book[t]||t>n) ans++;
		else book[t]++;
	}
	cout<<ans<<endl;
}


//CSDN博客：https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define ull unsigned long long
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
const int MAXN=5e3+10;
int n,book[MAXN],ans;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	int t;
	for(int i=1;i<=n;++i)
	{
		cin>>t;
		if(book[t]||t>n) ans++;
		else book[t]++;
	}
	cout<<ans<<endl;
}


//CSDN博客：https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define ull unsigned long long
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
const int MAXN=5e3+10;
int n,book[MAXN],ans;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	int t;
	while(n--)
	{
		cin>>t;
		if(book[t]) ans++;
		else book[t]++;
	}
	cout<<ans<<endl;
}


//CSDN博客：https://blog.csdn.net/qq_40889820
#include<iostream>
#include<sstream>
#include<fstream>
#include<algorithm>
#include<string>
#include<cstring>
#include<iomanip>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define mem(a,b) memset(a,b,sizeof(a))
#define random(a,b) (rand()%(b-a+1)+a)
#define ull unsigned long long
#define e 2.71828182
#define Pi 3.141592654
using namespace std;
const int MAXN=5e5+10;
int n,book[MAXN],ans;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	int t;
	while(n--)
	{
		cin>>t;
		if(book[t]) ans++;
		else book[t]++;
	}
	cout<<ans<<endl;
}


//in the name of GOD//
#include <bits/stdc++.h>

#define ll long long int
#define ldu long double
#define ISO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(x, n) for(ll i = x; i < n; i++)
#define ashar(x,d) fixed<<setprecision(x)<<d
/*
#define pii pair<int , int>
#define mp make_pair
#define S second
#define F first
#define pb push_back
#define po pop_back()
*/
using namespace std;

const ll mx = 1e6+10, inf = 1e9+10;
int n, a[mx], p, ans;

int main()
{
	ISO

  cin >> n;

  rep(0, n) cin >> a[i];
  sort(a, a+n);
  p = a[0];

  rep(1, n)
  {
    if(a[i] == a[i-1])
    {
      ans+= (a[i] - a[i-1]) + 1;
    }
  }

  cout << ans;

	return 0;
}


//in the name of GOD//
#include <bits/stdc++.h>

#define ll long long int
#define ldu long double
#define ISO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(x, n) for(ll i = x; i < n; i++)
#define ashar(x,d) fixed<<setprecision(x)<<d
/*
#define pii pair<int , int>
#define mp make_pair
#define S second
#define F first
#define pb push_back
#define po pop_back()
*/
using namespace std;

const ll mx = 1e6+10, inf = 1e9+10;
int n, a[mx], p, ans;

int main()
{
	ISO

  cin >> n;

  rep(0, n) cin >> a[i];
  sort(a, a+n);

  if(a[0] == a[n-1])
  {
      (a[0] >= 1 && a[0] <= n)? cout << n-1: cout << n;
      return 0;
  }

  rep(1, n)
  {
    if(a[i] == a[i-1]) ans+= (a[i] - a[i-1]) + 1;
  }

  cout << ans;

	return 0;
}


//in the name of GOD//
#include <bits/stdc++.h>

#define ll long long int
#define ldu long double
#define ISO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(x, n) for(ll i = x; i < n; i++)
#define ashar(x,d) fixed<<setprecision(x)<<d
/*
#define pii pair<int , int>
#define mp make_pair
#define S second
#define F first
#define pb push_back
#define po pop_back()
*/
using namespace std;

const ll mx = 1e6+10, inf = 1e9+10;
int n, a[mx], p, ans;

int main()
{
	ISO

  cin >> n;

  rep(0, n) cin >> a[i];
  sort(a, a+n);

  rep(1, n+1)
  {
    for(int j = 0; j<n; j++)
    {
      if(a[j] == i)
      {
        ans++;
        break;
      }
    }
  }

  cout << n - ans;

	return 0;
}


//in the name of GOD//
#include <bits/stdc++.h>

#define ll long long int
#define ldu long double
#define ISO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(x, n) for(ll i = x; i < n; i++)
#define ashar(x,d) fixed<<setprecision(x)<<d
/*
#define pii pair<int , int>
#define mp make_pair
#define S second
#define F first
#define pb push_back
#define po pop_back()
*/
using namespace std;

const ll mx = 1e6+10, inf = 1e9+10;
int n, a[mx], p, ans;

int main()
{
	ISO

  cin >> n;

  rep(0, n) cin >> a[i];
  sort(a, a+n);
  p = a[0];

  rep(0, n)
  {
    if(a[i] == p)
    {
      ans++;
      p++;
    }
  }

  cout << n - ans;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,i,j,cnt=0;
	int* a;
	int arr[50001]={0};
	cin>>n;
	a=new int[n];
	for(i=0;i<n;i++)
	{
		cin>>a[i];
		arr[a[i]]++;
	}
	for(i=1;i<=n;i++)
	{
		if(arr[i]==0)
		{
			cnt++;
		}
	}
	cout<<cnt<<"\n";
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl

using namespace std;
typedef long long  ll;

bool trytoadd(int &i0 , int &j0 , int n , pair<int , int> &p){
    if(j0 < n){
        p = {i0 , ++j0};
        return true;
    }
    else if(j0 == n){
        i0++;
    }
    for(int i = i0 ; i <= n ; ++i){
        for(int j = i + 2 ; j <= n ; ++j){
            p = {i0 , j};
            i0 = i;
            j0 = j;
            return true;
        }
    }
    return false;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r" , stdin);
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    int n , m;
    cin >> n >> m;
    vector<pair<pair<int , int> , int> > v(m);
    for(int i = 0 ; i < m ; ++i){
        cin >> v[i].first.first >> v[i].first.second;
        v[i].second = i;
        v[i].first.second ^= 1;
    }
    // 0 included  1 is not

    sort(all(v));
    vector<pair<int , int> > res(m);
    int i0 = 1 , j0 = 2 , cur = 1;
    pair<int , int> p;
    for(int i = 0 ; i < m ; ++i){
        if(v[i].first.second){
            bool f = trytoadd(i0 , j0 , cur , p);
            if(!f){
                cout << "-1\n";
                return 0;
            }
        }
        else{
            p = {cur , cur + 1};
            ++cur;
        }
        res[v[i].second] = p;
    }

    for(auto e : res)
        cout << e.first << ' ' << e.second << "\n";
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl

using namespace std;
typedef long long  ll;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r" , stdin);
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    int n , m;
    cin >> n >> m;
    vector<pair<int , int> > edges;
    vector<pair<pair<int , int> , int> > v(m);
    for(int i = 0 ; i < m ; ++i){
        cin >> v[i].first.first >> v[i].first.second;
        v[i].second = i;
        v[i].first.second ^= 1;
    }
    // 0 included  1 is not
    sort(all(v));
    vector<pair<int , int> > res(m);
    int curNodes = 1 , fr = 1 , to = 3;
    pair<int , int> p;
    for(int i = 0 ; i < m ; ++i){
        if(v[i].first.second){
            if(to <= curNodes){
                p = {fr , to};
                if(fr == to - 2)
                    to++ , fr = 1;
                else fr++;
            }
            else{
                cout << "-1\n";
                return 0;
            }
        }
        else{
            p = {curNodes , curNodes + 1};
            ++curNodes;
        }
        res[v[i].second] = p;
    }
    for(auto e : res)
        cout << e.first << ' ' << e.second << "\n";
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl

using namespace std;
typedef long long  ll;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r" , stdin);
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    srand(time(NULL));
    int n , m;
    cin >> n >> m;
    vector<pair<int , int> > edges;
    int rem = 1000001;
    for(int i = 1 ; rem && i <= n ; ++i){
        for(int j = i + 2 ; rem && j <= n ; ++j){
            rem--;
            edges.pb({i , j});
        }
    }
    sort(all(edges) , [](const pair<int , int> &a , const pair<int , int> &b){
         if(a.second == b.second)
            return a.first < b.first;
         return a.second < b.second;
    });
//    for(auto e : edges)
//        cout << e.first << ' ' << e.second << "\n";
    vector<pair<pair<int , int> , int> > v(m);
    for(int i = 0 ; i < m ; ++i){
        cin >> v[i].first.first >> v[i].first.second;
        v[i].second = i;
        v[i].first.second ^= 1;
    }
    // 0 included  1 is not
    sort(all(v));
    vector<pair<int , int> > res(m);
    int cur = 1 , j = 0;
    pair<int , int> p;
    for(int i = 0 ; i < m ; ++i){
        if(v[i].first.second){
            if(edges[j].second <= cur){
                p = edges[j++];
            }
            else{
                cout << "-1\n";
                return 0;
            }
        }
        else{
            p = {cur , cur + 1};
            ++cur;
        }
        res[v[i].second] = p;
    }
    for(auto e : res)
        cout << e.first << ' ' << e.second << "\n";
}


#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl

using namespace std;
typedef long long  ll;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r" , stdin);
#endif
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    srand(time(NULL));
    int n , m;
    cin >> n >> m;
    vector<pair<int , int> > edges;
    int rem = m - n + 1;
    for(int i = 1 ; rem && i <= n ; ++i){
        for(int j = i + 2 ; rem && j <= n ; ++j){
            rem--;
            edges.pb({i , j});
        }
    }
    vector<pair<pair<int , int> , int> > v(m);
    for(int i = 0 ; i < m ; ++i){
        cin >> v[i].first.first >> v[i].first.second;
        v[i].second = i;
        v[i].first.second ^= 1;
    }
    // 0 included  1 is not
    sort(all(v));
    vector<pair<int , int> > res(m);
    int cur = 1 , j = 0;
    pair<int , int> p;
    for(int i = 0 ; i < m ; ++i){
        if(v[i].first.second){
            if(edges[j].second <= cur){
                p = edges[j++];
            }
            else{
                cout << "-1\n";
                return 0;
            }
        }
        else{
            p = {cur , cur + 1};
            ++cur;
        }
        res[v[i].second] = p;
    }

    for(auto e : res)
        cout << e.first << ' ' << e.second << "\n";
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define p(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define s(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define sf(x) scanf("%lf", &x)
#define bitcount __builtin_popcountll
#define INF 1e15+9
#define endl '\n'
#define FIO ios_base::sync_with_stdio(false)
using namespace std;
#define M 1000000007
#define MAX 100005
#define MLOG 18
struct node{
	int val, bit, x, y, idx;
}a[MAX];
set <pair <int,int> > s;
bool cmp1(node a, node b){
	if(a.val!=b.val)
	return a.val<b.val;
	else
		return (a.bit>b.bit);
}
bool cmp2(node a, node b){
	return a.idx<b.idx;
}
int main(){
	int n, m, i, x, y, j;
	s(n); s(m);
	for(i = 0; i < m; ++i){
		s(a[i].val); s(a[i].bit);
		a[i].idx = i;
	}
	sort(a, a+m, cmp1);
	int cur = 2;
	for(i = 0; i < m; ++i){
		if(a[i].bit == 1){
			a[i].x = 1;
			a[i].y = cur;
 			if(s.size() < m){
				for(j = 2; j < cur; ++j){
					s.insert(mp(j, cur));
				}
			}
			cur++;
		}
		else{
			if(s.size() == 0){
				cout << "-1\n";
				return 0;
			}
			auto it = s.begin();
			a[i].x = it->fi;
			a[i].y = it->se;
			s.erase(it);
		}
	}
	sort(a,a+m, cmp2);
	for(i = 0; i < m; ++i){
		cout << a[i].x << " " << a[i].y << endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define p(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define s(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define sf(x) scanf("%lf", &x)
#define bitcount __builtin_popcountll
#define INF 1e15+9
#define endl '\n'
#define FIO ios_base::sync_with_stdio(false)
using namespace std;
#define M 1000000007
#define MAX 100005
#define MLOG 18
struct node{
	int val, bit, x, y, idx;
}a[MAX];
set <pair <int,int> > s;
bool cmp1(node a, node b){
	return a.val<b.val;
}
bool cmp2(node a, node b){
	return a.idx<b.idx;
}
int main(){
	int n, m, i, x, y, j;
	s(n); s(m);
	for(i = 0; i < m; ++i){
		s(a[i].val); s(a[i].bit);
		a[i].idx = i;
	}
	sort(a, a+m, cmp1);
	int cur = 2;
	for(i = 0; i < m; ++i){
		if(a[i].bit == 1){
			a[i].x = 1;
			a[i].y = cur;
 			if(s.size() < m-n+1){
				for(j = 2; j < cur; ++j){
					s.insert(mp(j, cur));
				}
			}
			cur++;
		}
		else{
			if(s.size() == 0){
				cout << "-1\n";
				return 0;
			}
			auto it = s.begin();
			a[i].x = it->fi;
			a[i].y = it->se;
			s.erase(it);
		}
	}
	sort(a,a+m, cmp2);
	for(i = 0; i < m; ++i){
		cout << a[i].x << " " << a[i].y << endl;
	}
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define pb push_back
#define pi pair<int,int>
#define F first
#define S second
#define pii pair<pi,int>
vector< pii > v;
pi ans[N];

bool cmp(pii x,pii y)
{
    if(x.F.F==y.F.F)
        return x.F.S>y.F.S;
    return x.F.F < y.F.F;
}

int main()
{
    int n,m,i,x,y;
    cin>>n>>m;

    for(i=0;i<m;i++)
    {
        cin>>x>>y;
        v.pb({{x,y},i});
    }
    sort(v.begin(),v.end(),cmp);

    if(v[0].F.S!=1)
    {
        cout<<-1;
        return 0;
    }
    if(v[1].F.S!=1)
    {
        cout<<-1;
        return 0;
    }
    ans[v[0].S].F=1;
    ans[v[0].S].S=2;

    ans[v[1].S].F=1;
    ans[v[1].S].S=3;

    int t1=2,t2=3,curr=3;
    for(i=2;i<v.size();i++)
    {
        if(v[i].F.S==1)
        {
            curr++;
            ans[v[i].S].F=1;
            ans[v[i].S].S=curr;
        }
        else
        {
            if(t2>curr)
            {
                t1++;
                t2=t1+1;

                if(t1>=curr)
                {
                    cout<<-1;
                    return 0;
                }
            }
            ans[v[i].S].F=t1;
            ans[v[i].S].S=t2;

            t2++;
        }
    }
    for(i=0;i<m;i++)
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define pb push_back
#define pi pair<int,int>
#define F first
#define S second
#define pii pair<pi,int>
set<pi> avail;
vector< pii > v;
pi ans[N];

bool cmp(pii x,pii y)
{
    if(x.F.F==y.F.F)
        return x.F.S>y.F.S;
    return x.F.F < y.F.F;
}

int main()
{
    int n,m,i,x,y;
    cin>>n>>m;

    for(i=0;i<m;i++)
    {
        cin>>x>>y;
        v.pb({{x,y},i});
    }

      if(m==1)
    {
        if(n!=2)
            cout<<-1;
        else
        cout<<1<<" "<<2;

        return 0;

    }

    sort(v.begin(),v.end(),cmp);

    if(v[0].F.S!=1)
    {
        cout<<-1;
        return 0;
    }
    if(v[1].F.S!=1)
    {
        cout<<-1;
        return 0;
    }
    ans[v[0].S].F=1;
    ans[v[0].S].S=2;

    ans[v[1].S].F=1;
    ans[v[1].S].S=3;

    //int t1=2,t2=3,curr=3;

    //cout<<"*"<<endl;
     int curr=3;
    avail.insert({2,3});
    for(i=2;i<v.size();i++)
    {
        if(v[i].F.S==1)
        {
            curr++;
            ans[v[i].S].F=1;
            ans[v[i].S].S=curr;

            int x=2;
            while(avail.size()<=m-i+2 && x<curr)
            {
                avail.insert({x,curr});
                x++;
            }
        }
        else
        {
            if(avail.size()==0)
            {
                cout<<-1;
                return 0;
            }
            pi t=*(avail.begin());
            ans[v[i].S].F=t.F;
            ans[v[i].S].S=t.S;
            avail.erase(avail.begin());
        }
    }
    for(i=0;i<m;i++)
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define pb push_back
#define pi pair<int,int>
#define F first
#define S second
#define pii pair<pi,int>
vector< pii > v;
pi ans[N];

bool cmp(pii x,pii y)
{
    if(x.F.F==y.F.F)
        return x.F.S>y.F.S;
    return x.F.F < y.F.F;
}

int main()
{
    int n,m,i,x,y;
    cin>>n>>m;

    for(i=0;i<m;i++)
    {
        cin>>x>>y;
        v.pb({{x,y},i});
    }

      if(m==1)
    {

        cout<<1<<" "<<2;

        return 0;

    }

    sort(v.begin(),v.end(),cmp);

    if(v[0].F.S!=1)
    {
        cout<<-1;
        return 0;
    }
    if(v[1].F.S!=1)
    {
        cout<<-1;
        return 0;
    }
    ans[v[0].S].F=1;
    ans[v[0].S].S=2;

    ans[v[1].S].F=1;
    ans[v[1].S].S=3;

    int t1=2,t2=3,curr=3;
    for(i=2;i<v.size();i++)
    {
        if(v[i].F.S==1)
        {
            curr++;
            ans[v[i].S].F=1;
            ans[v[i].S].S=curr;
        }
        else
        {
            if(t2>curr)
            {
                t1++;
                t2=t1+1;

                if(t1>=curr)
                {
                    cout<<-1;
                    return 0;
                }
            }
            ans[v[i].S].F=t1;
            ans[v[i].S].S=t2;

            t2++;
        }
    }
    for(i=0;i<m;i++)
        cout<<ans[i].F<<" "<<ans[i].S<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[100005][2];
int b[100005][2];
vector<pair<int ,int > > v;
int main(){
	int n,m ;
	cin>>n>>m;
	int mn =1e9+7,mx = 0,cur =1;
	for(int i=0;i<m;i++){
		cin>>a[i][0]>>a[i][1];
		if(a[i][1])mx = max(a[i][0],mx);
		else mn = min(mn ,a[i][0]);
		}
	if(mx>mn){
		cout<<-1;return 0;
		}
	for(int i=0;i<m;i++){
		if(a[i][1]){b[i][0] = cur++;b[i][1] =cur;v.push_back(make_pair(a[i][0],cur-1));}
		}
	sort(v.begin(),v.end());
	int j=0;
	for(int i=0;i<m;i++){
		if(!a[i][1]){
			cur = v[j].second;
			j++;
			b[i][0] = cur;
			b[i][1] = cur+1;
			}
		}
	for(int i=0;i<m;i++){
		cout<<b[i][0]<<" "<<b[i][1]<<endl;
		}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
vector<pair<int,int> >a;
vector<pair<int,int> >b;
int edx[100005];
int edy[100005];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		if(y){
			a.pb({x,i});
		}
		else{
			b.pb({x,i});
		}
	}
	sort(a.begin(),a.end());
	reverse(a.begin(),a.end());
	sort(b.begin(),b.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<n-1;i++){
		edx[a[i].se] = i+1;
		edy[a[i].se] = i+2;
	}
	int lst = 1;
	int cur = 3;
	for(int i=0;i<(int)b.size();i++){
		if(cur>n){
			lst++;
			cur = lst+2;
		}
		if(lst==n-1){
			cout<<-1<<endl;
			return 0;
		}
		if(b[i].fi>=a[lst-1].fi){
			edx[b[i].se] = lst;
			edy[b[i].se] = cur;

		}
		else{
			lst++;
			cur = lst+2;
			i--;
		}
	}
	for(int i=0;i<m;i++){
		cout<<edx[i]<<" "<<edy[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
vector<pair<int,int> >a;
vector<pair<int,int> >b;
int edx[100005];
int edy[100005];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		if(y){
			a.pb({x,i});
		}
		else{
			b.pb({x,i});
		}
	}
	sort(a.begin(),a.end());
	reverse(a.begin(),a.end());
	sort(b.begin(),b.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<n-1;i++){
		edx[a[i].se] = i+1;
		edy[a[i].se] = i+2;
	}
	int lst = 1;
	int cur = 3;
	for(int i=0;i<(int)b.size();i++){
		if(cur>n){
			lst++;
			cur = lst+2;
		}
		if(lst==n-1){
			cout<<-1<<endl;
			return 0;
		}
		if(b[i].fi>=a[lst-1].fi){
			edx[b[i].se] = lst;
			edy[b[i].se] = cur;
			cur++;
		}
		else{
			lst++;
			cur = lst+2;
			i--;
		}
	}
	for(int i=0;i<m;i++){
		cout<<edx[i]<<" "<<edy[i]<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
vector<int>a;
vector<int>b;
vector<pair<int,int> > edg;
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		if(y){
			a.pb(x);
		}
		else{
			b.pb(x);
		}
	}
	sort(a.begin(),a.end());
	reverse(a.begin(),a.end());
	sort(b.begin(),b.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<n-1;i++){
		edg.pb({i+1,i+2});
	}
	int lst = 1;
	int cur = 3;
	for(int i=0;i<(int)b.size();i++){
		if(cur>n){
			lst++;
			cur = lst+2;
		}
		if(b[i]>=a[lst-1]){
			edg.pb({lst,cur});
		}
		else{
			cout<<-1<<endl;
			return 0;
		}
		cur++;
	}
	for(auto x:edg){
		cout<<x.fi<<" "<<x.se<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
vector<pair<int,int> >a;
vector<pair<int,int> >b;
int edx[100005];
int edy[100005];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		if(y){
			a.pb({x,i});
		}
		else{
			b.pb({x,i});
		}
	}
	sort(a.begin(),a.end());
	reverse(a.begin(),a.end());
	sort(b.begin(),b.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<n-1;i++){
		edx[a[i].se] = i+1;
		edy[a[i].se] = i+2;
	}
	int lst = 1;
	int cur = 3;
	for(int i=0;i<(int)b.size();i++){
		if(cur>n){
			lst++;
			cur = lst+2;
		}
		if(b[i].fi>=a[lst-1].fi){
			edx[b[i].se] = lst;
			edy[b[i].se] = cur;

		}
		else{
			cout<<-1<<endl;
			return 0;
		}
		cur++;
	}
	for(int i=0;i<m;i++){
		cout<<edx[i]<<" "<<edy[i]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[100005][2];
int b[100005][2];
pair<int ,int> c[100005];
bool cmp(pair<int,int>x,pair<int ,int>y){
	if(x.first<y.first)return true;
	else if(x.first>y.first)return false;
	else return (x.second > y.second);
	}
int main(){
	int n,m ;
	cin>>n>>m;
	int cur =1;
	for(int i=0;i<m;i++){
		cin>>a[i][0]>>a[i][1];
		c[i] = make_pair(a[i][0],a[i][1]);
		}
	sort(c,c+m,cmp);
	for(int i=0;i<m;i++){
		if(c[i].second)cur++;
		else {
			if(i+1>(cur)*(cur-1)/2){
				cout<<-1;return 0;
				}
			}
		}
	cur =2;
	for(int i=0;i<m;i++){
		if(a[i][1]){b[i][0] = 1;b[i][1] =cur++;}
		}
	cur =2;
	int j=1;;
	for(int i=0;i<m;i++){
		if(!a[i][1]){
			b[i][0] = cur;
			b[i][1] = cur+j;
			j++;
			if(cur+j>n){j=1;cur++;}
			}
		}
	for(int i=0;i<m;i++){
		cout<<b[i][0]<<" "<<b[i][1]<<endl;
		}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
vector<pair<int,int> >a;
vector<pair<int,int> >b;
int edx[100005];
int edy[100005];
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,m;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int x,y;
		cin>>x>>y;
		if(y){
			a.pb({x,i});
		}
		else{
			b.pb({x,i});
		}
	}
	sort(a.begin(),a.end());
	reverse(a.begin(),a.end());
	sort(b.begin(),b.end());
	reverse(b.begin(),b.end());
	for(int i=0;i<n-1;i++){
		edx[a[i].se] = i+1;
		edy[a[i].se] = i+2;
	}
	int lst = 1;
	int cur = 3;
	for(int i=0;i<(int)b.size();i++){
		if(cur>n){
			lst++;
			cur = lst+2;
		}
		if(lst>=n-1){
			cout<<-1<<endl;
			return 0;
		}
		if(b[i].fi>=a[lst-1].fi){
			edx[b[i].se] = lst;
			edy[b[i].se] = cur;
			cur++;
		}
		else{
			lst++;
			cur = lst+2;
			i--;
		}
	}
	for(int i=0;i<m;i++){
		cout<<edx[i]<<" "<<edy[i]<<endl;
	}

	return 0;
}


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

pair <pair <int, int>, int> edg[100005];
pair <int, int> ans[100005];

int main() {
	int n, m;
	cin >> n;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> edg[i].first.first;
		cin >> edg[i].first.second;
		edg[i].second = i + 1;
		edg[i].first.second *= -1;
	}
	sort(edg, edg + m);
	if (m == 1) {
		cout << 1 << " " << 2;
		return 0;
	}
	if (edg[0].first.second == 0 || edg[1].first.second == 0) {
		cout << -1;
		return 0;
	}
	ans[edg[0].second] = {1, 2};
	ans[edg[1].second] = {1, 3};
	int x = 2, y = 3, q = 3;
	for (int i = 2; i < m; i++) {
		if (edg[i].first.second == -1) {
			q++;
			ans[edg[i].second] = {1, q};
		}
		else {
			ans[edg[i].second] = {x, y};
			y++;
			if (y > q) {
				x++;
				y = x + 1;
				if (x >= q) {
					cout << -1;
					return 0;
				}
			}

		}
	}
	for (int i = 1; i <= m; i++) {
		cout << ans[i].first << " " << ans[i].second << endl;
	}
	return 0;
}


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

pair <pair <int, int>, int> edg[100005];
pair <int, int> ans[100005];

int main() {
	int n, m;
	cin >> n;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> edg[i].first.first;
		cin >> edg[i].first.second;
		edg[i].second = i + 1;
		edg[i].first.second *= -1;
	}
	sort(edg, edg + m);
	if (edg[0].first.second == 0 || edg[1].first.second == 0) {
		cout << -1;
		return 0;
	}
	ans[edg[0].second] = {1, 2};
	ans[edg[1].second] = {1, 3};
	int x = 2, y = 3, q = 3;
	for (int i = 2; i < m; i++) {
		if (edg[i].first.second == -1) {
			q++;
			ans[edg[i].second] = {1, q};
		}
		else {
			ans[edg[i].second] = {x, y};
			y++;
			if (y > q) {
				x++;
				y = x + 1;
				if (x >= q) {
					cout << -1;
					return 0;
				}
			}

		}
	}
	for (int i = 1; i <= m; i++) {
		cout << ans[i].first << " " << ans[i].second << endl;
	}
	return 0;
}


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

pair <pair <int, int>, int> edg[100005];
pair <int, int> ans[100005];

int main() {
	int n, m;
	cin >> n;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> edg[i].first.first;
		cin >> edg[i].first.second;
		edg[i].second = i + 1;
		edg[i].first.second *= -1;
	}
	sort(edg, edg + m);
	if (m == 1) {
		cout << 1 << " " << 2;
		return 0;
	}
	if (edg[0].first.second == 0 || edg[1].first.second == 0) {
		cout << -1;
		return 0;
	}
	ans[edg[0].second] = {1, 2};
	ans[edg[1].second] = {1, 3};
	int x = 2, y = 3, q = 3;
	for (int i = 2; i < m; i++) {
		if (edg[i].first.second == -1) {
			q++;
			ans[edg[i].second] = {1, q};
		}
		else {
			if (y > q) {
				x++;
				y = x + 1;
				if (x >= q) {
					cout << -1;
					return 0;
				}
			}
			ans[edg[i].second] = {x, y};
			y++;

		}
	}
	for (int i = 1; i <= m; i++) {
		cout << ans[i].first << " " << ans[i].second << endl;
	}
	return 0;
}


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

pair <pair <int, int>, int> edg[100005];
pair <int, int> ans[100005];

int main() {
	int n, m;
	cin >> n;
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> edg[i].first.first;
		cin >> edg[i].first.second;
		edg[i].second = i + 1;
		edg[i].first.second *= -1;
	}
	sort(edg, edg + m);
	if (m == 1) {
		cout << 1 << " " << 2;
		return 0;
	}
	if (edg[0].first.second == 0 || edg[1].first.second == 0) {
		cout << -1;
		return 0;
	}
	ans[edg[0].second] = {1, 2};
	ans[edg[1].second] = {1, 3};
	int x = 3, y = 2, q = 3;
	for (int i = 2; i < m; i++) {
		if (edg[i].first.second == -1) {
			q++;
			ans[edg[i].second] = {1, q};
		}
		else {
			if (y == 1) {
				x++;
				y = x - 1;
				if (x > q) {
					cout << -1;
					return 0;
				}
			}
			ans[edg[i].second] = {x, y};
			y--;

		}
	}
	for (int i = 1; i <= m; i++) {
		cout << ans[i].first << " " << ans[i].second << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
#define lcm(x, y) ((x) * (y) / __gcd((x), (y)))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " : " << x << endl
#define part cout << "----------------------------------\n";
#include <iostream>

int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; // trick to explore an implicit 2D grid
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; // S,SE,E,NE,N,NW,W,SW neighbors  //GO FOR EVEN FOR 4 moves

#define fastinput                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

int main()
{
    fastinput;
    LL n, i, j, k, t, temp, tc;
    LL m;
    vector<pair<LL, LL>> v1, v2;
    cin >> n >> m;
    LL timer = 1;
    for (i = 0; i < m; i++)
    {
        cin >> j >> k;
        //j-=900000000;
        if (k == 0)
        {
            v2.pb({j, timer++});
        }
        else
        {
            v1.pb({j, timer++});
        }
    }

    sort(all(v1));
    sort(all(v2));
    // part;
    // for (auto x : v1)
    // {
    //     cout << x.first << " " << x.second << endl;
    // }
    // part;
    // for (auto x : v2)
    // {
    //     cout << x.first << " " << x.second << endl;
    // }
    int node1 = 1;
    int node2 = 3;
    vector<pair<LL, LL>> ans;
    pair<LL, LL> id[m + 1];
    int now = 1;
    for (auto x : v1)
    {
        int num = x.second;
        id[num] = {now, now + 1};
       //debug(num);
        now++;
    }
    LL sz1=v2.size();
    for (i = 0; i < sz1; i++)
    {
        LL weight_now = v2[i].first;
        LL curr = v1[node2 - 2].first;
        // debug(weight_now);
        // debug(curr);
        // debug(node1);
        // debug(node2);
        if (weight_now < curr)
        {
            cout << -1 << endl;
            return 0;
        }
        int num = v2[i].second;
        //debug(num);
        id[num] = {node1, node2};
        if (node1 == 1)
        {
            node2++;
            node1=node2-2;
        }
        else
        {
            node1--;
        }
      //  part;
    }

    for (i = 1; i <= m; i++)
    {
        cout << id[i].first << " " << id[i].second << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
#define lcm(x, y) ((x) * (y) / __gcd((x), (y)))
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " : " << x << endl
#define part cout << "----------------------------------\n";
#include <iostream>

int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; // trick to explore an implicit 2D grid
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; // S,SE,E,NE,N,NW,W,SW neighbors  //GO FOR EVEN FOR 4 moves

#define fastinput                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

int main()
{
    fastinput;
    LL n, i, j, k, t, temp, tc;
    LL m;
    vector<pair<LL, LL>> v1, v2;
    cin >> n >> m;
    LL timer = 1;
    for (i = 0; i < m; i++)
    {
        cin >> j >> k;
        if (k == 0)
        {
            v2.pb({j, timer++});
        }
        else
        {
            v1.pb({j, timer++});
        }
    }

    sort(all(v1));
    sort(all(v2));
    // part;
    // for (auto x : v1)
    // {
    //     cout << x.first << " " << x.second << endl;
    // }
    // part;
    // for (auto x : v2)
    // {
    //     cout << x.first << " " << x.second << endl;
    // }
    int node1 = 1;
    int node2 = 3;
    vector<pair<LL, LL>> ans;
    pair<LL, LL> id[m + 1];
    int now = 1;
    for (auto x : v1)
    {
        int num = x.second;
        id[num] = {now, now + 1};
       //debug(num);
        now++;
    }
    LL sz1=v2.size();
    for (i = 0; i < sz1; i++)
    {
        LL weight_now = v2[i].first;
        LL curr = v1[node2 - 2].first;
        //debug(weight_now);
        //debug(curr);
        if (weight_now < curr)
        {
            cout << -1 << endl;
            return 0;
        }
        int num = v2[i].second;
        //debug(num);
        id[num] = {node1, node2};
        if (node2 == n)
        {
            node1++;
            node2 = node1 + 2;
        }
        else
        {
            node2++;
        }
    }

    for (i = 1; i <= m; i++)
    {
        cout << id[i].first << " " << id[i].second << endl;
    }

    return 0;
}


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);

int main()
{
    long n,m,i,j,p,r,a,b;
    cin>>n>>m;
    pair <long,long> A[m];
    vector <pair<long,long>> v0,v1;
    long B[n+1];
    for(i=0;i<m;i++)
    {
        cin>>a>>b;
        if(b==0)
            v0.push_back({a,i});
        else
            v1.push_back({a,i});
    }

    sort(v0.begin(),v0.end());
    sort(v1.begin(),v1.end());

    for(i=1;i<n;i++)
        B[i+1]=v1[i-1].first;

    long k = m-n+1;
    j=0;
    r=3;
    while(j<k)
    {
        for(i=1;i<r-1 && j<k;i++,j++)
        {
            if(v0[j].first>=B[r])
                A[v0[j].second] = {i,r};
            else
            {
                cout<<"-1"<<endl;
                goto end;
            }
        }
        r++;
    }

    for(i=0;i<n-1;i++)
        A[v1[i].second]={i+1,i+2};

    for(i=0;i<m;i++)
        cout<<A[i].first<<" "<<A[i].second<<endl;
    end:;
}


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

#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);

int main()
{
    IOS;
    int n,m,i,j,p,r,a,b;
    cin>>n>>m;
    pair <int,int> A[m];
    vector <pair<int,int>> v0,v1;
    for(i=0;i<m;i++)
    {
        cin>>a>>b;
        if(b==0)
            v0.push_back({a,i});
        else
            v1.push_back({a,i});
    }

    sort(v0.begin(),v0.end());
    sort(v1.begin(),v1.end());

    int k = m-n+1;
    i=1;j=0;
    while(i<n-1 && j<k)
    {
        if(v1[i].first>v0[j].first)
        {
            cout<<"-1"<<endl;
            goto end;
        }
        i++;
        j++;
    }

    for(i=0;i<n-1;i++)
        A[v1[i].second]={i+1,i+2};

    p=3;r=1;
    for(i=0;i<k;i++)
    {
        if(p==n+1)
        {
            r++;
            p=r+2;
        }
        A[v0[i].second]={r,p};
        p++;
    }

    for(i=0;i<m;i++)
        cout<<A[i].first<<" "<<A[i].second<<endl;
    end:;
}


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v] = {0, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      int cur = 1;

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  cur ++;
                  ans[id] = {1, cur};
                  upd(cur);
                  upd(1);
                  deg[1] ++;
                  deg[cur] ++;
            } else {
                  int fi = get(2, n).second;
                  int se = min(get(2, fi - 1), get(fi + 1, n)).second;
                  if(deg[fi] >= n - 1 || deg[se] >= n - 1 || !i){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
                  upd(se);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v] = {0, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      int cur = 1;

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  cur ++;
                  ans[id] = {1, cur};
                  upd(cur);
                  upd(1);
                  deg[1] ++;
                  deg[cur] ++;
            } else {
                  int fi = get(1, cur).second;
                  int se = min(get(1, fi - 1), get(fi + 1, cur)).second;
                  if(deg[fi] >= cur - 1 || deg[se] >= cur - 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
                  upd(se);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N], c[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            c[tl] = tl;
            t[v] = {tl, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 1e9};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

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

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end(), cmp);

      build();

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  c[1] ++;
                  ans[id] = {1, c[1]};
                  upd(1);
            } else {
                  int fi = get(2, c[1]).second, se;
                  if(fi > n){
                        puts("-1");
                        return 0;
                  }
                  se = c[fi] + 1;
                  c[fi] ++;
                  if(se > c[1] || fi == 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v] = {0, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      int cur = 1;

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  cur ++;
                  ans[id] = {1, cur};
                  upd(cur);
                  upd(1);
                  deg[1] ++;
                  deg[cur] ++;
            } else {
                  pair <int, int> aa, bb;
                  aa = get(1, cur);
                  int fi = aa.second;
                  int f = aa.first;
                  bb = min(get(1, fi - 1), get(fi + 1, cur));
                  int se = bb.second;
                  int s = bb.first;
                  if(f >= cur - 1 || s >= cur - 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
                  upd(se);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N], c[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            c[tl] = tl;
            t[v] = {tl, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  c[1] ++;
                  ans[id] = {1, c[1]};
                  upd(1);
            } else {
                  int fi = get(1, c[1]).second, se;
                  se = c[fi] + 1;
                  c[fi] ++;
                  if(se > c[1] || fi == 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}
/***
5 8
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 0
***/


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N], c[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v] = {0, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      int cur = 1;

      for(int i = 1; i <= n; i ++)
            c[i] = i;

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  cur ++;
                  ans[id] = {1, cur};
                  upd(cur);
                  upd(1);
                  deg[1] ++;
                  deg[cur] ++;
            } else {
                  int fi = get(1, cur).second, se;
                  se = c[fi] + 1;
                  c[fi] ++;
                  if(deg[fi] >= cur - 1 || deg[se] >= cur - 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
                  upd(se);
                  deg[fi] ++;
                  deg[se] ++;
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}
/***
5 8
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 0
***/


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v] = {0, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      int cur = 1;

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  cur ++;
                  ans[id] = {1, cur};
                  upd(cur);
                  upd(1);
                  deg[1] ++;
                  deg[cur] ++;
            } else {
                  int fi = get(1, cur).second;
                  int se = min(get(1, fi - 1), get(fi + 1, cur)).second;
                  if(deg[fi] >= cur - 1 || deg[se] >= cur - 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
                  upd(se);
                  deg[fi] ++;
                  deg[se] ++;
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N], c[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            c[tl] = tl;
            t[v] = {tl, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end());

      build();

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  c[1] ++;
                  ans[id] = {1, c[1]};
                  upd(1);
            } else {
                  int fi = get(1, c[1]).second, se;
                  se = c[fi] + 1;
                  c[fi] ++;
                  if(se >= c[1]){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}
/***
5 8
1 1
2 1
3 1
4 1
5 0
6 0
7 0
8 0
***/


# include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 2;

int n, m, a[N], b[N], deg[N], c[N];
vector < pair <int, int> > vec;
pair <int, int> ans[N], t[N * 4];

void build(int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            c[tl] = tl;
            t[v] = {tl, tl};
      } else {
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

void upd(int pos, int v = 1, int tl = 1, int tr = n){
      if(tl == tr){
            t[v].first ++;
      } else {
            int tm = (tl + tr) >> 1;
            if(pos <= tm)
                  upd(pos, v << 1, tl, tm);
            else
                  upd(pos, v << 1 | 1, tm + 1, tr);
            t[v] = min(t[v << 1], t[v << 1 | 1]);
      }
}

pair <int, int> get(int l, int r, int v = 1, int tl = 1, int tr = n){
      if(l > tr || tl > r || l > r)
            return {1e9, 0};
      if(l <= tl && tr <= r)
            return t[v];
      int tm = (tl + tr) >> 1;
      return min(get(l, r, v << 1, tl, tm),
                        get(l, r, v << 1 | 1, tm + 1, tr));
}

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

int main(){
      cin >> n >> m;

      for(int i = 1; i <= m; i ++){
            cin >> a[i] >> b[i];
            vec.push_back({a[i], i});
      }

      sort(vec.begin(), vec.end(), cmp);

      build();

      for(int i = 0; i < vec.size(); i ++){
            int id = vec[i].second;
            if(b[id]){
                  c[1] ++;
                  ans[id] = {1, c[1]};
                  upd(1);
            } else {
                  int fi = get(2, c[1]).second, se;
                  se = c[fi] + 1;
                  c[fi] ++;
                  if(se > c[1] || fi == 1){
                        puts("-1");
                        return 0;
                  }
                  ans[id] = {fi, se};
                  upd(fi);
            }
      }

      for(int i = 1; i <= m; i ++)
            cout << ans[i].first << " " << ans[i].second << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "\n\n" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

const ll N=100005;
pair<pll,ll> edges[N];
pll ans[N];

inline bool cmp(pair<pll,ll> a, pair<pll,ll> b)
{
    if(a.ff.ff==b.ff.ff)
        return a.ff.ss>b.ff.ss;
    return a.ff.ff<b.ff.ff;

}

int main()
{
    fastio;
    #ifdef APNA_IO
        start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif

    ll n,m;cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll x,y;cin>>x>>y;
        edges[i]={{x,y},i};
    }
    sort(edges,edges+m,cmp);

    ll ch1=0,ch2=0;
    for(ll i=0;i<m;i++)
    {
        if(ch2>((ch1*(ch1-1))/2))
            return cout<<-1,0;
        if(edges[i].ff.ss)
            ch1++;
        else
            ch2++;
    }

    ch1++;
    ll cnt=0;
    vpl v;
    for(ll i=3;i<=ch1;i++)
    {
        if(cnt==ch2)
            break;
        for(ll j=2;j<i;j++)
        {
            if(cnt==ch2)
                break;
            v.pb({j,i});
            cnt++;
        }
    }
    reverse(v.begin(), v.end());
    for(auto i:v)
        cout<<i.ff<<" "<<i.ss<<endl;
    cout<<endl;

    cnt=2;
    for(ll i=0;i<m;i++)
    {
        if(edges[i].ff.ss)
            ans[edges[i].ss]={1,cnt},cnt++;
        else
            ans[edges[i].ss]=v.back(),v.pop_back();
    }

    for(ll i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    #ifdef APNA_IO
        end_routine();
    #endif
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "\n\n" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

const ll N=100005;
pair<pll,ll> edges[N];
pll ans[N];

inline bool cmp(pair<pll,ll> a, pair<pll,ll> b)
{
    if(a.ff.ff==b.ff.ff)
        return a.ff.ss>b.ff.ss;
    return a.ff.ff<b.ff.ff;

}

int main()
{
    fastio;
    #ifdef APNA_IO
        start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif

    ll n,m;cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll x,y;cin>>x>>y;
        edges[i]={{x,y},i};
    }
    sort(edges,edges+m,cmp);

    ll ch1=0,ch2=0;
    ll l=2,r=1;
    for(ll i=0;i<m;i++)
    {
        if(ch2>((ch1*(ch1-1))/2))
            return cout<<-1,0;
        if(edges[i].ff.ss)
        {
            ch1++,l=1,r++;
            ans[edges[i].ss]={1,r};
        }
        else
        {
            ch2++,l++;
            ans[edges[i].ss]={l,r};
        }
    }

    for(ll i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    #ifdef APNA_IO
        end_routine();
    #endif
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "\n\n" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

const ll N=100005;
pair<pll,ll> edges[N];
pll ans[N];

inline bool cmp(pair<pll,ll> a, pair<pll,ll> b)
{
    if(a.ff.ff==b.ff.ff)
        return a.ff.ss>b.ff.ss;
    return a.ff.ff<b.ff.ff;

}

int main()
{
    fastio;
    #ifdef APNA_IO
        start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif

    ll n,m;cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll x,y;cin>>x>>y;
        edges[i]={{x,y},i};
    }
    sort(edges,edges+m,cmp);

    ll ch1=0,ch2=0;
    for(ll i=0;i<m;i++)
    {
        if(ch2>((ch1*(ch1-1))/2))
            return cout<<-1,0;
        if(edges[i].ff.ss)
            ch1++;
        else
            ch2++;
    }

    ch1++;
    ll cnt=0;
    vpl v;
    for(ll i=3;i<=ch1;i++)
    {
        if(cnt==ch2)
            break;
        for(ll j=2;j<i;j++)
        {
            if(cnt==ch2)
                break;
            v.pb({j,i});
            cnt++;
        }
    }
    reverse(v.begin(), v.end());
    // for(auto i:v)
    //     cout<<i.ff<<" "<<i.ss<<endl;
    // cout<<endl;

    cnt=2;
    for(ll i=0;i<m;i++)
    {
        if(edges[i].ff.ss)
            ans[edges[i].ss]={1,cnt},cnt++;
        else
            ans[edges[i].ss]=v.back(),v.pop_back();
    }

    for(ll i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    #ifdef APNA_IO
        end_routine();
    #endif
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "\n\n" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

const ll N=100005;
pair<pll,ll> edges[N];
pll ans[N];

int main()
{
    fastio;
    #ifdef APNA_IO
        start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif

    ll n,m;cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll x,y;cin>>x>>y;
        edges[i]={{x,y},i};
    }
    sort(edges,edges+m);

    ll ch1=0,ch2=0;
    for(ll i=0;i<m;i++)
    {
        if(ch2>((ch1*(ch1-1))/2))
            return cout<<-1,0;
        if(edges[i].ff.ss)
            ch1++;
        else
            ch2++;
    }

    ll l=2,r=1,curr=3;
    for(ll i=0;i<m;i++)
    {
        if(edges[i].ff.ss)
        {
            r++;
            ans[edges[i].ss]={1,r};
        }
        else
        {
            if(curr>r)
                l++,curr=l+1;
            ans[edges[i].ss]={l,curr};
            curr++;
        }
    }
    for(ll i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    #ifdef APNA_IO
        end_routine();
    #endif
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "\n\n" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

const ll N=100005;
pair<pll,ll> edges[N];
pll ans[N];

inline bool cmp(pair<pll,ll> a, pair<pll,ll> b)
{
    if(a.ff.ff==b.ff.ff)
        return a.ff.ss>b.ff.ss;
    return a.ff.ff<b.ff.ff;

}

int main()
{
    fastio;
    #ifdef APNA_IO
        start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif

    ll n,m;cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll x,y;cin>>x>>y;
        edges[i]={{x,y},i};
    }
    sort(edges,edges+m,cmp);

    ll ch1=0,ch2=0;
    for(ll i=0;i<m;i++)
    {
        if(ch2>((ch1*(ch1-1))/2))
            return cout<<-1,0;
        if(edges[i].ff.ss)
            ch1++;
        else
            ch2++;
    }

    ll l=2,r=1,curr=3;
    for(ll i=0;i<m;i++)
    {
        if(edges[i].ff.ss)
        {
            r++;
            ans[edges[i].ss]={1,r};
        }
        else
        {
            if(curr>r)
                l++,curr=l+1;
            ans[edges[i].ss]={l,curr};
            curr++;
        }
    }
    for(ll i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    #ifdef APNA_IO
        end_routine();
    #endif
}


#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
                      cerr << "\n\n" << "Time elapsed: " << \
                      (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
                      return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

const ll N=100005;
pair<pll,ll> edges[N];
pll ans[N];

inline bool cmp(pair<pll,ll> a, pair<pll,ll> b)
{
    if(a.ff.ff==b.ff.ff)
        return a.ff.ss>b.ff.ss;
    return a.ff.ff<b.ff.ff;

}

int main()
{
    fastio;
    #ifdef APNA_IO
        start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif

    ll n,m;cin>>n>>m;
    for(ll i=0;i<m;i++)
    {
        ll x,y;cin>>x>>y;
        edges[i]={{x,y},i};
    }
    sort(edges,edges+m,cmp);

    ll ch1=0,ch2=0;
    for(ll i=0;i<m;i++)
    {
        if(ch2>((ch1*(ch1-1))/2))
            return cout<<-1,0;
        if(edges[i].ff.ss)
            ch1++;
        else
            ch2++;
    }

    ll cnt=0;
    vpl v;
    for(ll i=3;i<=ch1;i++)
    {
        if(cnt==ch2)
            break;
        for(ll j=2;j<i;j++)
        {
            if(cnt==ch2)
                break;
            v.pb({j,i});
            cnt++;
        }
    }
    reverse(v.begin(), v.end());

    cnt=2;
    for(ll i=0;i<m;i++)
    {
        if(edges[i].ff.ss)
            ans[edges[i].ss]={1,cnt},cnt++;
        else
            ans[edges[i].ss]=v.back(),v.pop_back();
    }

    for(ll i=0;i<m;i++)
        cout<<ans[i].ff<<" "<<ans[i].ss<<endl;

    #ifdef APNA_IO
        end_routine();
    #endif
}


//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
/*
 *STL headers
 */
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <string>

using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

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

	int n, r, c;
	int m;
	cin>>n>>m;
	int i = 0;
	vector<tuple<int, int, int>> v(m);
	while(i < m){
		cin>>r>>c;
		v[i] = make_tuple(r, -c, i);
		i++;
	}

	sort(v.begin(), v.end());

	vector<tuple<int, int, int>> connected;
	vector<int> loop_indices;
	LLI loops = 0, node = 1;
	for(int i = 0; i < m; ++i){
		// cout<<get<0>(v[i])<<" "<<get<1>(v[i]);PN;
		if(get<1>(v[i]) == -1){
			connected.push_back(make_tuple(get<2>(v[i]) + 1, (int)(node), (int)node + 1));
			node++;
		}
		else{
			loops++;
			if(((node - 2)*(node  - 1)) / 2 < loops){
				cout<<-1;
				return 0;
			}
			loop_indices.push_back(get<2>(v[i]) + 1);
		}
	}
	// cout<<loop_indices.size();PN;
	int ind = 0;
	for(int i = 2; i <= n - 1; ++i){
		for(int j = 1; j + i <= n; ++j){
			if(ind == (int)loop_indices.size())
				break;
			connected.push_back(make_tuple(loop_indices[ind], j, j+i));
			ind++;
		}
		if(ind == (int)loop_indices.size())
				break;
	}
	// PN;PN;
	sort(connected.begin(), connected.end());
	for(auto e: connected){
		cout<<get<1>(e)<<" "<<get<2>(e);PN;
	}

	return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
/*
 *STL headers
 */
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <string>

using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

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

	int n, r, c;
	int m;
	cin>>n>>m;
	int i = 0;
	vector<tuple<int, int, int>> v(m);
	while(i < m){
		cin>>r>>c;
		v[i] = make_tuple(r, -c, i);
		i++;
	}

	sort(v.begin(), v.end());

	vector<tuple<int, int, int>> connected;
	vector<int> isolated_vertices(n+1, 1);
	isolated_vertices[1] = 0;
	int from = 2, to = 3, node = 1;
	for(int i = 0; i < m; ++i){
		// cout<<get<0>(v[i])<<" "<<get<1>(v[i]);PN;
		if(get<1>(v[i]) == -1){
			connected.push_back(make_tuple(get<2>(v[i]) + 1, 1, (int)node + 1));
			node++;
			isolated_vertices[node] = 0;
		}
		else{
			if(isolated_vertices[to] == 1){
				cout<<-1;
				return 0;
			}
			connected.push_back(make_tuple(get<2>(v[i]) + 1, from, to));
			from++;
			if(from == to){
				to++;
				from = 2;
			}
		}
	}

	sort(connected.begin(), connected.end());
	for(auto e: connected){
		cout<<get<1>(e)<<" "<<get<2>(e);PN;
	}

	return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
/*
 *STL headers
 */
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <string>

using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

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

	int n, r, c;
	int m;
	cin>>n>>m;
	int i = 0;
	vector<tuple<int, int, int>> v(m);
	while(i < m){
		cin>>r>>c;
		v[i] = make_tuple(r, -c, i);
		i++;
	}

	sort(v.begin(), v.end());

	vector<tuple<int, int, int>> connected;
	vector<int> loop_indices;
	LLI loops = 0, node = 1;
	for(int i = 0; i < m; ++i){
		if(get<1>(v[i]) == -1){
			connected.push_back(make_tuple(get<2>(v[i]) + 1, (int)(node), (int)node + 1));
			node++;
		}
		else{
			loops++;
			if(((node - 2)*(node  - 1)) / 2 < loops){
				cout<<-1;
				return 0;
			}
			loop_indices.push_back(get<2>(v[i]) + 1);
		}
	}
	// cout<<loop_indices.size();PN;
	int ind = 0;
	for(int i = 1; i <= n; ++i){
		for(int j = i+2; j <= n; ++j){
			connected.push_back(make_tuple(loop_indices[ind], i, j));
			ind++;
			if(ind == (int)loop_indices.size())
				break;
		}
		if(ind == (int)loop_indices.size())
				break;
	}
	sort(connected.begin(), connected.end());
	for(auto e: connected){
		cout<<get<1>(e)<<" "<<get<2>(e);PN;
	}

	return 0;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
/*
 *STL headers
 */
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <string>

using namespace std;

#define LLI				long long int
#define PN 				cout<<'\n'

#define SZ(a) 			int((a).size())
#define PB 				push_back
#define ALL(c) 			(c).begin(),(c).end()
#define FORITER(i,c)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define CFIND(x,c) 		((c).find(x) != (c).end())
#define FIND(x,c) 		(find(ALL(c),x) != (c).end())

#define PI 			    3.1415926535
#define MOD				1000000007

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

	int n, r, c;
	int m;
	cin>>n>>m;
	int i = 0;
	vector<tuple<int, int, int>> v(m);
	while(i < m){
		cin>>r>>c;
		v[i] = make_tuple(r, -c, i);
		i++;
	}

	sort(v.begin(), v.end());

	vector<tuple<int, int, int>> connected;
	vector<int> isolated_vertices(n+1, 1);
	isolated_vertices[1] = 0;
	int from = 2, to = 3, node = 1;
	for(int i = 0; i < m; ++i){
		// cout<<get<0>(v[i])<<" "<<get<1>(v[i]);PN;
		if(get<1>(v[i]) == -1){
			connected.push_back(make_tuple(get<2>(v[i]) + 1, 1, (int)node + 1));
			node++;
			isolated_vertices[node] = 0;
		}
		else{
			if(isolated_vertices[to] == 1){
				cout<<-1;
				return 0;
			}
			connected.push_back(make_tuple(get<2>(v[i]) + 1, from, to));
			from++; // beautiful example of two pointers

			if(from == to){
				to++;
				from = 2;
			}
		}
	}

	sort(connected.begin(), connected.end());
	for(auto e: connected){
		cout<<get<1>(e)<<" "<<get<2>(e);PN;
	}

	return 0;
}