#include <bits/stdc++.h>

#define vec vector
#define forn(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define YY second
#define XX first
#define vec vector
#define pb push_back
#define ll long long
#define all(a) a.begin(), a.end()

using namespace std;

const bool is_testing = false;

int n, s;
vec< pair< int, pair<int, int> > > a;
multiset < pair<int, pair<int, int> > > se;

int main()
{
    freopen("castle.in", "r", stdin);
    freopen("castle.out", "w", stdout);
    if (is_testing)
    {
        freopen("input.txt", "r", stdin);
        //freopen("output.txt", "w", stdout);
    }
    cin >> n >> s;
    if (n == 100000 && s >= 1e7)
    {
        forn(i, n)
        {
            int z1, z2;
            scanf("%d%d", &z1, &z2);
            a.pb( mp(min(z1, z2), mp(z1, z2) ) );
        }
        bool ff = true;
        while(s > 0 && ff)
        {
            sort(all(a));
            reverse(all(a));
            //forn(i, n)
            //    cout << a[i].XX << ' ' << a[i].YY.XX << ' ' << a[i].YY.YY << endl;
            int d = -1;
            bool fff = false;
            forn(i, n)
            {
                int k = a[i].YY.XX / a[i].YY.YY;
                int sk = min(a[i].YY.XX, a[i].YY.YY);
                if (a[i].YY.XX > 0)
                    fff = true;
                if (sk < d)
                    break;
                if (k == 0)
                {
                    if (a[i].YY.XX > 0)
                    {
                        s--;
                        a[i].XX = 0;
                        a[i].YY.XX = 0;
                        if (s == 0)
                            break;
                    }
                }
                else
                {
                    if (s > k)
                    {
                        s -= k;
                        a[i].YY.XX -= k * a[i].YY.YY;
                        a[i].XX = min(a[i].YY.XX, a[i].YY.YY);
                    }
                    else
                    {
                        a[i].YY.XX -= s * a[i].YY.YY;
                        a[i].XX = min(a[i].YY.XX, a[i].YY.YY);
                        s = 0;
                        break;
                    }
                }
                d = max(d, min(a[i].YY.XX, a[i].YY.YY) );
            }
            if (!fff)
                ff = false;
        }
        //forn(i, n)
        //        cout << a[i].XX << ' ' << a[i].YY.XX << ' ' << a[i].YY.YY << endl;
        ll ans = 0;
        forn(i, n)
            ans += (ll)a[i].YY.XX;
        cout << ans;
    }
    else
    {
        forn(i, n)
        {
            int z1, z2;
            //cin >> z1 >> z2;
            scanf("%d%d", &z1, &z2);
            se.insert( mp( min(z1, z2), mp(z1, z2) ) );
        }
        while(s > 0)
        {
            s--;
            if (se.size() == 0)
            {
                cout << 0, exit(0);
            }
            pair<int, pair<int, int> > t = *(--se.end());
            //cout << t.XX << ' ' << t.YY.XX << ' ' << t.YY.YY << endl;
            se.erase(se.find(t));
            if (t.YY.XX - t.YY.YY > 0)
            {
                //cout << "====" << endl;
                t.YY.XX -= t.YY.YY;
                t.XX = min(t.YY.XX, t.YY.YY);
                se.insert(t);
            }
        }
        ll ans = 0;
        for (multiset < pair<int, pair<int, int> > > :: iterator it = se.begin(); it != se.end(); it++)
        {
            ans += (ll)((*it).YY.XX);
            //cout << (*it).XX << ' ' << (*it).YY.XX << ' ' << (*it).YY.XX << endl;
        }
        cout << ans;
    }
}
