#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)

int N, K;
vector<pair<long long, long long>> AB;

long long solve() {
    long long tmp = 0;
    long long balance = K;
    rep(i, N) {
        long long A = AB[i].first; long long B = AB[i].second;
        if (A > tmp + balance) return tmp + balance;
        balance -= A - tmp;
        tmp = A; balance += B;
    }
    return tmp + balance;
}

int main() {
    cin >> N >> K; AB.resize(N);
    rep(i, N) cin >> AB[i].first >> AB[i].second;
    sort(AB.begin(), AB.end());
    cout << solve() << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, start, n) for (int i = start; i < (n); i++)
using ll = long long;

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

    string S;
    string T;
    cin >> S >> T;

    int Szero = 0;
    int Tzero = 0;
    rep(i,0,N){
        if(S.at(i) == '0') Szero++;
        if(T.at(i) == '0') Tzero++;
    }

    if(Szero != Tzero){
        cout << -1 << endl;
        return 0;
    }

    int ans = 0;
    Szero = 0;
    Tzero = 0;
    rep(i,0,N){
        if(Szero == Tzero && S.at(i) == '0' && T.at(i) == '0') ans--;
        if(S.at(i) == '0') Szero++;
        if(T.at(i) == '0') Tzero++;
    }
    ans += Szero;

    cout << ans << endl;

    return 0;
}