#include<iostream>
#define N 1005
using namespace std;
bool flag[N];
int main()
{
    int n, m, i, j, start,end;
    while ((scanf("%d %d",&n,&m))!=EOF)
    {
        memset(flag,false,sizeof(flag));
        for (i=1;i<=m;i++)
        {
            scanf("%d %d",&start,&end);
            flag[start]=true;
            flag[end]=true;
        }
        for (i=1;i<=n;i++)
        {
            if (!flag[i])
            {
                j=i;
                break;
            }
        }
        printf("%d\n",n-1);
        for (i=1;i<=n;i++)
        {
            if (i!=j)
            {
                printf("%d %d\n",i,j);
            }
        }
    }
    return 0;
}


#include<iostream>
#define N 1005
using namespace std;
bool flag[N];
int main()
{
    int n, m, i, j, start,end;
    while ((scanf("%d %d",&n,&m))!=EOF)
    {
        memset(flag,false,sizeof(flag));
        for (i=1;i<=m;i++)
        {
            scanf("%d %d",&start,&end);
            flag[start]=true;
            flag[end]=true;
        }
        for (i=1;i<=n;i++)
        {
            if (!flag[i])
            {
                j=i;
                break;
            }
        }
        for (i=1;i<=n;i++)
        {
            if (i!=j)
            {
                printf("%d %d\n",i,j);
            }
        }
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
#define printll(x)  printf("%lld", x);
#define scand(x) scanf("%lf", &x);
#define printd(x) printf("%f", x);
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }
int main()
{
    ll n, m, center=-1, k=0; scl(n); scl(m);
    vector<pair<ll, ll>> noEdge(m), out(n - 1);
    lop(i, m)
    {
        scanf("%lld", &noEdge[i].first); scanf("%lld", &noEdge[i].second);
    }
    lop(i, n)
    {
        if (center != -1)
            break;
        lop(j, m)
        {
            if (noEdge[j].first == i + 1 || noEdge[j].second == i + 1)
                break;
            else if (j == m - 1)
                center = i+1;
        }
    }
    printf("%lld\n", n - 1);
    lop(i, n)
    {
        if (i != center - 1)
            printf("%lld %lld\n", i + 1, center);

    }
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
#define printll(x)  printf("%lld", x);
#define scand(x) scanf("%lf", &x);
#define printd(x) printf("%f", x);
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }
int main()
{
    ll n, m, center=-1, k=0; scl(n); scl(m);
    vector<pair<ll, ll>>  out(n - 1);
    vector<bool> check(n, true);
    lop(i, m)
    {
        ll x, y;    scanf("%lld", &x); scanf("%lld", &y);
        check[x - 1] = false; check[y - 1] = false;
    }
    lop(i, n)
        if (check[i])
        {
        center = i + 1; break;
        }
    printf("%lld\n", n - 1);
    lop(i, n)
    {
        if (i != center - 1)
            printf("%lld %lld\n", i + 1, center);

    }
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
#define printll(x)  printf("%lld", x);
#define scand(x) scanf("%lf", &x);
#define printd(x) printf("%f", x);
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }
int main()
{
    ll n, m; scl(n); scl(m);
    vector<pair<ll, ll>> noEdge(m), out(n - 1);
    lop(i, m)
    {
        scanf("%lld", &noEdge[i].first); scanf("%lld", &noEdge[i].second);
    }
    sort(all(noEdge));
    loop(i,1, n+1)
    {
        loop(j, i+1, n+1)
        {
            if (j == i)
                continue;
            if (!binary_search(all(noEdge), make_pair(i, j)) && !binary_search(all(noEdge), make_pair(j, i)))
            {
                out[i-1] = make_pair(i, j); break;
            }
        }
    }
    printf("%lld\n", n - 1);
    lop(i, n - 1)
        printf("%lld %lld\n", out[i].first, out[i].second);
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
#define printll(x)  printf("%lld", x);
#define scand(x) scanf("%lf", &x);
#define printd(x) printf("%f", x);
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }
int main()
{
	ll n, m, k=0; scl(n); scl(m);
	vector<pair<ll, ll>> noEdge(m), out(n - 1);
	lop(i, m)
	{
		scanf("%lld", &noEdge[i].first); scanf("%lld", &noEdge[i].second);
	}
	sort(all(noEdge));
	vector<bool> b(n, false);
	loop(i,1, n+1)
	{
		b[i-1] = true;
		loop(j, i+1, n+1)
		{
			if (b[j - 1])
				continue;
			if (!binary_search(all(noEdge), make_pair(i, j)) && !binary_search(all(noEdge), make_pair(j, i)))
			{
				out[k] = make_pair(i, j);
				b[j - 1] = true;
				++k;
			}
		}
	}
	printf("%lld\n", n - 1);
	lop(i, n - 1)
		printf("%lld %lld\n", out[i].first, out[i].second);
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<queue>
#include<stack>
#include<iterator>
#include<cmath>
#include<string>
#include<sstream>
#include<cstring>
#include<ctype.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<math.h>
#include<numeric>
using namespace std;

#define all(v)         v.begin(),v.end()
#define sz(V)        ((ll)((v).size()))
#define pp(x)               push_back(x)
#define ck(a)               (a<1 || a>9)
#define lop(i,n)    for(ll i=0;i<n;i++)
#define loop(i,f,l) for(ll i=f;i<l;i++)
#define READ(s)   freopen(s, "r", stdin)
#define WRITE(s) freopen(s, "w", stdout)
#define scl(n)          scanf("%lld",&n)
#define sc(n)             scanf("%d",&n)
#define INF                   1000000000
#define PI             3.141592653589793
#define print(v)      lop(i, v.size()) cout<<v[i]<<" ";
#define read(v)   lop(i, v.size()) cin>>v[i];
#define printll(x)  printf("%lld", x);
#define scand(x) scanf("%lf", &x);
#define printd(x) printf("%f", x);
typedef string            ss;
typedef long long         ll;
typedef pair <ll, ll>   ii;
typedef pair<string, ll> si;
typedef pair<ll, string> is;
typedef pair<char, ll>  chi;
typedef vector<ii>       vii;
typedef vector<ll>       vi;
typedef vector<vi>       vvi;
typedef vector<string>    vs;
typedef vector<ll>       vll;
//READ("input.txt");WRITE("output.txt");
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b){ return ((a*b) / gcd(a, b)); }
ll pw(ll b, ll p){ if (!p) return 1; ll sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; }
ll sd(ll x){ return x<10 ? x : x % 10 + sd(x / 10); }
double sq(double x){ lop(i, x)if ((double)i*i > x) return (i - 1); return double(1); }
int main()
{
    ll n, m, center=-1, k=0; scl(n); scl(m);
    vector<pair<ll, ll>> noEdge(m), out(n - 1);
    lop(i, m)
    {
        scanf("%lld", &noEdge[i].first); scanf("%lld", &noEdge[i].second);
    }
    lop(i, n)
    {
        if (center != -1)
            break;
        if (m == 0)
        {
            center = 1; break;
        }
        lop(j, m)
        {
            if (noEdge[j].first == i + 1 || noEdge[j].second == i + 1)
                break;
            else if (j == m - 1)
                center = i+1;
        }

    }
    printf("%lld\n", n - 1);
    lop(i, n)
    {
        if (i != center - 1)
            printf("%lld %lld\n", i + 1, center);

    }
}


#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen ("input.txt","r",stdin);
        //freopen ("output.txt","w",stdout);
    #endif

    int n,m,a[500],b[500];
    int s=0;
    cin >> n >> m;

    for(int i=0;i<m;i++){
        cin >> a[i] >> b[i];
    }
    int mas[100][2]={0};
    int c=1;bool got=false;
    for(int i=1; i<n+1;i++){
        for(int j=0; j<m;j++){
            if(a[j]==c || b[j]==c) {
                got=true;// ????
                c++;
                break;
            }
        }
        if(got==false) {/*c=i;*/got=false; break;}
    }
    int pos=0;
    for(int i=1; i<n+1;i++){
        if(c!=i) {
            mas[pos][0]=c;mas[pos][1]=i;
            s++;pos++;
        }
    }
    cout << s << endl;
    for(int i=0;i<s;i++){
        cout << mas[i][0] << " " << mas[i][1] << endl;
    }

    return 0;
}


#include <cstdio>
#include <iostream>

using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen ("input.txt","r",stdin);
        //freopen ("output.txt","w",stdout);
    #endif

    int n,m,a[500],b[500];
    int s=0;
    cin >> n >> m;

    for(int i=0;i<m;i++){
        cin >> a[i] >> b[i];
    }
    int mas[1000][2]={0};
    int c=1;bool got=false;
    for(int i=1; i<n+1;i++){
        for(int j=0; j<m;j++){
            if(a[j]==c || b[j]==c) {
                got=true;// ????
                c++;
                break;
            }
        }
        if(got==false) {/*c=i;*/got=false; break;}
    }
    int pos=0;
    for(int i=1; i<n+1;i++){
        if(c!=i) {
            mas[pos][0]=c;mas[pos][1]=i;
            s++;pos++;
        }
    }
    cout << s << endl;
    for(int i=0;i<s;i++){
        cout << mas[i][0] << " " << mas[i][1] << endl;
    }

    return 0;
}


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>

using namespace std;

bool alloved(int city1, int city2, vector<int> *restr);

int main()
{
    bool find = false;
    int n, m, tmp1, tmp2;
    n = m = tmp1 = tmp2 = 0;
    cin >> n >> m;

    vector<int> *restr = new vector<int>[n+1];
    vector<int> *roads = new vector<int>[n+1];
    queue<int> city;

    for (int i = 1; i < n; i++)
    {
        city.push(i+1);
    }

    for(int i = 0; i < m; i++)
    {
        cin >> tmp1 >> tmp2;
        restr[tmp1].push_back(tmp2);
        restr[tmp2].push_back(tmp1);
    }

    while(!city.empty())
    {
        find = false;
        tmp1 = city.front();

        if(alloved(1, tmp1, restr))
        {
            roads[1].push_back(tmp1);
            city.pop();
            find = true;
        }
        else
        {
            if(roads[1].empty()) continue;
            for(vector<int>::iterator it = roads[1].begin(); it != roads[1].end(); it++)
            {
                if(alloved(*it, tmp1, restr))
                {
                    roads[*it].push_back(tmp1);
                    city.pop();
                    find = true;
                }
            }
        }
        if(!find)
        {
            city.pop();
            city.push(tmp1);
        }
    }

    for(int i = 1; i < n+2; i++)
    {
        if(roads[i].empty()) continue;
        for(vector<int>::iterator it = roads[i].begin(); it != roads[i].end(); it++)
        {
            cout << i << " " << *it << endl;
        }
    }

    return 0;
}

bool alloved(int city1, int city2, vector<int> *restr)
{

    if(restr[city1].empty())
    {
        return true;
    }
    else
    {
        for(vector<int>::iterator it = restr[city1].begin(); it != restr[city1].end(); it++)
        {
            if(*it == city2) return false;
        }
    }

    return true;
}


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>

using namespace std;

bool alloved(int city1, int city2, vector<int> *restr);

int main()
{
    bool find = false;
    int n, m, tmp1, tmp2;
    n = m = tmp1 = tmp2 = 0;
    cin >> n >> m;

    vector<int> *restr = new vector<int>[n+1];
    vector<int> *roads = new vector<int>[n+1];
    queue<int> city;

    for (int i = 1; i < n; i++)
    {
        city.push(i+1);
    }

    for(int i = 0; i < m; i++)
    {
        cin >> tmp1 >> tmp2;
        restr[tmp1].push_back(tmp2);
        restr[tmp2].push_back(tmp1);
    }

    while(!city.empty())
    {
        find = false;
        tmp1 = city.front();

        if(alloved(1, tmp1, restr))
        {
            roads[1].push_back(tmp1);
            city.pop();
            find = true;
        }
        else
        {
            if(roads[1].empty()) continue;
            for(vector<int>::iterator it = roads[1].begin(); it != roads[1].end(); it++)
            {
                if(alloved(*it, tmp1, restr))
                {
                    roads[*it].push_back(tmp1);
                    city.pop();
                    find = true;
                }
            }
        }
        if(!find)
        {
            city.pop();
            city.push(tmp1);
        }
    }

    cout << n-1 << endl;
    for(int i = 1; i < n+2; i++)
    {
        if(roads[i].empty()) continue;
        for(vector<int>::iterator it = roads[i].begin(); it != roads[i].end(); it++)
        {
            cout << i << " " << *it << endl;
        }
    }

    return 0;
}

bool alloved(int city1, int city2, vector<int> *restr)
{

    if(restr[city1].empty())
    {
        return true;
    }
    else
    {
        for(vector<int>::iterator it = restr[city1].begin(); it != restr[city1].end(); it++)
        {
            if(*it == city2) return false;
        }
    }

    return true;
}


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>

using namespace std;

bool alloved(int city1, int city2, vector<int> *restr);

int main()
{
    bool find = false;
    int n, m, tmp1, tmp2;
    n = m = tmp1 = tmp2 = 0;
    cin >> n >> m;

    vector<int> *restr = new vector<int>[n+1];
    vector<int> *roads = new vector<int>[n+1];
    queue<int> city;

    for (int i = 1; i < n; i++)
    {
        city.push(i+1);
    }

    for(int i = 0; i < m; i++)
    {
        cin >> tmp1 >> tmp2;
        restr[tmp1].push_back(tmp2);
        restr[tmp2].push_back(tmp1);
    }

    while(!city.empty())
    {
        find = false;
        tmp1 = city.front();

        if(alloved(1, tmp1, restr))
        {
            roads[1].push_back(tmp1);
            city.pop();
            find = true;
        }
        else
        {
            if(roads[1].empty()) continue;
            for(vector<int>::iterator it = roads[1].begin(); it != roads[1].end(); it++)
            {
                if(alloved(*it, tmp1, restr))
                {
                    roads[*it].push_back(tmp1);
                    city.pop();
                    find = true;
                }
            }
        }
        if(!find)
        {
            city.pop();
            city.push(tmp1);
        }
    }

    cout < n-1;
    for(int i = 1; i < n+2; i++)
    {
        if(roads[i].empty()) continue;
        for(vector<int>::iterator it = roads[i].begin(); it != roads[i].end(); it++)
        {
            cout << i << " " << *it << endl;
        }
    }

    return 0;
}

bool alloved(int city1, int city2, vector<int> *restr)
{

    if(restr[city1].empty())
    {
        return true;
    }
    else
    {
        for(vector<int>::iterator it = restr[city1].begin(); it != restr[city1].end(); it++)
        {
            if(*it == city2) return false;
        }
    }

    return true;
}


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>

using namespace std;

int main()
{
    int n, m, tmp1, tmp2, notr;
    n = m = tmp1 = tmp2 = notr = 0;
    cin >> n >> m;

    vector<int> *restr = new vector<int>[n+1];

    for(int i = 0; i < m; i++)
    {
        cin >> tmp1 >> tmp2;
        restr[tmp1].push_back(tmp2);
        restr[tmp2].push_back(tmp1);
    }

    for(int i = 1; i < n+1; i++)
    {
        if(restr[i].empty())
        {
            notr = i;
            break;
        }
    }

    cout << n-1 << endl;
    for(int i = 1; i < n+1; i++)
    {
        if(i == notr) continue;
        cout << notr << " " << i << endl;
    }

    return 0;
}


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>

using namespace std;

bool alloved(int city1, int city2, vector<int> *restr);

int main()
{
    bool find = false;
    int n, m, tmp1, tmp2;
    n = m = tmp1 = tmp2 = 0;
    cin >> n >> m;

    vector<int> *restr = new vector<int>[n+1];
    vector<int> *roads = new vector<int>[n+1];
    queue<int> city;

    for (int i = 1; i < n; i++)
    {
        city.push(i+1);
    }

    for(int i = 0; i < m; i++)
    {
        cin >> tmp1 >> tmp2;
        restr[tmp1].push_back(tmp2);
        restr[tmp2].push_back(tmp1);
    }

    while(!city.empty())
    {
        find = false;
        tmp1 = city.front();

        if(alloved(1, tmp1, restr))
        {
            roads[1].push_back(tmp1);
            city.pop();
            find = true;
        }
        else
        {
            if(roads[1].empty()) continue;
            for(vector<int>::iterator it = roads[1].begin(); it != roads[1].end(); it++)
            {
                if(alloved(*it, tmp1, restr))
                {
                    roads[*it].push_back(tmp1);
                    city.pop();
                    find = true;
                }
            }
        }
        if(!find)
        {
            city.pop();
            city.push(tmp1);
        }
    }

    cout << n-1;
    for(int i = 1; i < n+2; i++)
    {
        if(roads[i].empty()) continue;
        for(vector<int>::iterator it = roads[i].begin(); it != roads[i].end(); it++)
        {
            cout << i << " " << *it << endl;
        }
    }

    return 0;
}

bool alloved(int city1, int city2, vector<int> *restr)
{

    if(restr[city1].empty())
    {
        return true;
    }
    else
    {
        for(vector<int>::iterator it = restr[city1].begin(); it != restr[city1].end(); it++)
        {
            if(*it == city2) return false;
        }
    }

    return true;
}


//#include<iostream>
//#include<cmath>
//#define LL long long
//#define slen(a) strlen(a)
//#define mem(a) memset(a, 0, sizeof (a))
//#define For(i, n) for (i = 0 i < n; ++i)
//#define dou double
//using namespace std;
//
//LL solve(LL n)
//{
//      LL re = 0, i, j, k, a, b, c, t1 = (LL) pow((dou) n, 1.0 / 3.0), t2 = pow((dou) n, 0.5);
//      re += t1;
//      for (i = 1; i <= t1; ++i)
//      {
//              if (n > ( i * i * i))
//                      re += 3 * (n / (i * i) - i);
//      }
//      for (i = t2; i >= t1; --i)
//              re += 3 * (n / (i * i));
//      for (i = 1; i <= t1; ++i)
//      {
//              LL tem = (LL) sqrt((dou) n / i);
//              for (j = i + 1; j <= tem; ++j)
//                      if (n > i * j * j)
//                              re += 6;
//      }
//      return re;
//}
//
//int main()
//{
//      LL n, ca = 1;
//
//      while (cin >> n)
//              cout << "Case " << ca++ << ": " << solve(n) << endl;
//
//      return 0;
//}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
#include<string>
#include<memory>
#include<map>
#include<sstream>
#define mem(a) memset(a, 0, sizeof(a))
typedef long long LL;
typedef double dou;
const int Mod = 1000000007;
const double eps = 1e-8;
const LL inf = 1e18;
const int inf1 = 1e9;
using namespace std;
int a[1005];

int main()
{
        LL n, m, fa = 0, num = 0, i, x, y;
        cin >> n >> m;
        for (i = 0; i < m; ++i)
        {
                cin >> x >> y;
                a[x] = a[y] = 1;
        }
        for (i = 1; i <= n; ++i)
                if (a[i] == 0)
                {
                        fa = i;
                        break;
                }
        cout << n - 1 << endl;
        for (i = 0; i < num; ++i)
        {
                if (i != fa)
                         cout << fa << ' ' << i << endl;
        }

        return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
#include<string>
#include<memory>
#include<map>
#include<sstream>
#define mem(a) memset(a, 0, sizeof(a))
typedef long long LL;
typedef double dou;
const int Mod = 1000000007;
const double eps = 1e-8;
const LL inf = 1e18;
const int inf1 = 1e9;
using namespace std;
int a[1005];

int main()
{
        LL n, m, fa = 0, num = 0, i, x, y;
        cin >> n >> m;
        for (i = 0; i < m; ++i)
        {
                cin >> x >> y;
                a[x] = a[y] = 1;
        }
        for (i = 1; i <= n; ++i)
                if (a[i] == 0)
                {
                        fa = i;
                        break;
                }
        cout << n - 1 << endl;
        for (i = 0; i < n; ++i)
        {
                if (i + 1 != fa)
                         cout << fa << ' ' << i + 1 << endl;
        }

        return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<stack>
#include<deque>
#include<set>
#include<vector>
#include<iomanip>
#include<cctype>
#include<string>
#include<memory>
#include<map>
#include<sstream>
#define mem(a) memset(a, 0, sizeof(a))
typedef long long LL;
typedef double dou;
const int Mod = 1000000007;
const double eps = 1e-8;
const LL inf = 1e18;
const int inf1 = 1e9;
using namespace std;
int a[1005] = {0};
struct pair
{
        int x, y;
}pa[505], re[1000005];
int ans[1005][1005];

int main()
{
        LL n, m, i, j, k, x, y, fa = 0, num = 0;
        cin >> n >> m;
        for (i = 0; i < m; ++i)
        {
                cin >> x >> y;
                pa[i].x = x;
                pa[i].y = y;
                a[x] = a[y] = 1;
        }
        for (i = 1; i <= n; ++i)
                if (a[i] == 0)
                {
                        fa = i;
                        a[fa] = 1;
                        break;
                }
        for (i = 0, j = 0; i < m; ++i)
        {
                if (ans[pa[i].x][fa] == 0)
                {
                        ans[pa[i].x][fa] = 1;
                        re[j].x = pa[i].x;
                        re[j++].y = fa;
                        num++;
                }
                if (ans[pa[i].y][fa] == 0)
                {
                        ans[pa[i].y][fa] = 1;
                        re[j].x = pa[i].y;
                        re[j++].y = fa;
                        num++;
                }
        }
        for (i = 1; i <= n; ++i)
                if (a[i] == 0)
                {
                        re[j].x = i;
                        re[j++].y = fa;
                        num++;
                }
        cout << num << endl;
        for (i = 0; i < num; ++i)
                cout << re[i].x << ' ' << re[i].y << endl;

        return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

static const double EPS = 1e-9;

typedef pair<int, int> P;
typedef long long ll;

template<class T> inline T sq(T a){return a*a;}
template<class T> T gcd(T m, T n){return n?gcd(n, m%n):m;}

const int N = 10000;
int par[N];

int find(int x){
	if(x == par[x]) return x;
	return par[x] = find(par[x]);
}

bool same(P p){
	return find(p.first) == find(p.second);
}

void unite(P p){
	par[find(p.first)] = find(p.second);
}

int main(){
	rep(i, N) par[i] = i;

	vector<P> ans;

	int n, m;
	cin >> n >> m;
	map<P, bool> ng;
	rep(i, m){
		int x, y;
		cin >> x >> y;
		if(x > y) swap(x, y);
		ng[MP(x, y)] = 1;
	}

	queue<P> q;
	rep(j, n) rep(i, j) if(!ng[MP(i+1, j+1)])q.push(MP(i+1, j+1));
	while(!q.empty()){
		if(!same(q.front())){
			ans.push_back(q.front());
			unite(q.front());
		}
		q.pop();
	}

	cout << ans.size() << endl;
	rep(i, ans.size()){
		cout << ans[i].first << ' ' << ans[i].second << endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define REV(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

static const double EPS = 1e-9;

typedef pair<int, int> P;
typedef long long ll;

template<class T> inline T sq(T a){return a*a;}
template<class T> T gcd(T m, T n){return n?gcd(n, m%n):m;}

const int N = 1010;
int ng[N][N];

int main(){
	int n, m;
	cin >> n >> m;
	rep(i, m){
		int x, y;
		cin >> x >> y;
		ng[x][y] = ng[y][x] = 1;
	}

	vector<P> ans;
	FOR(i, 1, n+1){
		bool ok = true;
		FOR(j, 1, n+1){
			if(i == j) continue;
			if(ng[i][j]) ok = false;
		}
		if(ok){
			cout << n-1 << endl;
			FOR(j, 1, n+1){
				if(i == j) continue;
				cout << i << ' ' << j << endl;
			}
			return 0;
		}
	}

	return 0;
}


#include<iostream>
#include<vector>
#define REP(i , j) for(i = 1 ; i<=j ; i++)
using namespace std;

int main(){

    unsigned int n , m , t , in , i ;
    cin>>n>>m;

    vector<int> city(n+1,1);

    REP(i , m){
        cin>>t;
        city[t]=0;
        cin>>t;
        city[t]=0;
    }

    REP(i,n){
        if(city[i] == 1){
            in = i;
            break;
        }
    }
    cout<<n-1<<endl;

    REP(i , n){
        if(i != in)
            cout<<i<<" "<<in<<endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
#define REP(i , j) for(i = 1 ; i<=j ; i++)
using namespace std;

int main(){

    unsigned int n , m , t , in , i ;
    cin>>n>>m;

    vector<bool> city(n+1,1);

    REP(i , m){
        cin>>t;
        city[t]=0;
        cin>>t;
        city[t]=0;
    }

    REP(i,n){
        if(city[i] == 1){
            in = i;
            break;
        }
    }
    cout<<n-1<<endl;

    REP(i , n){
        if(i != in)
            cout<<i<<" "<<in<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
ll n,m,a[4000],b[4000],k,n25,n50,n100;
bool forbidden[10000];

int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

	while(cin>>n>>m)
	{
		fr(i,0,m)
		{
			ll x,y;
			cin>>x>>y;
			forbidden[x] = true;
			forbidden[y] = true;
		}

		ll starnode=-1000;
		fr(i,1,n+1)
		{
			if(!forbidden[i])
			{
				starnode = i;
				break;
			}
		}

			cout<<n-1<<endl;

			fr(j,0,n)
			{
				if(j+1 == starnode) continue;
				cout<<starnode<<" "<<j+1<<endl;
			}

	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
ll n,m,a[4000],b[4000],k,n25,n50,n100;
bool forbidden[10000];

int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

	while(cin>>n>>m)
	{
		fr(i,0,m)
		{
			ll x,y;
			cin>>x>>y;
			forbidden[x+1] = true;
			forbidden[y+1] = true;
		}

		ll starnode=-1000;
		fr(i,1,n+1)
		{
			if(!forbidden[i])
			{
				starnode = i;
				break;
			}
		}

			cout<<n-1<<endl;

			fr(j,0,n)
			{
				if(j+1 == starnode) continue;
				cout<<starnode<<" "<<j+1<<endl;
			}

	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <bitset>
#include <ctime>
#include <cmath>
#include <cassert>
#include <algorithm>
using namespace std;

#define pb(p) push_back(p)
#define fr(i,a,b) for(int i=a;i<b;i++)
#define frd(i,a,b) for(int i=a; i>=b i--)
#define PI 2*acos(0.0)
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

int MIN (int a, int b) {
  int x= (b>a)?a:b;
  return x;
}

int MAX(int a, int b) {
  int x= (b<a)?a:b;
  return x;
}

typedef long long ll;
const int size = 1000007;
const ll modulo = 1000000007;
const ll inf = 1e18;
const double eps = 1e-6;
ll n,m,a[4000],b[4000],k,n25,n50,n100;
bool forbidden[10000];

int main()
{
    #ifdef WRITE_IN_FILE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif

	while(cin>>n>>m)
	{
		fr(i,0,m)
		{
			ll x,y;
			cin>>x>>y;
			forbidden[x+1] = true;
			forbidden[y+1] = true;
		}

		ll starnode=-1000;
		fr(i,1,n+1)
		{
			if(!forbidden[i])
			{
				starnode = i+1;
				break;
			}
		}

		if(starnode==-1000)
		{
			cout<<-1<<endl;
		}
		else
		{
			cout<<n-1<<endl;

			fr(j,0,n)
			{
				if(j+1 == starnode) continue;
				cout<<starnode<<" "<<j+1<<endl;
			}

		}
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    cin>>m;
    int *arr;
    arr=new int[n];
    int *arr2;
    arr2=new int[n];
    for(int i=0;i<n;i++)
    {
        arr[i]=i+1;
        arr2[i]=i+1;
    }
    for(int i=0;i<m;i++)
    {
        int a1,a2;
        cin>>a1;
        cin>>a2;
        arr[a1-1]=0;
        arr[a2-1]=0;
    }
    int b;
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=0)
        {
            b=arr[i];
            break;
        }
    }
    int num=0;
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            num++;
    }
    cout<<num<<endl;
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            cout<<b<<" "<<arr2[i]<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    cin>>m;
    int *arr;
    arr=new int[n];
    int *arr2;
    arr2=new int[n];
    for(int i=0;i<n;i++)
    {
        arr[i]=i+1;
        arr2[i]=i+1;
    }
    for(int i=0;i<m;i++)
    {
        int a1,a2;
        cin>>a1;
        cin>>a2;
        if(arr[i]==a1 || arr[i]==a2)
            arr[i]=0;
    }
    int b;
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=0)
        {
            b=arr[i];
            break;
        }
    }
    int num=0;
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            num++;
    }
    cout<<num<<endl;
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            cout<<b<<" "<<arr2[i]<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    cin>>m;
    int *arr;
    arr=new int[n];
    int *arr2;
    arr2=new int[n];
    for(int i=0;i<n;i++)
    {
        arr[i]=i+1;
        arr2[i]=i+1;
    }
    for(int i=0;i<m;i++)
    {
        int a1,a2;
        cin>>a1;
        cin>>a2;
        if(arr[i]==a1 || arr[i]==a2)
            arr[i]=0;
    }
    int b;
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=0)
        {
            b=arr[i];
            break;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            cout<<b<<" "<<arr2[i]<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    cin>>m;
    int *arr;
    arr=new int[n];
    int *arr2;
    arr2=new int[n];
    for(int i=0;i<n;i++)
    {
        arr[i]=i+1;
        arr2[i]=i+1;
    }
    for(int i=0;i<m;i++)
    {
        int a1,a2;
        cin>>a1;
        cin>>a2;
        for(int j=0;j<n;j++)
        {
        if(arr[j]==a1 || arr[j]==a2)
        {
            arr[j]=0;
            break;
        }
        }
    }
    int b;
    for(int i=0;i<n;i++)
    {
        if(arr[i]!=0)
        {
            b=arr[i];
            break;
        }
    }
    int num=0;
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            num++;
    }
    cout<<num<<endl;
    for(int i=0;i<n;i++)
    {
        if(arr2[i]==b)
            continue;
        else
            cout<<b<<" "<<arr2[i]<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int main()
{
    freopen("cb.txt","r",stdin);
    int n,m;
    cin>>n>>m;
    int u,v;
    int link[1001];
    memset(link,0,sizeof(link));
    for(int i=0;i<m;i++)
    {
        cin>>u>>v;
        link[u]++;
        link[v]++;
    }
    int point;
    for(int i=1;i<=n;i++)
    {
        if(!link[i])
        {
            point=i;
            break;
        }
    }
    cout<<n-1<<endl;
    for(int i=1;i<=n;i++)
    {
        if(i==point)
            continue;
        cout<<point<<" "<<i<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int edge[1001][1001];
int main()
{
  //  freopen("cb.txt","r",stdin);
    int n,m;
    cin>>n>>m;
    int link[1001];
    memset(link,0,sizeof(link));
    int u,v;
    for(int i=0;i<m;i++)
    {
        cin>>u>>v;
        link[u]++;
        link[v]++;
    }
    int point;
    for(int i=1;i<=n;i++)
    {
        if(!link[i])
        {
            point=i;
            break;
        }
    }
    cout<<n-1<<endl;
    for(int i=1;i<=n;i++)
    {
        if(i==point)
            continue;
        cout<<point<<" "<<i<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int edge[1001][1001];
int main()
{
    freopen("cb.txt","r",stdin);
    int n,m;
    cin>>n>>m;
    int link[1001];
    memset(link,0,sizeof(link));
    int u,v;
    for(int i=0;i<m;i++)
    {
        cin>>u>>v;
        link[u]++;
        link[v]++;
    }
    int point;
    for(int i=1;i<=n;i++)
    {
        if(!link[i])
        {
            point=i;
            break;
        }
    }
    cout<<n-1<<endl;
    for(int i=1;i<=n;i++)
    {
        if(i==point)
            continue;
        cout<<point<<" "<<i<<endl;
    }
    return 0;
}


#include<iostream>
#include<queue>

using namespace std;

int n, m,a,b;
int arr[400];
int dis[400];

/*void bfs(int s){
	memset(dis, -1, sizeof(dis));
	dis[s] = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <=n ; j++)
			if (arr[i][j] != -1 && dis[j] <= 2)
			{
				cout << i, j;
			}
	}

}*/

int main(){
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		cin >> a >> b;

		arr[a]= 1;
		arr[b] = 1;

	}

	cout << n - 1 << endl;
	int i ;
	for ( i = 1; i <= n; i++)

		if (!arr[i])
			break;

		for (int j = 1; j <= n; j++)
		{
			if (i!=j)
			cout << i << j;
		}

		return 0;
	}


#include<iostream>
#include<queue>

using namespace std;

int n, m,a,b;
int arr[100000];
int dis[400];

int main(){
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		cin >> a >> b;

		arr[a]= 1;
		arr[b] = 1;

	}

	cout << n - 1 << endl;
	int i ;
	for ( i = 1; i <= n; i++)

		if (!arr[i])
			break;

		for (int j = 1; j <= n; j++)
		{
			if (i!=j)
			cout << i <<" "<< j  <<endl;
		}

		return 0;
	}


#include<iostream>
#include<queue>

using namespace std;

int n, m,a,b;
int arr[400];
int dis[400];

/*void bfs(int s){
	memset(dis, -1, sizeof(dis));
	dis[s] = 0;
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <=n ; j++)
			if (arr[i][j] != -1 && dis[j] <= 2)
			{
				cout << i, j;
			}
	}

}*/

int main(){
	cin >> n >> m;
	for (int i = 0; i < m; i++){
		cin >> a >> b;

		arr[a]= 1;
		arr[b] = 1;

	}

	cout << n - 1 << endl;
	int i ;
	for ( i = 1; i <= n; i++)

		if (!arr[i])
			break;

		for (int j = 1; j <= n; j++)
		{
			if (i!=j)
			cout << i <<" "<< j  <<endl;
		}

		return 0;
	}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
bool f[1009] = {};

int main() {

	for (int i = 1; i <= 1000; i++)
		f[i] = 1;
	int n, m;
	vector<int>w;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y;

		f[x] = 0; f[y] = 0;
	}
	int t = 1;
	for (int i = 2; i <= n;i++)
		if (f[i]) {
			t = i; break;
		}
	int ans = (n -1);
	cout << ans << endl;
	for (int i = 2; i <= n; i++) {
		cout << t << " " << i << endl;

	}

	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
bool f[1009] = {};

int main() {

	for (int i = 1; i <= 1000; i++)
		f[i] = 1;
	int n, m;
	vector<int>w;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y;

		f[x] = 0; f[y] = 0;
	}
	int t = 1;
	for (int i = 2; i <= n;i++)
		if (f[i]) {
			t = i; break;
		}
	int ans = (n -1);
	cout << ans << endl;
	for (int i = 1; i <= n; i++) {
		if (t == i)continue;
		cout << t << " " << i << endl;

	}

	return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<map>
#include<set>
#include<queue>
#include <utility>

using namespace std;
bool f[1009] = {};

int main() {

	for (int i = 1; i <= 1000; i++)
		f[i] = 1;
	int n, m;
	vector<int>w;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int x, y; cin >> x >> y;
		if (y < x)swap(x, y);
		if (x == 1) {
			f[y] = 0;
			w.push_back(y);
		}
	}
	int t = 1;
	for (int i = 2; i <= n;i++)
		if (f[i]) {
			t = i; break;
		}
	int ans = (n -1);
	cout << ans << endl;
	for (int i = 2; i <= n; i++) {
		if (f[i])cout << "1 " << i << endl;
		else cout << t << " " << i << endl;

	}

	return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <functional>
#include <numeric>

using namespace std;

#define MAX 1000+10
#define INF 1000000000000000000
#define MOD 1000000009
#define CLR(a,x) memset(a,x,sizeof a)
#define LL long long
#define ALL(v) v.begin(),v.end()
#define FR(i,n) for(LL i=0;i<(LL)n;i++)
#define FAB(i,a,b) for(LL i=(LL)a;i<(LL)b;i++)
#define FBA(i,b,a) for(LL i=(LL)b;i>=(LL)a;i--)
#define IIN(x) scanf("%d",&x)
#define IIN2(x,y) scanf("%d%d",&x,&y)
#define LIN(x) scanf("%I64d",&x)
#define LIN2(x,y) scanf("%I64d%I64d",&x,&y)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define PIS pair<int,string>
#define PI 3.141592653589793238
#define VI vector<int>
#define VLL vector<LL>
#define VS vector<string>
#define SI set<int>
#define SLL set<LL>
#define SS set<string>
#define MII map<int,int>
#define MLL map<LL,LL>
#define MIV map<int,VI>
#define MSI map<string,int>
#define MIS map<int,string>

int n,m,mark[MAX];

int main()
{
	//freopen("in.txt","r",stdin);

	cin>>n>>m;

	CLR(mark,0);
	FR(i,m)
	{
		int a,b;
		IIN2(a,b);
		mark[a]=mark[b]=1;
	}

	int center;
	FAB(i,1,n+1)if(!mark[i])
	{
		center=i;
		break;
	}

	cout<<n-1<<endl;
	FAB(i,1,n+1)if(i!=center) cout<<center<<" "<<i<<endl;

	return 0;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <functional>
#include <numeric>

using namespace std;

#define MAX 1000+10
#define INF 1000000000000000000
#define MOD 1000000009
#define CLR(a,x) memset(a,x,sizeof a)
#define LL long long
#define ALL(v) v.begin(),v.end()
#define FR(i,n) for(LL i=0;i<(LL)n;i++)
#define FAB(i,a,b) for(LL i=(LL)a;i<(LL)b;i++)
#define FBA(i,b,a) for(LL i=(LL)b;i>=(LL)a;i--)
#define IIN(x) scanf("%d",&x)
#define IIN2(x,y) scanf("%d%d",&x,&y)
#define LIN(x) scanf("%I64d",&x)
#define LIN2(x,y) scanf("%I64d%I64d",&x,&y)
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define PIS pair<int,string>
#define PI 3.141592653589793238
#define VI vector<int>
#define VLL vector<LL>
#define VS vector<string>
#define SI set<int>
#define SLL set<LL>
#define SS set<string>
#define MII map<int,int>
#define MLL map<LL,LL>
#define MIV map<int,VI>
#define MSI map<string,int>
#define MIS map<int,string>

int n,m,mark[MAX];

int main()
{
	//freopen("in.txt","r",stdin);

	cin>>n>>m;

	if(n==1)
	{
		cout<<0<<endl;
		return 0;
	}
	if(n==2)
	{
		cout<<1<<endl;
		cout<<"1 2"<<endl;
		return 0;
	}
	if(n==3)
	{
		cout<<2<<endl;
		cout<<"1 2"<<endl;
		cout<<"2 3"<<endl;
		return 0;
	}

	CLR(mark,0);
	FR(i,m)
	{
		int a,b;
		IIN2(a,b);
		mark[a]=mark[b]=1;
	}

	int center;
	FAB(i,1,n+1)if(!mark[i])
	{
		center=i;
		break;
	}

	cout<<n-1<<endl;
	FAB(i,1,n+1)if(i!=center) cout<<center<<" "<<i<<endl;

	return 0;
}


#include <iostream>
using namespace std;
int main ()
{int n,m,k[1009]={0},a,b;
cin>>n>>m;
if (m==0) while (true) m++;
for (int i=0; i<m; i++)
{cin>>a>>b; k[a]=1; k[b]=1;}
for (int i=1; i<n; i++)
if (k[i]==0) a=i;
cout<<n-1<<endl;
for (int i=1; i<=n; i++)
if (i!=a) cout<<a<<" "<<i<<endl;
return 0;
}


#include <iostream>
using namespace std;
int main ()
{int n,m,k[1000]={0},a,b;
cin>>n>>m;
for (int i=0; i<m; i++)
{cin>>a>>b; k[a]=1; k[b]=1;}
for (int i=1; i<n; i++)
if (k[i]==0) a=i;
cout<<n-1<<endl;
for (int i=1; i<=n; i++)
if (i!=a) cout<<a<<" "<<i<<endl;
return 0;
}


#include <iostream>
using namespace std;
int main ()
{int n,m,k[1009]={0},a,b;
cin>>n>>m;
for (int i=0; i<m; i++)
{cin>>a>>b; k[a]=1; k[b]=1;}
for (int i=1; i<n; i++)
if (k[i]==0) a=i;
cout<<n-1<<endl;
for (int i=1; i<=n; i++)
if (i!=a) cout<<a<<" "<<i<<endl;
return 0;
}


#include <iostream>
using namespace std;
int main ()
{int n,m,k[1009]={0},a,b;
cin>>n>>m;
for (int i=0; i<m; i++)
{cin>>a>>b; k[a]=1; k[b]=1;}
for (int i=1; i<=n; i++)
if (k[i]==0) a=i;
cout<<n-1<<endl;
for (int i=1; i<=n; i++)
if (i!=a) cout<<a<<" "<<i<<endl;
return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

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

    bool key[101];
    memset(key, true, sizeof(key));

    for(int i = 0; i < m; i++){
        int tmp;
        cin >> tmp;
        key[tmp] = false;
        cin >> tmp;
        key[tmp] = false;
    }

    for(int i = 1; i <= n; i++){
        if(key[i]){
            cout << n-1 << endl;
            for(int j = 1; j <= n; j++){
                if(j != i) cout << j << ' ' << i << endl;
            }
            break;
        }
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

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

    bool key[1001];
    memset(key, true, sizeof(key));

    for(int i = 0; i < m; i++){
        int tmp;
        cin >> tmp;
        key[tmp] = false;
        cin >> tmp;
        key[tmp] = false;
    }

    for(int i = 1; i <= n; i++){
        if(key[i]){
            cout << n-1 << endl;
            for(int j = 1; j <= n; j++){
                if(j != i) cout << j << ' ' << i << endl;
            }
            return 0;
        }
    }

    /*
    cout << n << endl;
    for(int i = 1; i <= n-1; i++) cout << i << ' ' << i+1 << endl;
    cout << n << ' ' << 1 << endl;
    */
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

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

    bool key[101];
    memset(key, true, sizeof(key));

    for(int i = 0; i < m; i++){
        int tmp;
        cin >> tmp;
        key[tmp] = false;
        cin >> tmp;
        key[tmp] = false;
    }

    for(int i = 1; i <= n; i++){
        if(key[i]){
            cout << n-1 << endl;
            for(int j = 1; j <= n; j++){
                if(j != i) cout << j << ' ' << i << endl;
            }
            return 0;
        }
    }

    cout << n << endl;
    for(int i = 1; i <= n; i++){
        if(i == n){
            cout << i << ' ' << 0 << endl;
        }
        cout << i << ' ' << i+1 << endl;
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
typedef long long ll;
using namespace std;

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

    bool key[101];
    memset(key, true, sizeof(key));

    for(int i = 0; i < m; i++){
        int tmp;
        cin >> tmp;
        key[tmp] = false;
        cin >> tmp;
        key[tmp] = false;
    }

    for(int i = 1; i <= n; i++){
        if(key[i]){
            cout << n-1 << endl;
            for(int j = 1; j <= n; j++){
                if(j != i){
                    cout << j << ' ' << i << endl;
                }
            }
            return 0;
        }
    }

    return 0;
}


//***********INCLUDE************//
    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <algorithm>
    #include <math.h>
    #include <set>
    #include <numeric>
    #include <ctime>
//******************************//

//*********************DEFINES*********************//
    #define rep(a,b,c) for(long int a = b; a < c; a++)
    #define repd(a,b,c) for(long int a = b; a >= c; a--)
    #define _sort(a) sort (a.begin(), a.end())
    #define Sort(a,b) sort (a.begin(), a.end(), b)
    #define length(s) s.size()
    #define start freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #define _start freopen("cows.in", "r", stdin); freopen("cows.out", "w", stdout);
    #define mp std::make_pair
    #define xx first
    #define yy second
//*************************************************//

//*********TYPE************//
    typedef long long int li;
    typedef long double ldb;
    //typedef __int64 lli;
    typedef std::vector<int> vi;
    typedef std::vector<li> vli;
    typedef std::vector<bool> vb;
    typedef std::vector<std::string> vs;
    typedef std::vector<vi> vvi;
    typedef std::pair<int,int> pii;
    typedef std::pair<ldb,ldb> pdd;
//*************************//

//*********CONST*********//
    const int INF = 1e9+1e4;
    const int EPS = 1e-6;
    const ldb PI = 3.14159265358;
//***********************//

ldb vec_prod (pdd a, pdd b){    return (a.xx * b.yy - a.yy * b.xx); }
ldb scal_prod (pdd a, pdd b){ return (a.xx * b.xx + a.yy * b.yy); }
ldb evk_dist (pdd a, pdd b) { return sqrt (ldb((a.xx - b.xx) * (a.xx - b.xx) + (a.yy - b.yy) * (a.yy - b.yy))); }
ldb v_evk_dist (pdd a) { return sqrt (a.xx * a.xx + a.yy * a.yy); }
pdd ofs (pdd a, pdd b){ return mp (a.xx - b.xx, a.yy - b.yy); }
pdd neg (pdd a) { return mp (-a.xx, -a.yy); }
pdd norm (pdd a) { return mp (-a.yy, a.xx); }

using namespace std;

void no(){
    cout << "NO";
    exit (0);
}

int main (){
   // start;
    int n, m;
    cin >> n >> m;
    vb x(n, false);
    rep (i, 0, m){
        int t;
        cin >> t;
        x[t] = true;
    }
    int y;
    rep (i, 0, n)
        if (!x[i]){
            y = i;
            break;
        }
    cout << n - 1 << "\n";
    rep (i, 0, n)
        if (i != y)
            cout << y + 1 << " " << i + 1 << "\n";
}


//***********INCLUDE************//
    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <algorithm>
    #include <math.h>
    #include <set>
    #include <numeric>
    #include <ctime>
//******************************//

//*********************DEFINES*********************//
    #define rep(a,b,c) for(long int a = b; a < c; a++)
    #define repd(a,b,c) for(long int a = b; a >= c; a--)
    #define _sort(a) sort (a.begin(), a.end())
    #define Sort(a,b) sort (a.begin(), a.end(), b)
    #define length(s) s.size()
    #define start freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #define _start freopen("cows.in", "r", stdin); freopen("cows.out", "w", stdout);
    #define mp std::make_pair
    #define xx first
    #define yy second
//*************************************************//

//*********TYPE************//
    typedef long long int li;
    typedef long double ldb;
    //typedef __int64 lli;
    typedef std::vector<int> vi;
    typedef std::vector<li> vli;
    typedef std::vector<bool> vb;
    typedef std::vector<std::string> vs;
    typedef std::vector<vi> vvi;
    typedef std::pair<int,int> pii;
    typedef std::pair<ldb,ldb> pdd;
//*************************//

//*********CONST*********//
    const int INF = 1e9+1e4;
    const int EPS = 1e-6;
    const ldb PI = 3.14159265358;
//***********************//

ldb vec_prod (pdd a, pdd b){    return (a.xx * b.yy - a.yy * b.xx); }
ldb scal_prod (pdd a, pdd b){ return (a.xx * b.xx + a.yy * b.yy); }
ldb evk_dist (pdd a, pdd b) { return sqrt (ldb((a.xx - b.xx) * (a.xx - b.xx) + (a.yy - b.yy) * (a.yy - b.yy))); }
ldb v_evk_dist (pdd a) { return sqrt (a.xx * a.xx + a.yy * a.yy); }
pdd ofs (pdd a, pdd b){ return mp (a.xx - b.xx, a.yy - b.yy); }
pdd neg (pdd a) { return mp (-a.xx, -a.yy); }
pdd norm (pdd a) { return mp (-a.yy, a.xx); }

using namespace std;

void no(){
    cout << "NO";
    exit (0);
}

int main (){
   // start;
    int n, m;
    cin >> n >> m;
    vb x(n, false);
    rep (i, 0, m){
        int t;
        cin >> t;
        x[t-1] = true;
    }
    int y;
    rep (i, 0, n)
        if (!x[i]){
            y = i;
            break;
        }
    cout << n - 1 << "\n";
    rep (i, 0, n)
        if (i != y)
            cout << y + 1 << " " << i + 1 << "\n";
}


//***********INCLUDE************//
    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <algorithm>
    #include <math.h>
    #include <set>
    #include <numeric>
    #include <ctime>
//******************************//

//*********************DEFINES*********************//
    #define rep(a,b,c) for(long int a = b; a < c; a++)
    #define repd(a,b,c) for(long int a = b; a >= c; a--)
    #define _sort(a) sort (a.begin(), a.end())
    #define Sort(a,b) sort (a.begin(), a.end(), b)
    #define length(s) s.size()
    #define start freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #define _start freopen("cows.in", "r", stdin); freopen("cows.out", "w", stdout);
    #define mp std::make_pair
    #define xx first
    #define yy second
//*************************************************//

//*********TYPE************//
    typedef long long int li;
    typedef long double ldb;
    //typedef __int64 lli;
    typedef std::vector<int> vi;
    typedef std::vector<li> vli;
    typedef std::vector<bool> vb;
    typedef std::vector<std::string> vs;
    typedef std::vector<vi> vvi;
    typedef std::pair<int,int> pii;
    typedef std::pair<ldb,ldb> pdd;
//*************************//

//*********CONST*********//
    const int INF = 1e9+1e4;
    const int EPS = 1e-6;
    const ldb PI = 3.14159265358;
//***********************//

ldb vec_prod (pdd a, pdd b){    return (a.xx * b.yy - a.yy * b.xx); }
ldb scal_prod (pdd a, pdd b){ return (a.xx * b.xx + a.yy * b.yy); }
ldb evk_dist (pdd a, pdd b) { return sqrt (ldb((a.xx - b.xx) * (a.xx - b.xx) + (a.yy - b.yy) * (a.yy - b.yy))); }
ldb v_evk_dist (pdd a) { return sqrt (a.xx * a.xx + a.yy * a.yy); }
pdd ofs (pdd a, pdd b){ return mp (a.xx - b.xx, a.yy - b.yy); }
pdd neg (pdd a) { return mp (-a.xx, -a.yy); }
pdd norm (pdd a) { return mp (-a.yy, a.xx); }

using namespace std;

void no(){
    cout << "NO";
    exit (0);
}

int main (){
   // start;
    int n, m;
    cin >> n >> m;
    vb x(n, false);
    rep (i, 0, m*2){
        int t;
        cin >> t;
        x[t-1] = true;
    }
    int y;
    rep (i, 0, n)
        if (!x[i]){
            y = i;
            break;
        }
    cout << n - 1 << "\n";
    rep (i, 0, n)
        if (i != y)
            cout << y + 1 << " " << i + 1 << "\n";
}


#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
void main()
{
	int n,m; cin>>n>>m;
	vector<bool>v(n);
	int a,b;
	for(int i=0; i<m; ++i)
	{
		cin>>a>>b;
		v[a-1]=v[b-1]=1;
	}
	for(int i=0; i<n; ++i)
	{
		if(!v[i])
		{
			for(int j=0; j<n; ++j)
			{
				if(i!=j)cout<<i+1<<' '<<j+1<<endl;
			}
			break;
		}
	}
}


#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
void main()
{
	int n,m; cin>>n>>m;
	vector<bool>v(n);
	int a,b;
	for(int i=0; i<m; ++i)
	{
		cin>>a>>b;
		v[a-1]=v[b-1]=1;
	}
	cout<<n-1<<endl;
	for(int i=0; i<n; ++i)
	{
		if(!v[i])
		{
			for(int j=0; j<n; ++j)
			{
				if(i!=j)cout<<i+1<<' '<<j+1<<endl;
			}
			break;
		}
	}
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <utility>
#include <cstdlib>
#include <cstdio>
#include <cstdarg>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstring>
#include <bitset>
#include <set>
#include <stdio.h>

using namespace std;
typedef vector<string>          vs;
typedef vector<int>             vi;
typedef long long               ll;
#define sz(a)					int((a).size())
#define in(a)					atoi(a.c_str())
#define FOR(a,b,c)				for(int (a)=(b);(a)<(c);(a)++)
#define sor(a)					sort((a).begin(),(a).end())
#define all(i)					(i).begin(),(i).end()
#define pb						push_back
#define mp						make_pair
#define bug1(x)					cout<<#x<<" = "<<x<<endl
#define bug2(x,y)				cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define bug3(x,y,z)				cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<endl
#define INF INT_MAX/2

int main(int argc, char* argv[])
{
	int n, m;
	cin >> n >> m;
	bool count[1005] = {0};
	FOR(i,0,m)
	{
		int a,b;
		cin >> a >> b;
		count[a] = true, count[b] = true;
	}
	int pos = 0;
	FOR(i,1,n+1)
		if(!count[i]){
			pos = i;
			break;
		}

	cout << n-1 << endl;
	FOR(i,1,n+1)
	{
		if(i == pos)continue;
		cout << i << " " << pos << endl;
	}

    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <utility>
#include <cstdlib>
#include <cstdio>
#include <cstdarg>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstring>
#include <bitset>
#include <set>
#include <stdio.h>

using namespace std;
typedef vector<string>          vs;
typedef vector<int>             vi;
typedef long long               ll;
#define sz(a)					int((a).size())
#define in(a)					atoi(a.c_str())
#define FOR(a,b,c)				for(int (a)=(b);(a)<(c);(a)++)
#define sor(a)					sort((a).begin(),(a).end())
#define all(i)					(i).begin(),(i).end()
#define pb						push_back
#define mp						make_pair
#define bug1(x)					cout<<#x<<" = "<<x<<endl
#define bug2(x,y)				cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define bug3(x,y,z)				cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<endl
#define INF INT_MAX/2

int main(int argc, char* argv[])
{
	int n, m;
	cin >> n >> m;
	int count[1005] = {0};
	FOR(i,0,m)
	{
		int a,b;
		cin >> a >> b;
		count[a]++, count[b]++;
	}
	int Min = INF;
	FOR(i,0,n)
		Min = min(Min, count[i]);

	if(Min == 0)
		cout << n-1 << endl;
	else cout << (n-1)+Min*(n-1) << endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <utility>
#include <cstdlib>
#include <cstdio>
#include <cstdarg>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstring>
#include <bitset>
#include <set>
#include <stdio.h>

using namespace std;
typedef vector<string>          vs;
typedef vector<int>             vi;
typedef long long               ll;
#define sz(a)					int((a).size())
#define in(a)					atoi(a.c_str())
#define FOR(a,b,c)				for(int (a)=(b);(a)<(c);(a)++)
#define sor(a)					sort((a).begin(),(a).end())
#define all(i)					(i).begin(),(i).end()
#define pb						push_back
#define mp						make_pair
#define bug1(x)					cout<<#x<<" = "<<x<<endl
#define bug2(x,y)				cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define bug3(x,y,z)				cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<endl
#define INF INT_MAX/2

int main(int argc, char* argv[])
{
	int n, m;
	cin >> n >> m;
	int count[1005] = {0};
	FOR(i,0,m)
	{
		int a,b;
		cin >> a >> b;
		count[a]++, count[b]++;
	}
	int Min = INF;
	int pos = 0;
	FOR(i,1,n+1)
		Min = min(Min, count[i]), pos = i;

	if(Min == 0)
	{
		cout << n-1 << endl;
		FOR(i,1,n+1)
		{
			if(i == pos)continue;
			cout << i << " " << pos << endl;
		}
	}

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int used[10001]={0};
int main()
{
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	for(i=1;i<=m;i++)
	{
		int a,b;
		scanf("%d %d",&a,&b);
		used[a]=1;
		used[b]=1;
	}
	if(n==2)
	{
		printf("1\n1 2");
	}
	else
	{
		for(i=1;i<=n;i++)
		{
			if(used[i]==0) break;
		}
		printf("%d\n",n-1);
		for(j=1;j<=n;j++)
		{
			if(i!=j)
			{
				printf("%d %d\n",i,j);
			}
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int used[10001]={0};
int main()
{
	int i,j,n,m;
	scanf("%d %d",&n,&m);
	for(i=1;i<=m;i++)
	{
		int a,b;
		scanf("%d %d",&a,&b);
		used[a]=1;
		used[b]=1;
	}
	if(n==2)
	{
		printf("1 2");
	}
	else
	{
		for(i=1;i<=n;i++)
		{
			if(used[i]==0) break;
		}
		printf("%d\n",n-1);
		for(j=1;j<=n;j++)
		{
			if(i!=j)
			{
				printf("%d %d\n",i,j);
			}
		}
	}
	return 0;
}


#include <queue>
#include <iostream>
#include <set>
#include <cstdio>
#include <algorithm>
#include <string>
#include <map>
#include <stack>
#include <string>
#include <sstream>
#include <complex>
#include <cmath>
#include <math.h>

using namespace std;
typedef long long LL;
typedef vector<int> lnum;
typedef complex<double> cd;
const int base= 1000 * 1000 * 1000;
const double PI =  3.14159265358979323846;
double Eps = 0.000001;
pair<int,int> bound[1<<18];
int wood[1<<18];
int a[1<<18];
struct point
{
	double x , y;
	point(double x = 0 , double y = 0)
	{
		this->x = x;
		this-> y = y;
	}
};
struct lineD
{
	double a , b , c;
	lineD()
	{
		a = b = c = 0;
	}
	lineD(double a , double b , double c)
	{
		double z=  sqrt(a * a + b * b);
		a/=z;
		b/=z;
		c/=z;
		if ( a <-Eps || (a < Eps && b <-Eps))
		{
			a *=-1;
			b *=-1;
			c *=-1;
		}
		this->a = a;
		this->b = b;
		this->c = c;
	}
};
struct lineI
{
	int a , b , c;
	lineI()
	{
		a = b = c = 0;
	}
	int gcd(int a , int b)
	{
		if ( b==0)
			return a;
		return gcd(b,a % b);
	}
	lineI(int a , int b , int c)
	{
		double z = gcd(gcd(a,b),c);
		a/=z;
		b/=z;
		c/=z;
		if ( a < 0 || a==0 && b < 0)
		{
			a*=-1;
			b*=-1;
			c*=-1;
		}
		this->a = a;
		this->b = b;
		this->c = c;
	}
};
lineD K(point a, point b)
{
	double A = a.y - b.y;
	double B = b.x - a.x;
	double C = -A * a.x - B*a.y;
	return lineD(A,B,C);
}

bool cmp(point a , point b)
{
	return a.x < b.x || a.x == b.x && a.y < b.y;
}

bool l(point a , point b , point c)
{
	return (b.x - a.x) * (c.y - a.y) - ( b.y - a.y)* ( c.y - a.y) < - Eps;
}

bool r(point a , point b , point c)
{
	return (b.x - a.x) * (c.y - a.y) - ( b.y - a.y)* ( c.y - a.y) > Eps;
}

void Grah(vector<point> & a)
{
	if ( a.size() ==0)
		return ;
	sort(a.begin() , a.end() , &cmp);
	vector<point> up,down;
	point p1 = a[0] , p2 = a.back();
	for ( int i = 1; i < a.size() ; i ++)
	{
		if ( i == a.size() - 1 || l(p1,a[i],p2))
		{
			while ( up.size() >= 2 && !l(up[up.size() - 2], up[up.size() - 1] , a[i]))
				up.pop_back();
			up.push_back(a[i]);
		}
		if ( i == a.size() - 1 || r(p1,a[i],p2))
		{
			while ( down.size() >= 2 && !r(down[down.size() - 2], down[down.size() - 1] , a[i]))
				down.pop_back();
			down.push_back(a[i]);
		}
	}
	a.clear();
	for ( int i = 0 ; i < up.size(); i ++)
		a.push_back(up[i]);
	for ( int i = (int)down.size() - 2; i > 0; i --)
		a.push_back(down[i]);
}

void ini(int  l , int r , int n)
{
	bound[n].first = l;
	bound[n].second = r;
	if ( l==r)
	{
		wood[n]  = a[l];
	}
	else
	{
		ini(l,(r+l) / 2, 2 * n + 1);
		ini((r+l) / 2 + 1, r, 2 * n + 2);
		wood[n] = wood[2 * n +1 ] + wood[2 * n + 2];
	}
}

int find(int l , int r , int n)
{
	if ( bound[n].first==l && bound[n].second==r)
	{
		return wood[n];
	}
	else
	{
		int left , right;
		if ( bound[n * 2 + 1].second >= r)
		{
			return find(l,r, n * 2 + 1);
		}
		else
			if ( bound[n * 2 + 2].first <= l)
			{
				return find(l,r,n * 2 + 2);
			}
			else
			{
				left = find(l,bound[n * 2 + 1].second,n * 2 + 1);
				right = find(bound[n * 2 + 2].first, r , n * 2 + 2);
				return left + right;
			}
	}
}

vector <int> z_function(string s)
{
	int n = s.size();
	vector<int> z(n);
	for( int i = 1, l = 0, r =0; i<n;i++)
	{
		if ( i <=r)
			z[i] = min(z[i-l],r-i+1);
		while( i+z[i] < n && s[z[i]]==s[i+z[i]])
			z[i]++;
		if ( i+z[i] - 1	>r)
			 l = i, r = i + z[i] - 1;
	}
	return z;
}
vector<int> CMP(string s)
{
	int n = s.size();
	vector<int> pi(n);
	for ( int i = 1; i < n ; i ++)
	{
		int j = pi[i-1];
		while ( j > 0 && s[i]!=s[j])
			j = pi[j-1];
		if ( s[i]==s[j]) j++;
		pi[i] = j;
	}
	return pi;
}

void bm(string s)
{
	int a[26];
	memset(a,0,26);
	int n = s.size();
	for ( int i = 0 ; i < n ; i ++)
		a[s[i]] = i;
}

lnum read(string s)
{
	lnum a;
	for ( int i = s.size() ; i > 0 ; i-=9)
	{
		if ( i < 9)
			a.push_back(atoi(s.substr(0,i).c_str()));
		else
			a.push_back(atoi(s.substr(i-9,9).c_str()));
	}
	return a;
}

void write(lnum &a)
{
	printf("%d",a.empty() ? 0 : a.back());
	for ( int i = (int) a.size() - 2; i >=0;i--)
		printf("%09d",a[i]);
}

void sum(lnum & a, lnum & b)
{
	int carry = 0;
	for ( int i = 0 ; i < max(a.size() , b.size()) || carry; i ++)
	{
		if ( i==a.size())
			a.push_back(0);
		a[i] +=carry + (i < b.size() ? b[i] : 0);
		carry = a[i] > base;
		if ( carry)
			a[i] -=base;
	}

}
void raz(lnum & a, lnum & b)
{
	int carry = 0;
	for ( int i = 0 ; i < b.size() || carry; i ++)
	{
		a[i]-=carry + (i < b.size() ? b[i] : 0);
		carry = a[i] < 0;
		if ( carry)
			a[i] +=base;
	}
	while ( a.size() > 1 && a.back() ==0)
		a.pop_back();
}

void mul( lnum & a , int b)
{
	int carry = 0;
	for ( int i = 0 ; i < a.size() || carry; i ++)
	{
		LL cur = carry + a[i] * 1ll * b;
		a[i] = (int)(cur % base);
		carry = (int)(cur / base);
	}

	while ( a.size() > 1 && a.back() ==0)
		a.pop_back();
}

void fft(vector<cd> & a, int inv = 1)
{
	if (a.size() == 1)
		return;
	int n = a.size() / 2;
	vector<cd> a0(n), a1(n);
	for(int i = 0; i < n; ++i)
		a0[i] = a[2 * i], a1[i] = a[2 * i + 1];
	fft(a0, inv), fft(a1, inv);
	cd w = 1.0;
	double angle = acos(-1.0) / n * inv;
	cd mul(cos(angle), sin(angle));
	for(int i = 0; i < n; ++i)
	{
		a[i] = a0[i] + a1[i] * w;
		a[n + i] = a0[i] - a1[i] * w;
		w *= mul;
		if (inv == -1)
		{
			a[i] /= 2;
			a[n + i] /= 2;
		}
	}
}
void mul(vector<int> & a, vector<int> & b, vector<int> & c)
{
	int n = 1;
	while (n < a.size())
		n <<= 1;
	n <<= 1;
	vector<cd> A(a.begin(), a.end());
	vector<cd> B(b.begin(), b.end());
	A.resize(n), B.resize(n);
	fft(A), fft(B);
	for(int i = 0; i < A.size(); ++i)
		A[i] *= B[i];
	fft(A, -1);
	c.clear();
	c.resize(A.size());
	int carry = 0;
	for(int i = 0; i < A.size(); ++i)
	{
		c[i] = (int)(A[i].real() + 0.5) + carry;
		carry = c[i] / base;
		c[i] %= base;
	}
	while ( c.size() > 1 && c.back() ==0)
		c.pop_back();
}
int main()
{
	int n;
	scanf("%d", &n);
	for ( int i = 0 ; i < n ; i ++)
	{
		scanf("%d",&a[i]);
	}
	ini(0,n-1,0);
	int k;
	cin >> k;
	for ( int i = 0 ; i < k ; i ++)
	{
		int l , r;
		cin >> l>> r;
		l--;
		r--;
		cout << find(l,r,0) << endl;
	}
	return 0;
}


#include <iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<sstream>
#include<iomanip>
#include<vector>

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define DB(x) cout<<x<<endl;
#define big  32770
#define mod 1000000000
#define ll long long

using namespace std;

const int N=3005;

int m,n;
vector<ll> a[3005];
vector<ll> s;

ll p[3005];
ll res=LLONG_MAX;

long long XULY(int l)
{
    ll c=0;
    int sophieu=0;
    s.clear();
    FOR(i,2,n)
    {
        if (p[i]>l)
        {
            FOR(j,1,p[i]-l) c=c+a[i][j-1],sophieu++;
            FOR(j,p[i]-l+1,p[i]) s.push_back(a[i][j-1]);
        }
        else
        {
            FOR(j,1,p[i]) s.push_back(a[i][j-1]);
        }
    }

    if (sophieu+p[1]>l) return c;
    if (s.size()+sophieu+p[1]<=l) return LLONG_MAX;

    sort(s.begin(),s.end());
    FOR(i,1,l-sophieu-p[1]+1) c=c+s[i-1];
    return c;

}

int main()
{
  //freopen("text.inp","r",stdin);
  //freopen("BANK.OUT","w",stdout);
   ios::sync_with_stdio(false);
   cin>>m>>n;
   FOR(i,1,m)
   {
       int pi,ci;
       cin>>pi>>ci;
       p[pi]++;
       a[pi].push_back(ci);
   }

   FOR(i,0,m/2+1)
   {
       res=min(res,XULY(i));
   }

   cout<<res;

}


#include <iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<sstream>
#include<iomanip>
#include<vector>

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define DB(x) cout<<x<<endl;
#define big  32770
#define mod 1000000000
#define ll long long

using namespace std;

const int N=3005;

int m,n;
vector<int> a[3005];
vector<int> s;

int p[3005];
ll res=LLONG_MAX;

long long XULY(int l)
{
    ll c=0;
    int sophieu=0;
    s.clear();
    FOR(i,2,n)
    {
        if (p[i]>l)
        {
            FOR(j,1,p[i]-l) c=c+a[i][j-1],sophieu++;
            FOR(j,p[i]-l+1,p[i]) s.push_back(a[i][j-1]);
        }
        else
        {
            FOR(j,1,p[i]) s.push_back(a[i][j-1]);
        }
    }

    if (sophieu+p[1]>l) return c;
    if (s.size()+sophieu+p[1]<=l) return LLONG_MAX;

    sort(s.begin(),s.end());
    FOR(i,1,l-sophieu-p[1]+1) c=c+s[i-1];
    return c;

}

int main()
{
  freopen("text.inp","r",stdin);
  //freopen("BANK.OUT","w",stdout);
   ios::sync_with_stdio(false);
   cin>>m>>n;
   FOR(i,1,m)
   {
       int pi,ci;
       cin>>pi>>ci;
       p[pi]++;
       a[pi].push_back(ci);
   }

   FOR(i,0,m/2+1)
   {
       res=min(res,XULY(i));
   }

   cout<<res;

}


#include <iostream>
#include<fstream>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<sstream>
#include<iomanip>
#include<vector>

#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define DEBUG(x) { cerr << #x << '=' << x << endl; }
#define DB(x) cout<<x<<endl;
#define big  32770
#define mod 1000000000
#define ll long long

using namespace std;

const int N=3005;

int m,n;
vector<int> a[3005];
vector<int> s;

int p[3005];
ll res=LLONG_MAX;

long long XULY(int l)
{
    ll c=0;
    int sophieu=0;
    s.clear();
    FOR(i,2,n)
    {
        if (p[i]>l)
        {
            FOR(j,1,p[i]-l) c=c+a[i][j-1],sophieu++;
            FOR(j,p[i]-l+1,p[i]) s.push_back(a[i][j-1]);
        }
        else
        {
            FOR(j,1,p[i]) s.push_back(a[i][j-1]);
        }
    }

    if (sophieu+p[1]>l) return c;
    if (s.size()+sophieu+p[1]<=l) return LLONG_MAX;

    sort(s.begin(),s.end());
    FOR(i,1,l-sophieu-p[1]+1) c=c+s[i-1];
    return c;

}

int main()
{
  //freopen("text.inp","r",stdin);
  //freopen("BANK.OUT","w",stdout);
   ios::sync_with_stdio(false);
   cin>>m>>n;
   FOR(i,1,m)
   {
       int pi,ci;
       cin>>pi>>ci;
       p[pi]++;
       a[pi].push_back(ci);
   }

   FOR(i,0,m/2+1)
   {
       res=min(res,XULY(i));
   }

   cout<<res;

}


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

typedef long long llt;
typedef pair <llt, int> pli;

const int MAXN = 3000, MAXM = 3000;
const llt INF = 0x7F7F7F7F7F7F7F7F;

struct Voter { int P; llt c; };

int N, M;
Voter A[MAXN+1];
Voter AA[MAXN+1];
int v[MAXM+1];
vector <pli> arr[MAXM+1];
llt ans = INF;

bool cmp( pli x, pli y ) { return x.first < y.first; }

bool cmp2( Voter x, Voter y ) { return x.c < y.c; }

void init()
{
    scanf( "%d %d", &N, &M );
    for ( int i = 1; i <= N; ++i )
    {
        scanf( "%d %I64d", &A[i].P, &A[i].c );
        v[ A[i].P ]++;
        arr[ A[i].P ].push_back( make_pair( A[i].c, i ) );
        if ( A[i].P == 1 ) A[i].c = INF;
    }
    for ( int i = 1; i <= M; ++i )
        sort( arr[i].begin(), arr[i].end(), cmp );
}

void solve()
{
    for ( int i = v[1]; i <= M; ++i ) //for all votes
    {
        for ( int j = 1; j <= N; ++j ) AA[j] = A[j];
        llt res = 0; int m = v[1];
        for ( int j = 2; j <= M; ++j )
            if ( v[j] >= i )
            {
                int v2 = v[j];
                for ( int k = 0; k < v[j]; ++k )
                {
                    if ( m > v2 ) break;
                    res += arr[j][k].first;
                    AA[ arr[j][k].second ].c = INF;
                    m++;
                    v2--;
                }
            }
        if ( m < i )
        {
            sort( AA + 1, AA + 1 + N, cmp2 );
            for ( int j = 1; j <= i - m; ++j )
                res += AA[j].c;
        }
        ans = min( ans, res );
    }
    cout << ans << endl;
}

int main()
{
    init();
    solve();
    return 0;
}


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

typedef long long llt;
typedef pair <llt, int> pli;

const int MAXN = 3000, MAXM = 3000;
const llt INF = 0x7F7F7F7F7F7F;

struct Voter { int P; llt c; };

int N, M;
Voter A[MAXN+1];
int v[MAXM+1];
vector <pli> arr[MAXM+1];
priority_queue < llt, vector<llt>, greater<llt> > pq;
llt ans = INF;
bool o[MAXN+1];

bool cmp( pli x, pli y ) { return x.first < y.first; }

void init()
{
    scanf( "%d %d", &N, &M );
    for ( int i = 1; i <= N; ++i )
    {
        scanf( "%d %I64d", &A[i].P, &A[i].c );
        v[ A[i].P ]++;
        arr[ A[i].P ].push_back( make_pair( A[i].c, i ) );
    }
    for ( int i = 1; i <= M; ++i )
        sort( arr[i].begin(), arr[i].end(), cmp );
}

void solve()
{
    for ( int i = v[1]; i <= N; ++i ) //for all votes
    {
        memset( o, 0, sizeof o );
        llt res = 0; int m = v[1];
        for ( int j = 2; j <= M; ++j )
            if ( v[j] >= i )
            {
                int v2 = v[j];
                for ( int k = 0; k < v[j]; ++k )
                {
                    if ( v2 < i ) break;
                    res += arr[j][k].first;
                    o[ arr[j][k].second ] = true;
                    m++;
                    v2--;
                }
            }
        while ( !pq.empty() ) pq.pop();
        for ( int i = 1; i <= N; ++i )
            if ( A[i].P != 1 && !o[i] ) pq.push( A[i].c );
        while ( m < i )
        {
            res += pq.top();
            pq.pop();
            m++;
        }
        ans = min( ans, res );
    }
    cout << ans << endl;
}

int main()
{
    init();
    solve();
    return 0;
}


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

typedef long long llt;
typedef pair <llt, int> pli;

const int MAXN = 3000, MAXM = 3000;
const llt INF = 0x7F7F7F7F7F7F7F7F;

struct Voter { int P; llt c; };

int N, M;
Voter A[MAXN+1];
Voter AA[MAXN+1];
int v[MAXM+1];
vector <pli> arr[MAXM+1];
llt ans = INF;

bool cmp( pli x, pli y ) { return x.first < y.first; }

bool cmp2( Voter x, Voter y ) { return x.c < y.c; }

void init()
{
    scanf( "%d %d", &N, &M );
    for ( int i = 1; i <= N; ++i )
    {
        scanf( "%d %I64d", &A[i].P, &A[i].c );
        v[ A[i].P ]++;
        arr[ A[i].P ].push_back( make_pair( A[i].c, i ) );
        if ( A[i].P == 1 ) A[i].c = INF;
    }
    for ( int i = 1; i <= M; ++i )
        sort( arr[i].begin(), arr[i].end(), cmp );
}

void solve()
{
    for ( int i = v[1]; i <= M; ++i ) //for all votes
    {
        for ( int j = 1; j <= N; ++j ) AA[j] = A[j];
        llt res = 0; int m = v[1];
        for ( int j = 2; j <= M; ++j )
            if ( v[j] >= i )
            {
                int v1 = i, v2 = v[j];
                for ( int k = 0; k < arr[j].size(); ++k )
                {
                    if ( v1 > v2 ) break;
                    res += arr[j][k].first;
                    AA[ arr[j][k].second ].c = INF;
                    m++;
                    v1++;
                    v2--;
                }
            }
        if ( m < i )
        {
            sort( AA + 1, AA + 1 + N, cmp2 );
            for ( int j = 1; j <= i - m; ++j )
                res += AA[j].c;
        }
        ans = min( ans, res );
    }
    cout << ans << endl;
}

int main()
{
    init();
    solve();
    return 0;
}


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

typedef long long llt;
typedef pair <llt, int> pli;

const int MAXN = 3000, MAXM = 3000;
const llt INF = 0x7F7F7F7F7F7F;

struct Voter { int P; llt c; };

int N, M;
Voter A[MAXN+1];
Voter AA[MAXN+1];
int v[MAXM+1];
vector <pli> arr[MAXM+1];
llt ans = INF;

bool cmp( pli x, pli y ) { return x.first < y.first; }

bool cmp2( Voter x, Voter y ) { return x.c < y.c; }

void init()
{
    scanf( "%d %d", &N, &M );
    for ( int i = 1; i <= N; ++i )
    {
        scanf( "%d %I64d", &A[i].P, &A[i].c );
        v[ A[i].P ]++;
        arr[ A[i].P ].push_back( make_pair( A[i].c, i ) );
        if ( A[i].P == 1 ) A[i].c = INF;
    }
    for ( int i = 1; i <= M; ++i )
        sort( arr[i].begin(), arr[i].end(), cmp );
}

void solve()
{
    for ( int i = v[1]; i <= M; ++i ) //for all votes
    {
        for ( int j = 1; j <= N; ++j ) AA[j] = A[j];
        llt res = 0; int m = v[1];
        for ( int j = 2; j <= M; ++j )
            if ( v[j] >= i )
            {
                int v2 = v[j];
                for ( int k = 0; k < v[j]; ++k )
                {
                    if ( v2 < i ) break;
                    res += arr[j][k].first;
                    AA[ arr[j][k].second ].c = INF;
                    m++;
                    v2--;
                }
            }
        if ( m < i )
        {
            sort( AA + 1, AA + 1 + N, cmp2 );
            for ( int j = 1; j <= i - m; ++j )
                res += AA[j].c;
        }
        ans = min( ans, res );
    }
    cout << ans << endl;
}

int main()
{
    init();
    solve();
    return 0;
}


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

typedef long long llt;
typedef pair <llt, int> pli;

const int MAXN = 3000, MAXM = 3000;
const llt INF = 0x7F7F7F7F7F7F7F7F;

struct Voter { int P; llt c; };

int N, M;
Voter A[MAXN+1];
Voter AA[MAXN+1];
int v[MAXM+1];
vector <pli> arr[MAXM+1];
llt ans = INF;

bool cmp( pli x, pli y ) { return x.first < y.first; }

bool cmp2( Voter x, Voter y ) { return x.c < y.c; }

void init()
{
    scanf( "%d %d", &N, &M );
    for ( int i = 1; i <= N; ++i )
    {
        scanf( "%d %I64d", &A[i].P, &A[i].c );
        v[ A[i].P ]++;
        arr[ A[i].P ].push_back( make_pair( A[i].c, i ) );
        if ( A[i].P == 1 ) A[i].c = INF;
    }
    for ( int i = 1; i <= M; ++i )
        sort( arr[i].begin(), arr[i].end(), cmp );
}

void solve()
{
    for ( int i = v[1]; i <= M; ++i ) //for all votes
    {
        for ( int j = 1; j <= N; ++j ) AA[j] = A[j];
        llt res = 0; int m = v[1];
        for ( int j = 2; j <= M; ++j )
            if ( v[j] >= i )
            {
                for ( int k = 0; k <= ( v[j] - i ) / 2; ++k )
                {
                    res += arr[j][k].first;
                    AA[ arr[j][k].second ].c = INF;
                    m++;
                }
            }
        if ( m < i )
        {
            sort( AA + 1, AA + 1 + N, cmp2 );
            for ( int j = 1; j <= i - m; ++j )
                res += AA[j].c;
        }
        ans = min( ans, res );
    }
    cout << ans << endl;
}

int main()
{
    init();
    solve();
    return 0;
}


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

typedef long long llt;
typedef pair <llt, int> pli;

const int MAXN = 3000, MAXM = 3000;
const llt INF = 0x7F7F7F7F7F7F7F7F;

struct Voter { int P; llt c; };

int N, M;
Voter A[MAXN+1];
Voter AA[MAXN+1];
int v[MAXM+1];
vector <pli> arr[MAXM+1];
llt ans = INF;

bool cmp( pli x, pli y ) { return x.first < y.first; }

bool cmp2( Voter x, Voter y ) { return x.c < y.c; }

void init()
{
    scanf( "%d %d", &N, &M );
    for ( int i = 1; i <= N; ++i )
    {
        scanf( "%d %I64d", &A[i].P, &A[i].c );
        v[ A[i].P ]++;
        arr[ A[i].P ].push_back( make_pair( A[i].c, i ) );
        if ( A[i].P == 1 ) A[i].c = INF;
    }
    for ( int i = 1; i <= M; ++i )
        sort( arr[i].begin(), arr[i].end(), cmp );
}

void solve()
{
    for ( int i = v[1]; i <= M; ++i ) //for all votes
    {
        for ( int j = 1; j <= N; ++j ) AA[j] = A[j];
        llt res = 0; int m = v[1];
        for ( int j = 2; j <= M; ++j )
            if ( v[j] >= i )
            {
                int v2 = v[j];
                for ( int k = 0; k < v[j]; ++k )
                {
                    if ( v2 < i ) break;
                    res += arr[j][k].first;
                    AA[ arr[j][k].second ].c = INF;
                    m++;
                    v2--;
                }
            }
        if ( m < i )
        {
            sort( AA + 1, AA + 1 + N, cmp2 );
            for ( int j = 1; j <= i - m; ++j )
                res += AA[j].c;
        }
        ans = min( ans, res );
    }
    cout << ans << endl;
}

int main()
{
    init();
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll a[3333],c[3333],p[3333];
int main()
{
    ll n,m,i,j,k,maxi=0,num,d;
    cin>>n>>m;
    for(i=0 ; i<n ; i++)
    {
        cin>>c[i]>>p[i];
        a[c[i]]++;
        if(a[c[i]]>maxi)
        {
            maxi=a[c[i]];
            num=c[i];
        }
    }
    if(num==1)
    {
        cout<<0;
        return 0;
    }
    vector<ll> left,right,res;
    for(i=0 ; i<n ; i++)
    {
        if(c[i]==num)
            right.push_back(p[i]);
        else
            left.push_back(p[i]);
    }
    sort(left.begin(),left.end());
    sort(right.begin(),right.end());
    for(i=0 ; i<=right.size() ; i++)
    {
        k=i;
        d=a[num];
        if(i==0)
            d++;
        ll sum=0;
        while(k--)
            {
                sum+=right[k];
                d--;
            }
        while(d--)
        {
            sum+=left[d];
        }
        res.push_back(sum);
    }
    sort(res.begin(),res.end());
    cout<<res[0]<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll a[3333],c[3333],p[3333],e[3333],s[3333];
int main()
{
    ll n,m,i,j,k,maxi=0,num,d;
    cin>>n>>m;
    for(i=0 ; i<n ; i++)
    {
        cin>>c[i]>>p[i];
        a[c[i]]++;
        s[c[i]]++;
        if(a[c[i]]>=maxi)
        {
            maxi=a[c[i]];
            num=c[i];
        }
    }
    if(num==1)
    {
        cout<<0;
        return 0;
    }
    ll w=0;
    for(i=0 ; i<n ; i++)
    {
        if(e[c[i]])
            continue;
        e[c[i]]=1;
        if(a[c[i]]==a[num])
            w++;
    }
    vector<ll> left,right,res;
    if(w==1)
    {
    for(i=0 ; i<n ; i++)
    {
        if(c[i]==num)
            right.push_back(p[i]);
        else
            left.push_back(p[i]);
    }
    sort(left.begin(),left.end());
    sort(right.begin(),right.end());
    for(i=0 ; i<=right.size() ; i++)
    {
        k=i;
        d=a[num];
        if(i==0)
            d++;
        ll sum=0;
        while(k--)
            {
                sum+=right[k];
                d--;
            }
        while(d--)
        {
            sum+=left[d];
        }
        res.push_back(sum);
    }
    sort(res.begin(),res.end());
    cout<<res[0]<<endl;
    }
    else
    {
        ll u=a[num]-1;
        ll res2=0;
        cout<<u<<endl;
        for(i=0 ; i<n ;i++)
        {
            if(a[c[i]]==a[num])
                if(s[c[i]]>=u)
                {
                    s[c[i]]--;
                    //cout<<p[i]<<"+";
                    res2+=p[i];
                }
        }
       // cout<<"=";
        cout<<res2<<endl;
    }
    return 0;
}


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

typedef long long ll;
ll n,m;
ll p[3005], c[3005], x[3005], v[3005];
int main()
{
  cin>>n>>m;
  for(int i=1 ; i<=n; i++)
    cin>>p[i]>>c[i];
  for(int i=1; i<=n ; i++)
  {
    for(int j=i+1 ; j<=n ; j++)
    {
      if(c[i]>c[j])
      {
        swap(p[i],p[j]);
        swap(c[i],c[j]);
      }
    }
  }
  ll ans=1e18;
  for(int i=0 ; i<=n ; i++)
  {
    ll sum=0;
    memset(x,0,sizeof x);
    memset(v,0,sizeof v);
    for(int j=n ; j>=1 ; j--)
    {
      if(x[p[j]]<i || p[j]==1)
      {
        x[p[j]]++;
      }
      else
      {
        sum += c[j];
        x[1]++;
        v[j] = 1;
      }
    }
    for(int j=1 ; j<=n ; j++)
    {
      if(x[1] <= i && p[j]!=1 && !v[j])
      {
        sum += c[j];
        x[1]++;
        v[j] = 1;
      }
    }
    if(x[1]>i)
      ans=min(ans, sum);
  }
  cout<<ans<<endl;
return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll a[3333],c[3333],p[3333],e[3333];
int main()
{
    ll n,m,i,j,k,maxi=0,num,d;
    cin>>n>>m;
    for(i=0 ; i<n ; i++)
    {
        cin>>c[i]>>p[i];
        a[c[i]]++;
        if(a[c[i]]>=maxi)
        {
            maxi=a[c[i]];
            num=c[i];
        }
    }
    if(num==1)
    {
        cout<<0;
        return 0;
    }
    ll w=0;
    for(i=0 ; i<n ; i++)
    {
        if(e[c[i]])
            continue;
        e[c[i]]=1;
        if(a[c[i]]==a[num])
            w++;
    }
    vector<ll> left,right,res;
    if(w==1)
    {
    for(i=0 ; i<n ; i++)
    {
        if(c[i]==num)
            right.push_back(p[i]);
        else
            left.push_back(p[i]);
    }
    sort(left.begin(),left.end());
    sort(right.begin(),right.end());
    for(i=0 ; i<=right.size() ; i++)
    {
        k=i;
        d=a[num];
        if(i==0)
            d++;
        ll sum=0;
        cout<<k<<" "<<d<<endl;
        while(k--)
            {
                sum+=right[k];
                d--;
            }
        while(d--)
        {
            sum+=left[d];
        }
        res.push_back(sum);
    }
    sort(res.begin(),res.end());
    for(i=0 ; i<res.size() ; i++)
        cout<<res[i]<<" ";
    }
    else
    {
        ll u=a[num]-1;
        ll res2=0;
        for(i=0 ; i<n ;i++)
        {
            if(a[c[i]]==a[num])
                if(a[c[i]]>u)
                {
                    a[c[i]]--;
                    //cout<<p[i]<<"+";
                    res2+=p[i];
                }
        }
        //cout<<"=";
        cout<<res2<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
ll a[3333],c[3333],p[3333],e[3333];
int main()
{
    ll n,m,i,j,k,maxi=0,num,d;
    cin>>n>>m;
    for(i=0 ; i<n ; i++)
    {
        cin>>c[i]>>p[i];
        a[c[i]]++;
        if(a[c[i]]>=maxi)
        {
            maxi=a[c[i]];
            num=c[i];
        }
    }
    if(num==1)
    {
        cout<<0;
        return 0;
    }
    ll w=0;
    for(i=0 ; i<n ; i++)
    {
        if(e[c[i]])
            continue;
        e[c[i]]=1;
        if(a[c[i]]==a[num])
            w++;
    }
    vector<ll> left,right,res;
    if(w==1)
    {
    for(i=0 ; i<n ; i++)
    {
        if(c[i]==num)
            right.push_back(p[i]);
        else
            left.push_back(p[i]);
    }
    sort(left.begin(),left.end());
    sort(right.begin(),right.end());
    for(i=0 ; i<=right.size() ; i++)
    {
        k=i;
        d=a[num];
        if(i==0)
            d++;
        ll sum=0;
        while(k--)
            {
                sum+=right[k];
                d--;
            }
        while(d--)
        {
            sum+=left[d];
        }
        res.push_back(sum);
    }
    sort(res.begin(),res.end());
    cout<<res[0]<<endl;
    }
    else
    {
        ll u=a[num]-1;
        ll res2=0;
        for(i=0 ; i<n ;i++)
        {
            if(a[c[i]]==a[num])
                if(a[c[i]]>u)
                {
                    a[c[i]]--;
                    //cout<<p[i]<<"+";
                    res2+=p[i];
                }
        }
        //cout<<"=";
        cout<<res2<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn=3005;
pair <int, int > pi[maxn];
int s[maxn], s1[maxn];
int use[maxn];
int one;

int main () {
    int n, m;
    scanf("%d%d", &n ,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d%d", &pi[i].second, &pi[i].first);
        if(pi[i].second==1) one++;
        s[pi[i].second]++;
    }
    sort(pi+1, pi+1+n);
    LL ans=LONG_LONG_MAX;
    for(int t=0;t<=n;t++) {
        memset(use, 0, sizeof(use));
        for(int i=0;i<=m;i++) s1[i]=s[i];
        int cnt=0;
        LL sm=0;
        for(int i=1;i<=n;i++) {
            if(pi[i].second==1||s1[pi[i].second]<t) continue;
            cnt++, sm+=pi[i].first, use[i]=1, s1[pi[i].second]--;
        }
        for(int i=1;i<=n;i++) {
            if(pi[i].second==1||use[i]) continue;
            if(cnt+one<t) cnt++, sm+=pi[i].first;
        }
        ans=min(ans, sm);
    }
    printf("%lld\n", ans);
    return 0;
}
/**

5 5
1 7
3 3
2 7
2 4
1 2

*/


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn=3005;
pair <int, int > pi[maxn];
int s[maxn], s1[maxn];
int use[maxn];
int one;

int main () {
    int n, m;
    scanf("%d%d", &n ,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d%d", &pi[i].first, &pi[i].second);
        if(pi[i].first==1) one++;
        s[pi[i].first]++;
    }
    sort(pi+1, pi+1+n);
    LL ans=LONG_LONG_MAX;
    for(int t=0;t<=n;t++) {
        memset(use, 0, sizeof(use));
        for(int i=0;i<=m;i++) s1[i]=s[i];
        int cnt=0;
        LL sm=0;
        for(int i=1;i<=n;i++) {
            if(pi[i].first==1||s1[pi[i].first]<t) continue;
            cnt++, sm+=pi[i].second, use[i]=1, s1[pi[i].first]--;
        }
        for(int i=1;i<=n;i++) {
            if(pi[i].first==1||cnt+one>=t||use[i]) continue;
            cnt++, sm+=pi[i].second;
        }
        ans=min(ans, sm);
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn=3005;
pair <int, int > pi[maxn];
int s[maxn], s1[maxn];
int use[maxn];
int one;

int main () {
    int n, m;
    scanf("%d%d", &n ,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d%d", &pi[i].first, &pi[i].second);
        if(pi[i].first==1) one++;
        s[pi[i].first]++;
    }
    sort(pi+1, pi+1+n);
    LL ans=LONG_LONG_MAX;
    for(int t=1;t<=n;t++) {
        memset(use, 0, sizeof(use));
        for(int i=0;i<=m;i++) s1[i]=s[i];
        int cnt=0;
        LL sm=0;
        for(int i=1;i<=n;i++) {
            if(pi[i].first==1||s1[pi[i].first]<t) continue;
            cnt++, sm+=pi[i].second, use[i]=1, s1[pi[i].first]--;
        }
        for(int i=1;i<=n;i++) {
            if(pi[i].first==1||cnt+one>t||use[i]) continue;
            cnt++, sm+=pi[i].second;
        }
        ans=min(ans, sm);
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn=3005;
pair <int, int > pi[maxn];
int s[maxn], s1[maxn];
int use[maxn];
int one;

int main () {
    int n, m;
    scanf("%d%d", &n ,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d%d", &pi[i].second, &pi[i].first);
        if(pi[i].second==1) one++;
        s[pi[i].second]++;
    }
    sort(pi+1, pi+1+n);
    LL ans=LONG_LONG_MAX;
    for(int t=0;t<=n;t++) {
        memset(use, 0, sizeof(use));
        for(int i=0;i<=m;i++) s1[i]=s[i];
        int cnt=0;
        LL sm=0;
        for(int i=1;i<=n;i++) {
            if(pi[i].second==1||s1[pi[i].second]<t) continue;
            cnt++, sm+=pi[i].first, use[i]=1, s1[pi[i].second]--;
        }
        for(int i=1;i<=n;i++) {
            if(pi[i].second==1||use[i]) continue;
            if(s1[pi[i].second]>=cnt+one) cnt++, sm+=pi[i].first;
        }
        ans=min(ans, sm);
    }
    printf("%lld\n", ans);
    return 0;
}
/**

5 5
1 7
3 3
2 7
2 4
1 2

*/


#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int maxn=3005;
pair <int, int > pi[maxn];
int s[maxn], s1[maxn];
int use[maxn];
int one;

int main () {
    int n, m;
    scanf("%d%d", &n ,&m);
    for(int i=1;i<=n;i++) {
        scanf("%d%d", &pi[i].first, &pi[i].second);
        if(pi[i].first==1) one++;
        s[pi[i].first]++;
    }
    sort(pi+1, pi+1+n);
    LL ans=LONG_LONG_MAX;
    for(int t=1;t<=n;t++) {
        memset(use, 0, sizeof(use));
        for(int i=0;i<=m;i++) s1[i]=s[i];
        int cnt=0;
        LL sm=0;
        for(int i=1;i<=n;i++) {
            if(pi[i].first==1||s1[pi[i].first]<t) continue;
            cnt++, sm+=pi[i].second, use[i]=1, s1[pi[i].first]--;
        }
        for(int i=1;i<=n;i++) {

            if(pi[i].first==1||cnt+one>=t||use[i]) continue;
            cnt++, sm+=pi[i].second;
        }
        ans=min(ans, sm);
    }
    printf("%lld\n", ans);
    return 0;
}