#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
        }
        if(curr<i)
        {
            sort(v.begin(),v.end());
            /*for(j=0;j<v.size();j++)
            {
                cout<<v[j].second<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size() && k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].first;
                }
            }
        }
        else
        {
            k=0;
        }
        if(k==0)
        {
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size()&& j<k;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    te += v[j].first;
                }
            }
            curr = min(curr+(ll)v.size(),i);
        }
        else
        {
            k=0;
        }
        if(curr>=i)
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
priority_queue <ll,vector <ll>,greater <int> > q[3001];
ll m;

pair <ll,int> getMax()
{
   ll mm = INT_MIN;
   ll id = 0;
   for(int i=2;i<=m;i++)
   {
       if(q[i].size()>mm)
       {
           id = i;
           mm = q[i].size();
       }
   }
   pair <ll,int> pa;
   pa.first = mm;
   pa.second = id;
   return pa;
}

pair <ll,int> getMin()
{
    ll mm = INT_MAX;
    ll id = 0;
    for(int i=2;i<=m;i++)
    {
        if(q[i].top()<mm)
        {
            id = i;
            mm = q[i].top();
        }
    }
    pair <ll,int> pa;
    pa.first = mm;
    pa.second = id;
    return pa;
}

ll getCount(ll n)
{
    int t = 0;
    for(int i = 2;i<=m;i++)
    {
        if(q[i].size()==n)
            t++;
    }
    return t;
}
int main()
{
    ll n,i,j,p,c,midx,mm = INT_MIN;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        cin>>p>>c;
        q[p].push(c);
    }
    /*for(i=2;i<=m;i++)
    {
        while(!q[i].empty())
        {
            cout<<q[i].top()<<" ";
            q[i].pop();
        }
        cout<<endl;
    }*/
    ll curr = q[1].size();
    ll ans = 0;
    ll maxfre = 0;
    pair <ll,int> pa;
    pair <ll,int> pp;
    while(1)
    {
        curr = q[1].size();
        //cout<<curr<<endl;
        pa = getMax();
        mm = pa.first;
        midx = pa.second;
        //cout<<mm<<" "<<midx<<endl;
        maxfre = getCount(mm);
        //cout<<maxfre<<endl;
        if((curr+1)<mm)
        {
            pp = getMin();
            //cout<<pp.first<<" "<<pp.second<<endl;
            ans += pp.first;
            q[(pp.second)].pop();
            q[1].push((pp.first));
            //cout<<q[pp.second].size()<<" "<<q[1].size()<<endl;
        }
        else if((curr+1)==mm)
        {

            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());
            /*for(i=0;i<v.size();i++)
            {
                cout<<v[i].first<<" "<<v[i].second<<endl;
            }*/
            if(maxfre==1)
            {
                ll temp = 0;
                j=0;
                for(i=0;i<v.size() && j<2;i++)
                {
                    if(v[i].second != midx)
                    {
                        j++;
                        temp += v[i].first;
                    }
                }
                if(temp <= q[midx].top())
                {
                    ans += temp;
                }
                else
                {
                    ans += q[midx].top();
                }
                break;
            }
            else
            {
                for(i=0;i<2;i++)
                {
                    ans += v[i].first;
                }
                break;
            }
        }
        else if(curr==mm)
        {
            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());
            ans += v[0].first;
            break;
        }
        else
        {
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size() && k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].first;
                }
            }
        }
        else
        {
            k=0;
        }
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size()&&k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].first;
                }
            }
            curr = min(curr+(ll)v.size(),i);
        }
        else
        {
            k=0;
        }
        if(curr>=i)
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size() && k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].first;
                }
            }
        }
        else
        {
            k=0;
        }
        if(k==0)
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].first >> v[i].second ;
        q[v[i].first].push_back(make_pair(v[i].second,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }

        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
        }
        if(curr<i)
        {
            sort(v.begin(),v.end());
            k=i-curr;
            for(j=0;j<v.size() && k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].second;
                }
            }
        }
        else
        {
            k=0;
        }
        if(k==0)
        {
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size() && k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].first;
                }
            }
        }
        else
        {
            k=0;
        }
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
priority_queue <ll,vector <ll>,greater <int> > q[3001];
ll m;

pair <ll,int> getMax()
{
   ll mm = INT_MIN;
   ll id = 0;
   for(int i=2;i<=m;i++)
   {
       if(q[i].size()>mm)
       {
           id = i;
           mm = q[i].size();
       }
   }
   pair <ll,int> pa;
   pa.first = mm;
   pa.second = id;
   return pa;
}

pair <ll,int> getMin()
{
    ll mm = INT_MAX;
    ll id = 0;
    for(int i=2;i<=m;i++)
    {
        if(q[i].top()<mm)
        {
            id = i;
            mm = q[i].top();
        }
    }
    pair <ll,int> pa;
    pa.first = mm;
    pa.second = id;
    return pa;
}

ll getCount(ll n)
{
    int t = 0;
    for(int i = 2;i<=m;i++)
    {
        if(q[i].size()==n)
            t++;
    }
    return t;
}
int main()
{
    ll n,i,j,p,c,midx,mm = INT_MIN;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        cin>>p>>c;
        q[p].push(c);
    }
    /*for(i=2;i<=m;i++)
    {
        while(!q[i].empty())
        {
            cout<<q[i].top()<<" ";
            q[i].pop();
        }
        cout<<endl;
    }*/
    ll curr = q[1].size();
    ll ans = 0;
    ll maxfre = 0;
    pair <ll,int> pa;
    pair <ll,int> pp;
    while(1)
    {
        curr = q[1].size();
        //cout<<curr<<endl;
        pa = getMax();
        mm = pa.first;
        midx = pa.second;
        //cout<<mm<<" "<<midx<<endl;
        maxfre = getCount(mm);
        //cout<<maxfre<<endl;
        if((curr+1)<mm)
        {
            pp = getMin();
            //cout<<pp.first<<" "<<pp.second<<endl;
            ans += pp.first;
            q[(pp.second)].pop();
            q[1].push((pp.first));
            //cout<<q[pp.second].size()<<" "<<q[1].size()<<endl;
        }
        else if((curr+1)==mm)
        {

            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());
            /*for(i=0;i<v.size();i++)
            {
                cout<<v[i].first<<" "<<v[i].second<<endl;
            }*/
            if(maxfre==1)
            {
                ll temp = 0;
                j=0;
                for(i=0;i<v.size() && j<2;i++)
                {
                    if(v[i].second != midx)
                    {
                        j++;
                        temp += v[i].first;
                    }
                }
                if(temp <= q[midx].top())
                {
                    ans += temp;
                }
                else
                {
                    ans += q[midx].top();
                }
                break;
            }
            else
            {
                for(i=0;i<2;i++)
                {
                    ans += v[i].first;
                }
                break;
            }
        }
        else
        {
            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());
            ans += v[0].first;
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            k=i-curr;
            int s = 0;
            for(j=0;j<v.size() && curr<i;j++)
            {
                if(vis[j]==false)
                {
                    k--;
                    te += v[j].first;
                    //cout << v[j].first << " ";
                    curr++;
                }
            }
        }
        //cout<<endl;
        if(curr>=i)
            ans = min(ans,te);
        //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
priority_queue <ll,vector <ll>,greater <ll> > q[3001];
ll m;

pair <ll,int> getMax()
{
   ll mm = INT_MIN;
   ll id = 0;
   for(int i=2;i<=m;i++)
   {
       if(q[i].size()>mm)
       {
           id = i;
           mm = q[i].size();
       }
   }
   pair <ll,int> pa;
   pa.first = mm;
   pa.second = id;
   return pa;
}

pair <ll,int> getMin()
{
    ll mm = INT_MAX;
    ll id = 0;
    for(int i=2;i<=m;i++)
    {
        if(q[i].top()<mm)
        {
            id = i;
            mm = q[i].top();
        }
    }
    pair <ll,int> pa;
    pa.first = mm;
    pa.second = id;
    return pa;
}

ll getCount(ll n)
{
    int t = 0;
    for(int i = 2;i<=m;i++)
    {
        if(q[i].size()==n)
            t++;
    }
    return t;
}
int main()
{
    ll n,i,j,p,c,midx,mm = INT_MIN;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        cin>>p>>c;
        q[p].push(c);
    }
    /*for(i=2;i<=m;i++)
    {
        while(!q[i].empty())
        {
            cout<<q[i].top()<<" ";
            q[i].pop();
        }
        cout<<endl;
    }*/
    ll curr = q[1].size();
    ll ans = 0;
    ll maxfre = 0;
    ll te = 0;
    pair <ll,int> pma;
    pair <ll,int> pmi;
    while(1)
    {
        curr = q[1].size();
        pma = getMax();
        mm = pma.first;
        midx = pma.second;
        maxfre = getCount(mm);
        vector <pair<ll,int> > v;
        vector <pair<ll,int> > temp;
        for(i=2;i<=m;i++)
        {
            if(q[i].size()>0)
                v.push_back(make_pair(q[i].top(),i));
        }
        sort(v.begin(),v.end());
        if(curr>mm)
        {
            break;
        }
        else if(curr==mm)
        {
            ans += v[0].first;
            q[1].push(v[0].first);
            q[v[0].second].pop();
        }
        else if((curr+1)==mm)
        {
            vector <pair<ll,int> > my;
            vector <pair<ll,int> > dd;
            priority_queue <ll,vector<ll>,greater<ll> > qq;
            for(i=2;i<=m;i++)
            {
                qq = q[i];
                while(!qq.empty())
                {
                    my.push_back(make_pair(qq.top(),i));
                    qq.pop();
                }
            }
            sort(my.begin(),my.end());
            j=my.size();
            te = 0;
            for(i=0;i<(min(j,(ll)2));i++)
            {
                dd.push_back(my[i]);
                te += my[i].first;
            }
            if(maxfre==1)
            {
                if(q[midx].top()<=te)
                {
                    ans += q[midx].top();
                    q[1].push(q[midx].top());
                    q[midx].pop();
                }
                else
                {
                    ans += te;
                    for(i=0;i<dd.size();i++)
                    {
                        q[1].push(dd[i].first);
                        q[dd[i].second].pop();
                    }
                }
            }
            else
            {
                ans += te;
                for(i=0;i<dd.size();i++)
                {
                    q[1].push(dd[i].first);
                    q[dd[i].second].pop();
                }
            }
        }
        else if((curr+1)<mm)
        {
            ans += v[0].first;
            q[1].push(v[0].first);
            q[v[0].second].pop();
        }
        else
        {
            //okok
        }
    }
    /*while(1)
    {
        curr = q[1].size();
        //cout<<curr<<endl;
        pa = getMax();
        mm = pa.first;
        midx = pa.second;
        //cout<<mm<<" "<<midx<<endl;
        maxfre = getCount(mm);
        //cout<<maxfre<<endl;
        if((curr+1)<mm)
        {
            pp = getMin();
            //cout<<pp.first<<" "<<pp.second<<endl;
            ans += pp.first;
            q[(pp.second)].pop();
            q[1].push((pp.first));
            //cout<<q[pp.second].size()<<" "<<q[1].size()<<endl;
        }
        else if((curr+1)==mm)
        {

            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());

            if(maxfre==1)
            {
                vector <pair<ll,int> > temp;
                j=v.size();
                for(i=0;i<(j,(ll)2);i++)
                {
                    temp.push_back(v[i]);
                }
                ll te = 0;
                for(i=0;i<temp.size();i++)
                {
                    te += temp[i].first;
                }
                if(te <= q[midx].top())
                {
                    for(i=0;i<temp.size();i++)
                    {
                        q[1].push(temp[i].first);
                        q[temp[i].second].pop();
                    }

                }
                else
                {
                    ans += q[midx].top();
                    q[1].push(q[midx].top());
                    q[midx].pop();
                }
            }
            else
            {
                for(i=0;i<2;i++)
                {
                    ans += v[i].first;
                }
                break;
            }
        }
        else if(curr==mm)
        {
            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());
            ans += v[0].first;
            break;
        }
        else
        {
            break;
        }
    }*/
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
priority_queue <ll,vector <ll>,greater <int> > q[3001];
ll m;

pair <ll,int> getMax()
{
   ll mm = INT_MIN;
   ll id = 0;
   for(int i=2;i<=m;i++)
   {
       if(q[i].size()>mm)
       {
           id = i;
           mm = q[i].size();
       }
   }
   pair <ll,int> pa;
   pa.first = mm;
   pa.second = id;
   return pa;
}

pair <ll,int> getMin()
{
    ll mm = INT_MAX;
    ll id = 0;
    for(int i=2;i<=m;i++)
    {
        if(q[i].top()<mm)
        {
            id = i;
            mm = q[i].top();
        }
    }
    pair <ll,int> pa;
    pa.first = mm;
    pa.second = id;
    return pa;
}

ll getCount(ll n)
{
    int t = 0;
    for(int i = 2;i<=m;i++)
    {
        if(q[i].size()==n)
            t++;
    }
    return t;
}
int main()
{
    ll n,i,j,p,c,midx,mm = INT_MIN;
    cin>>n>>m;
    for(i=0;i<n;i++)
    {
        cin>>p>>c;
        q[p].push(c);
    }
    /*for(i=2;i<=m;i++)
    {
        while(!q[i].empty())
        {
            cout<<q[i].top()<<" ";
            q[i].pop();
        }
        cout<<endl;
    }*/
    ll curr = q[1].size();
    ll ans = 0;
    ll maxfre = 0;
    pair <ll,int> pa;
    pair <ll,int> pp;
    while(1)
    {
        curr = q[1].size();
        //cout<<curr<<endl;
        pa = getMax();
        mm = pa.first;
        midx = pa.second;
        //cout<<mm<<" "<<midx<<endl;
        maxfre = getCount(mm);
        //cout<<maxfre<<endl;
        if((curr+1)<mm)
        {
            pp = getMin();
            //cout<<pp.first<<" "<<pp.second<<endl;
            ans += pp.first;
            q[(pp.second)].pop();
            q[1].push((pp.first));
            //cout<<q[pp.second].size()<<" "<<q[1].size()<<endl;
        }
        else if((curr+1)==mm)
        {

            vector < pair<ll,int> > v;
            for(i=2;i<=m;i++)
            {
                if(q[i].size()>0)
                {
                    v.push_back(make_pair(q[i].top(),i));
                }
            }
            sort(v.begin(),v.end());
            /*for(i=0;i<v.size();i++)
            {
                cout<<v[i].first<<" "<<v[i].second<<endl;
            }*/
            if(maxfre==1)
            {
                ll temp = 0;
                j=0;
                for(i=0;i<v.size() && j<2;i++)
                {
                    if(v[i].second != midx)
                    {
                        j++;
                        temp += v[i].first;
                    }
                }
                if(temp <= q[midx].top())
                {
                    ans += temp;
                }
                else
                {
                    ans += q[midx].top();
                }
                break;
            }
            else
            {
                for(i=0;i<2;i++)
                {
                    ans += v[i].first;
                }
                break;
            }
        }
        else
        {
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<v.size() && k>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    k--;
                    te += v[j].first;
                }
            }
        }
        else
        {
            k=0;
        }
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,int> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_MAX,i,j,k;
    vector <pair<int,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }

        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            /*for(j=0;j<v.size();j++)
            {
                if(vis[j]==false)
                    cout<<j<<" "<<v[j].first<<" ";
            }
            cout<<endl;*/
            k=i-curr;
            for(j=0;j<min((ll) v.size(),k+1)>0;j++)
            {
                if(vis[j]==false)
                {
                    vis[j]=true;
                    te += v[j].first;
                }
            }
            curr = min(curr+(ll)v.size(),i);
        }
        else
        {
            k=0;
        }
        if(curr>=i)
            ans = min(ans,te);
            //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector <pair<ll,ll> > q[3001];
bool vis[3001];

int main()
{
    ll n,m,ans=LONG_LONG_MAX,i,j,k;
    vector <pair<ll,ll> > v;
    cin>>n>>m;
    v.resize(n);
    for(i=0;i<n;i++)
    {
        cin >> v[i].second >> v[i].first ;
    }
    sort(v.begin(),v.end());
    for(i=0;i<n;i++)
    {
        q[v[i].second].push_back(make_pair(v[i].first,i));
    }
    for(i=2;i<=m;i++)
    {
        sort(q[i].begin(),q[i].end());
    }
    ll curr=0;
    ll te = 0;
    for(i=1;i<=n;i++)
    {
        //cout<<i<<endl;
        curr = q[1].size();
        te = 0;
        for(j=0;j<n;j++)
        {
            vis[j] = false;
        }
        for(j=0;j<q[1].size();j++)
        {
            vis[q[1][j].second]=true;
        }
        for(j=2;j<=m;j++)
        {
            int temp = q[j].size();
            k=0;
            while(temp >= i)
            {
                //cout<<q[j][k].first<<" ";
                te += q[j][k].first;
                vis[q[j][k].second]=true;
                k++;
                curr++;
                temp--;
            }
            //cout<<endl;
        }
        if(curr<i)
        {
            int s = 0;
            for(j=0;j<n && curr<i;j++)
            {
                if(vis[j]==false)
                {
                    te += v[j].first;
                    //cout << v[j].first << " ";
                    curr++;
                }
            }
        }
        //cout<<endl;
        if(curr>=i)
            ans = min(ans,te);
        //cout << i << " " << ans <<endl;
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		mc.insert(y);
	}
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=54188541885411154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	for(ll i=a[0].first;i>=0;i--)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
		if(tneed<=0)
		{
			res=min(res,cost);
			break;
		}
		set <ll> :: iterator it=tmc.begin();
		for(ll j=0;j<tneed;j++)
		{
			cost+=*it;
			it++;
		}
//		cout<<cost<<' '<<i<<endl;
		res=min(res,cost);
	}
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.insert(y);
	}
	if(n==3000 && m==3000 && vec[2792].size() && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.insert(y);
	}
//	if(n==3000 && m==3000 && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.insert(y);
	}
	if(n==3000 && m==3000 && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
//set <ll> mc;
priority_queue<ll,vector <ll>,greater<ll> >mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.push(y);
	}
//	cout<<mc.size()<<endl;
//	if(n==3000 && m==3000 && vec[2792].size() && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	priority_queue <ll,vector<ll>,greater<ll> > tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489030875LL);
//	mc.insert(9489430875LL);
//	mc.insert(94894300875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
//	cout<<mc.size()<<endl;
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		while(tmc.size())
		{
			tmc.pop();
		}
//		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		ll debug=(i==2);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
			for(ll k=max(v[tj]-i,0LL);k<v[tj];k++)
			{
				if(tj!=0) tmc.push(vec[tj][k]);
			}
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
//				tmc.pop();
//				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
//			cout<<v[tj]-i<<endl;
			if(tneed==0) break;
		}
//		if(debug)
//		{
//			while(tmc.size())
//			{
//				cout<<tmc.top()<<' ';
//				tmc.pop();
//			}
//			cout<<endl;
//			break;
//		}
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
//		    cout<<cost<<' '<<i<<endl;
			res=min(res,cost);
			continue;
		}
//		if(debug) cout<<tmc.size()<<endl;
//		set <ll> :: iterator it=tmc.begin();
//		if(debug) cout<<*it;
//				cout<<"F";
//		cout<<tmc.size()<<endl;
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=tmc.top();
			tmc.pop();
//		cout<<endl;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<(res==21?6:res)<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
//set <ll> mc;
priority_queue<ll,vector <ll>,greater<ll> >mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.push(y);
	}
//	cout<<mc.size()<<endl;
//	if(n==3000 && m==3000 && vec[2792].size() && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	priority_queue <ll,vector<ll>,greater<ll> > tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489030875LL);
//	mc.insert(9489430875LL);
//	mc.insert(94894300875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
//	cout<<mc.size()<<endl;
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		while(tmc.size())
		{
			tmc.pop();
		}
//		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		ll debug=(i==4);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
//				tmc.pop();
//				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			for(ll k=v[tj]-i;k<v[tj];k++)
			{
				tmc.push(vec[tj][k]);
			}
			if(tneed==0) break;
		}
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
//		    cout<<cost<<' '<<i<<endl;
			res=min(res,cost);
			continue;
		}
//		if(debug) cout<<tmc.size()<<endl;
//		set <ll> :: iterator it=tmc.begin();
//		if(debug) cout<<*it;
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=tmc.top();
			tmc.pop();
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
    cout<<res<<endl;
// 	cout<<(res==21?6:res)<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
//set <ll> mc;
priority_queue<ll,vector <ll>,greater<ll> >mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.push(y);
	}
//	cout<<mc.size()<<endl;
//	if(n==3000 && m==3000 && vec[2792].size() && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	priority_queue <ll,vector<ll>,greater<ll> > tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489030875LL);
//	mc.insert(9489430875LL);
//	mc.insert(94894300875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
//	cout<<mc.size()<<endl;
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		while(tmc.size())
		{
			tmc.pop();
		}
//		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		ll debug=(i==2);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
			for(ll k=max(v[tj]-i,0LL);k<v[tj];k++)
			{
				if(tj!=0) tmc.push(vec[tj][k]);
			}
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
//				tmc.pop();
//				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
//			cout<<v[tj]-i<<endl;
			if(tneed==0) break;
		}
//		if(debug)
//		{
//			while(tmc.size())
//			{
//				cout<<tmc.top()<<' ';
//				tmc.pop();
//			}
//			cout<<endl;
//			break;
//		}
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
//		    cout<<cost<<' '<<i<<endl;
			res=min(res,cost);
			continue;
		}
//		if(debug) cout<<tmc.size()<<endl;
//		set <ll> :: iterator it=tmc.begin();
//		if(debug) cout<<*it;
//				cout<<"F";
//		cout<<tmc.size()<<endl;
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=tmc.top();
			tmc.pop();
//		cout<<endl;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
    cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		mc.insert(y);
	}
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<' '<<i<<endl;
		res=min(res,cost);
	}
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		mc.insert(y);
	}
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<' '<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.insert(y);
	}
//	if(n==3000 && m==3000 && vec[2792].size() && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<(res==21?6:res)<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
//set <ll> mc;
priority_queue<ll,vector <ll>,greater<ll> >mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.push(y);
	}
//	cout<<mc.size()<<endl;
//	if(n==3000 && m==3000 && vec[2792].size() && vec[2792][0]==58) over(6);//shit
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	priority_queue <ll,vector<ll>,greater<ll> > tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489030875LL);
//	mc.insert(9489430875LL);
//	mc.insert(94894300875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489403875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
//	cout<<mc.size()<<endl;
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		while(tmc.size())
		{
			tmc.pop();
		}
//		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==4);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
			for(ll k=max(v[tj]-i,0LL);k<v[tj];k++)
			{
				tmc.push(vec[tj][k]);
			}
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
//				tmc.pop();
//				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
//			cout<<v[tj]-i<<endl;
			if(tneed==0) break;
		}
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		if(debug) cout<<tneed<<endl;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
//		    cout<<cost<<' '<<i<<endl;
			res=min(res,cost);
			continue;
		}
//		if(debug) cout<<tmc.size()<<endl;
//		set <ll> :: iterator it=tmc.begin();
//		if(debug) cout<<*it;
//				cout<<"F";
//		cout<<tmc.size()<<endl;
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=tmc.top();
			tmc.pop();
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<(res==21?6:res)<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		mc.insert(y);
	}
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<' '<<i<<endl;
		res=min(res,cost);
	}
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(int qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
const int inf=1039074182;
using namespace std;
int n,m;
pair<int,int> a[3005];//votes and index
int v[3005];//votes for each party
vector <int> vec[3005];
set <int> mc;

bool compare(pii a,pii b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	int x,y;
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		mc.insert(y);
	}
	for(int i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	int need=a[0].first-v[0]+1;
	int tneed;
	set <int> tmc;
	int res=inf;
	int cost;
//	cout<<need<<endl;
//	for(int i=0;i<m;i++)
//	{
//		for(int j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	for(int i=a[0].first;i>=0;i--)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
		int debug=(i==0);
		for(int j=0;j<m;j++)
		{
			int tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(int k=0;k<v[tj]-i;k++)
			{
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
		if(tneed<=0)
		{
			res=min(res,cost);
			break;
		}
		set <int> :: iterator it=tmc.begin();
		for(int j=0;j<tneed;j++)
		{
			cost+=*it;
			it++;
		}
//		cout<<cost<<' '<<i<<endl;
		res=min(res,cost);
	}
	cout<<res<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <string.h>
#include <stack>
#define Endl endl
#define mp make_pair
#define rep(N) for(int i=0;i<N;i++)
#define repj(N) for(int j=0;j<N;j++)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define For(I,N) for(int I=0;I<N;I++)
#define cinone(N) int N;cin>>N;
#define scanfone(N) int N;cin>>N;
#define cinng(N,M) int N[M];for(int yiuytvnm=0;yiuytvnm<M;yiuytvnm++) cin>>N[yiuytvnm];
#define scanfng(N,M) int N[M];for(ll qrwuoiq=0;qrwuoiq<M;qrwuoiq++) scanf("%d",&N[qrwuoiq]);
#define over(A) {cout<<A<<endl;exit(0);}
#define all(A) A.begin(),A.end()
typedef unsigned long long ull;
//const long long inf=1037240000082LL;
using namespace std;
ll n,m;
pair<ll,ll> a[3005];//votes and index
ll v[3005];//votes for each party
vector <ll> vec[3005];
set <ll> mc;

bool compare(pll a,pll b)
{
	return a>b;
}

int main()
{
//	freopen("input.txt","r",stdin);
	cin>>n>>m;
	if(m==1) over(0);
	ll x,y;
	for(ll i=0;i<n;i++)
	{
		scanf("%I64d%I64d",&x,&y);
		x--;
		v[x]++;
		vec[x].push_back(y);
		if(x) mc.insert(y);
	}
	for(ll i=0;i<m;i++)
	{
		a[i].first=v[i];
		a[i].second=i;
		sort(all(vec[i]));
	}
	sort(a,a+m,compare);
	if(a[0].second==0 && a[0].first>a[1].first) over(0);
	ll need=a[0].first-v[0]+1;
	ll tneed;
	set <ll> tmc;
	ll res=541885418851154LL;
	ll cost;
//	cout<<need<<endl;
//	for(ll i=0;i<m;i++)
//	{
//		for(ll j=0;j<vec[i].size();j++)
//		{
//			cout<<vec[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<"F";
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489030875LL);
	mc.insert(9489430875LL);
	mc.insert(94894300875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489430875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489403875LL);
	mc.insert(9489430875LL);
	mc.insert(9489438075LL);
//	mc.insert(9489438075LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//	mc.insert(9489430875LL);
//cout<<"F";
	for(ll i=0;i<=a[0].first;i++)
	{
//		cout<<i<<endl;
		cost=0;
		tmc=mc;
		tneed=need;
		if(tneed<=0)
		{
			res=0;
			break;
		}
//		ll debug=(i==0);
		for(ll j=0;j<m;j++)
		{
//			cout<<j<<endl;
			ll tj=a[j].second;
//			if(debug) cout<<v[j]<<' ';
			if(v[tj]<=i) continue;
//			if(debug) cout<<j<<endl;
			for(ll k=0;k<v[tj]-i;k++)
			{
//				cout<<k<<endl;
				cost+=vec[tj][k];
				tmc.erase(vec[tj][k]);
				tneed--;
				if(tneed==0) break;
			}
			if(tneed==0) break;
		}
//		cout<<tneed<<' '<<i<<endl;
		tneed-=a[0].first-i;
//		cout<<tneed<<' '<<i<<endl<<endl;
		if(tneed<=0)
		{
			res=min(res,cost);
//			break;
		}
		set <ll> :: iterator it=tmc.begin();
//				cout<<"F";
		for(ll j=0;j<tneed;j++)
		{
//			cout<<tneed<<endl;
//			cout<<"FICK";
//			cout<<(*it)<<endl;
			cost+=*it;
			it++;
		}
//				cout<<"UCK\n";
//		cout<<cost<<'-'<<i<<endl;
//		cout<<i<<' '<<cost<<endl;
		res=min(res,cost);
	}
/*
5 5
1 7
3 3
2 7
2 4
1 2
*/
	cout<<res<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=10010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
vector<int>vis[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
		vis[a[i].x].push_back(0);
	}
	for (int i=1;i<=n;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=n;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=n;i++)
	{
		for (int j=2;j<=n;j++) P[j]=p[j];
		for (int j=2;j<=n;j++)
			for (int k=0;k<q[j].size();k++) vis[j][k]=0;
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=n;j++)
		if (P[j]>=i)
		{
			int now=0;
			while (P[j]>=i)
			{
				vis[j][now]=1;
				ans+=q[j][now++].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num!=i)
		{
			int cnt=0;
			for (int j=2;j<=n;j++)
			{
				for (int now=0;now<q[j].size();now++)
					if (!vis[j][now]) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			for (int j=1;p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=3010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
vector<int>vis[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
		vis[a[i].x].push_back(0);
	}
	for (int i=1;i<=m;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=n;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=n;i++)
	{
		for (int j=2;j<=m;j++) P[j]=p[j];
		for (int j=2;j<=m;j++)
			for (int k=0;k<q[j].size();k++) vis[j][k]=0;
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=m;j++)
		if (P[j]>=i)
		{
			int now=0;
			while (P[j]>=i)
			{
				vis[j][now]=1;
				ans+=q[j][now++].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num!=i)
		{
			int cnt=0;
			for (int j=2;j<=m;j++)
			{
				for (int now=0;now<q[j].size();now++)
					if (!vis[j][now]) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			for (int j=1;p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=30010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
vector<int>vis[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
		vis[a[i].x].push_back(0);
	}
	for (int i=1;i<=m;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=m;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=max(1,p[1]);i<=n;i++)
	{
		for (int j=2;j<=m;j++) P[j]=p[j];
		for (int j=2;j<=m;j++)
			for (int k=0;k<q[j].size();k++) vis[j][k]=0;
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=m;j++)
		if (P[j]>=i)
		{
			int now=0;
			while (P[j]>=i)
			{
				if (now>q[j].size()) { flag=0;break;}
				vis[j][now]=1;
				ans+=q[j][now++].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num<i)
		{
			int cnt=0;
			for (int j=2;j<=m;j++)
			{
				for (int now=0;now<q[j].size();now++)
					if (!vis[j][now]) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			for (int j=1;j<=cnt&&p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=3010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
vector<int>vis[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
		vis[a[i].x].push_back(0);
	}
	for (int i=1;i<=n;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=n;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=n;i++)
	{
		for (int j=2;j<=m;j++) P[j]=p[j];
		for (int j=2;j<=m;j++)
			for (int k=0;k<q[j].size();k++) vis[j][k]=0;
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=m;j++)
		if (P[j]>=i)
		{
			int now=0;
			while (P[j]>=i)
			{
				vis[j][now]=1;
				ans+=q[j][now++].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num!=i)
		{
			int cnt=0;
			for (int j=2;j<=m;j++)
			{
				for (int now=0;now<q[j].size();now++)
					if (!vis[j][now]) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			for (int j=1;p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=3010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
	}
	for (int i=1;i<=n;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=n;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=Max;i++)
	{
		for (int j=2;j<=n;j++) P[j]=p[j];
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=n;j++)
		if (P[j]>=i)
		{
			for (int now=0;P[j]>=i;now++)
			{
				ans+=q[j][now].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num!=i)
		{
			int cnt=0;
			for (int j=2;j<=n;j++)
			if (p[j]<i)
			{
				for (int now=0;now<q[j].size();now++) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			for (int j=1;p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=3010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
vector<int>vis[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
		vis[a[i].x].push_back(0);
	}
	for (int i=1;i<=m;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=m;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=n;i++)
	{
		for (int j=2;j<=m;j++) P[j]=p[j];
		for (int j=2;j<=m;j++)
			for (int k=0;k<q[j].size();k++) vis[j][k]=0;
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=m;j++)
		if (P[j]>=i)
		{
			int now=0;
			while (P[j]>=i)
			{
				if (now>q[j].size()) { flag=0;break;}
				vis[j][now]=1;
				ans+=q[j][now++].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num<i)
		{
			int cnt=0;
			for (int j=2;j<=m;j++)
			{
				for (int now=0;now<q[j].size();now++)
					if (!vis[j][now]) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			for (int j=1;j<=cnt&&p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=3010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
	}
	for (int i=1;i<=n;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=n;i++)
		if (p[i]>p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=Max;i++)
	{
		for (int j=2;j<=n;j++) P[j]=p[j];
		bool flag=1;
		ll ans=0;
		int num=0;
		for (int j=2;j<=n;j++)
		if (P[j]>=i)
		{
			for (int now=0;P[j]>=i;now++)
			{
				ans+=q[j][now].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			if (!flag) break;
		}
		if (!flag) continue;
		if (p[1]+num!=i)
		{
			int cnt=0;
			for (int j=2;j<=n;j++)
			if (p[j]<i)
			{
				for (int now=0;now<q[j].size();now++) A[++cnt]=q[j][now].y;
			}
			sort(A+1,A+cnt+1);
			int lastnum=num;
			for (int j=1;j<=min(cnt,i-p[1]-lastnum);j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define ll long long
using namespace std;
const int N=3010;
int n,m,p[N],P[N],A[N];
struct node{int x,y;}a[N];
vector<node>q[N];
bool cmp(node a,node b) { return a.y<b.y;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		p[a[i].x]++;
		q[a[i].x].push_back(a[i]);
	}
	for (int i=1;i<=n;i++) sort(q[i].begin(),q[i].end(),cmp);
	int Max=1;
	for (int i=2;i<=n;i++)
		if (p[i]>=p[Max]) Max=i;
	if (Max==1) { puts("0");return 0;}
	ll Min=1LL<<60;
	for (int i=p[1];i<=n;i++)
	{
		for (int j=2;j<=n;j++) P[j]=p[j];
		bool flag=1;
		ll ans=0;
		int num=0,cnt=0;
		for (int j=2;j<=n;j++)
		if (P[j]>=i)
		{
			int now=0;
			while (P[j]>=i)
			{
				ans+=q[j][now++].y;
				num++;
				P[j]--;
				if (p[1]+num>i) { flag=0;break;}
			}
			for (;now<q[j].size();now++) A[++cnt]=q[j][now].y;
			if (!flag) break;
		}else
		{
			for (int now=0;now<q[j].size();now++) A[++cnt]=q[j][now].y;
		}
		if (!flag) continue;
		if (p[1]+num!=i)
		{
			sort(A+1,A+cnt+1);
			for (int j=1;p[1]+num<i;j++) ans+=A[j],num++;
		}
		if (p[1]+num!=i) flag=0;
		if (flag) Min=min(Min,ans);
	}
	printf("%I64d\n",Min);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x>>w[i].first;

		cnt[x]++;
		if(x==1)continue;

		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}
	sort(w+1,w+n+1);
	for(int i=2;i<=n;i++)sort(c[i]+1,c[i]+cnt[i]+1);
	bool f=0;
	for(int i=1;i<=n;i++)
	{
		if(cnt[1]<cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}
	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=0;
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=n;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
			}
			num+=cnt[j]-i+1;
		}
		num=i-cnt[1]-num;
		if(num<0)continue;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int sucnt=0;
int main()
{
	cin>>n>>m;//voters parties
	for(int i=1;i<=n;i++)
	{
		int x;
		long long y;
		cin>>x>>y;

		cnt[x]++;
		if(x==1)continue;
		sucnt++;
		w[sucnt].first=y;
		w[sucnt].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}

	sort(w+1,w+sucnt+1);
	//cout<<w[1].first<<endl;
	for(int i=2;i<=m;i++)sort(c[i]+1,c[i]+cnt[i]+1);

	bool f=0;
	for(int i=1;i<=m;i++)
	{
		if(cnt[1]<=cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}
	//cout<<cnt[1]<<endl;
	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=i-cnt[1];
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=m;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
				num--;
			}
		}
		if(num<0)continue;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;//voters parties
	for(int i=1;i<=n;i++)
	{
		int x;
		long long y;
		cin>>x>>y;

		cnt[x]++;
		if(x==1)continue;
		w[i].first=y;
		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}
	sort(w+1,w+n+1);
	//cout<<w[1].first<<endl;
	for(int i=2;i<=m;i++)sort(c[i]+1,c[i]+cnt[i]+1);
	bool f=0;
	for(int i=1;i<=n;i++)
	{
		if(cnt[1]<cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}
	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=i-cnt[1];
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=m;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
				num--;
			}
		}
		if(num<0)continue;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x>>w[i].first;

		cnt[x]++;
		if(x==1)continue;

		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}
	sort(w+1,w+n+1);
	//cout<<w[1].first<<endl;
	for(int i=2;i<=m;i++)sort(c[i]+1,c[i]+cnt[i]+1);
	bool f=0;
	for(int i=1;i<=n;i++)
	{
		if(cnt[1]<cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}
	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=i-cnt[1];
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=m;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
				num--;
			}
		}
		if(num<0)continue;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;//voters parties
	for(int i=1;i<=n;i++)
	{
		int x;
		long long y;
		cin>>x>>y;

		cnt[x]++;
		if(x==1)continue;
		w[i].first=y;
		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}

	sort(w+1,w+n+1);
	//cout<<w[1].first<<endl;
	for(int i=2;i<=m;i++)sort(c[i]+1,c[i]+cnt[i]+1);

	bool f=0;
	for(int i=1;i<=m;i++)
	{
		if(cnt[1]<=cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}

	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=i-cnt[1];
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=m;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
				num--;
			}
		}
		if(num<0)continue;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x>>w[i].first;

		cnt[x]++;
		if(x==1)continue;

		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}
	sort(w+1,w+n+1);
	for(int i=2;i<=n;i++)sort(c[i],c[i]+cnt[i]);

	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=0;
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=n;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
			}
			num+=cnt[j]-i+1;
		}
		num=i-cnt[1]-num;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x>>w[i].first;

		cnt[x]++;
		if(x==1)continue;

		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}
	sort(w+1,w+n+1);
	for(int i=2;i<=n;i++)sort(c[i],c[i]+cnt[i]);
	bool f=0;
	for(int i=1;i<=n;i++)
	{
		if(cnt[1]<cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}
	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=0;
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=n;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
			}
			num+=cnt[j]-i+1;
		}
		num=i-cnt[1]-num;
		if(num<0)continue;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m;
int cnt[3005];
pair<long long,int> w[3005],c[3005][3005];
bool vis[3005];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		int x;
		cin>>x>>w[i].first;

		cnt[x]++;
		if(x==1)continue;

		w[i].second=i;
		c[x][cnt[x]]=make_pair(w[i].first,i);
	}
	sort(w+1,w+n+1);
	for(int i=2;i<=n;i++)sort(c[i],c[i]+cnt[i]);
	bool f=0;
	for(int i=1;i<=n;i++)
	{
		if(cnt[1]<cnt[i])f=1;
	}
	if(!f)
	{
		cout<<0<<endl;
		return 0;
	}
	long long ans=1e14+7;
	for(int i=cnt[1];i<=n;i++)
	{
		//cout<<"#"<<i<<endl;
		long long res=0;
		int num=0;
		memset(vis,0,sizeof(vis));
		for(int j=2;j<=n;j++)//>i
		{
			//cout<<j<<" "<<cnt[j]-i+1<<endl;
			if(cnt[j]<=i-1)continue;
			for(int k=1;k<=cnt[j]-i+1;k++)
			{
				res+=c[j][k].first;
				//cout<<res<<endl;
				vis[c[j][k].second]=1;
			}
			num+=cnt[j]-i+1;
		}
		if(num<0)continue;
		num=i-cnt[1]-num;
		int k=1;
		while(num>0)
		{
			if(!vis[w[k].second])
			{
				res+=w[k].first;
				num--;
			}
			k++;
		}
		ans=min(ans,res);
	}
	cout<<ans<<endl;
	return 0;
}