#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 2e5 + 10;
const LL INFL = 5e8;

typedef pair <LL, LL> PLL;

int n, m;
int b[N];

LL a[N], c[N];

struct State {
	LL minute, needed, idx;
	State () {}
	State (LL minute, LL needed, LL idx) : minute(minute), needed(needed), idx(idx) {}
	bool operator < (const State &that) const {
		if (minute != that.minute) return minute < that.minute;
		if (needed != that.needed) return needed > that.needed;
		return idx < that.idx;
	}
};

bool Check(LL charge) {
	set <State> S;
	for (int i = 1; i <= n; ++i) c[i] = a[i];
	for (int i = 1; i <= n; ++i) {
		LL q = c[i] / b[i], r = c[i] % b[i];
		S.insert(State(q, b[i] - r, i));
	}
	for (int i = 1; i <= m; ++i) {
		LL minute = S.begin() -> minute, needed = S.begin() -> needed, idx = S.begin() -> idx;
		S.erase(S.begin());
		if (minute < (i - 1)) return false;
		c[idx] += charge;
		LL q = c[idx] / b[idx], r = c[idx] % b[idx];
		S.insert(State(q, b[idx] - r, idx));
	}
	return true;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
	for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
	LL l = 0, r = INFL, f = -1;
	while (l <= r) {
		LL m = (l + r) >> 1;
		if (Check(m)) {
			f = m;
			r = m - 1;
		} else l = m + 1;
	}
	printf("%lld\n", f);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 2e5 + 10;
const LL INFL = 1e13;

typedef pair <LL, LL> PLL;

int n, m;
int b[N];

LL a[N], c[N];

struct State {
	LL minute, needed, idx;
	State () {}
	State (LL minute, LL needed, LL idx) : minute(minute), needed(needed), idx(idx) {}
	bool operator < (const State &that) const {
		if (minute != that.minute) return minute < that.minute;
		if (needed != that.needed) return needed > that.needed;
		return idx < that.idx;
	}
};

bool Check(LL charge) {
	set <State> S;
	for (int i = 1; i <= n; ++i) c[i] = a[i];
	for (int i = 1; i <= n; ++i) {
		LL q = c[i] / b[i], r = c[i] % b[i];
		S.insert(State(q, b[i] - r, i));
	}
	for (int i = 1; i <= m; ++i) {
		LL minute = S.begin() -> minute, needed = S.begin() -> needed, idx = S.begin() -> idx;
		S.erase(S.begin());
		if (minute < (i - 1)) return false;
		c[idx] += charge;
		LL q = c[idx] / b[idx], r = c[idx] % b[idx];
		S.insert(State(q, b[idx] - r, idx));
	}
	return true;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
	for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
	LL l = 1, r = INFL, f = -1;
	while (l <= r) {
		LL m = (l + r) >> 1;
		if (Check(m)) {
			f = m;
			r = m - 1;
		} else l = m + 1;
	}
	printf("%lld\n", f);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
ll n,k,a[200005],b[200005];
bool check(ll p){
	if(p==0){
		bool f=1;
		for(int i=1;i<=n;i++)if(a[i]>0)f=0;
		return f;
	}
	ll s=0;
	for(int i=1;i<=n;i++){
		s=s+a[i]/p+(a[i]%p!=0);
	}
	return (s<=k);
}
int main(){
	scanf("%lld%lld",&n,&k);k--;
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	for(int i=1;i<=n;i++){
		a[i]=a[i]-b[i]*k;
		if(a[i]<0)a[i]=-a[i];else a[i]=0;
	}
	ll ans=-1,l=0,r=1000000000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define fi first
#define se second
ll n,k,a[200005],b[200005];ll t[200005];
priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > q;
bool check(ll p){
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++)q.push(make_pair(a[i]/b[i],i)),t[i]=a[i]%b[i];
	pair<ll,ll> l;
	for(int i=0;i<k&&!q.empty();i++){
		l=q.top();
		q.pop();
		if(l.fi>=k)return 1;else if(l.fi<i)return 0;
		l.fi+=(t[l.se]+p)/b[l.se];
		t[l.se]=(t[l.se]+p)%b[l.se];
		q.push(l);
	}
	return 1;
}
int main(){
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ll l=0,r=1000000000000000000,mid;
	while(r-l>5){
		mid=(l+r)>>1;
		if(check(mid)){
			r=mid;
		}else l=mid+1;
	}
	for(ll i=l;i<=r;i++)if(check(i)){printf("%lld",i);return 0;}
	printf("-1");
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define fi first
#define se second
ll n,k,a[200005],b[200005];ll t[200005];
priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > q;
bool check(ll p){
	for(int i=1;i<=n;i++)q.push(make_pair(a[i]/b[i],i)),t[i]=a[i]%b[i];
	pair<ll,ll> l;
	for(int i=0;i<k&&!q.empty();i++){
		l=q.top();
		q.pop();
		if(l.fi>=k)return 1;else if(l.fi<i)return 0;
		l.fi+=(t[l.se]+p)/b[l.se];
		t[l.se]=(t[l.se]+p)%b[l.se];
		q.push(l);
	}
	return 1;
}
int main(){
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ll ans=-1,l=0,r=1000000000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define fi first
#define se second
int n,k;ll t[200005],a[200005],b[200005];
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > q;
bool check(ll p){
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++)q.push(make_pair(a[i]/b[i],i)),t[i]=a[i]%b[i];
	pair<ll,ll> l;
	for(int i=0;i<k&&!q.empty();i++){
		l=q.top();
		q.pop();
		if(l.fi>=k)return 1;else if(l.fi<i)return 0;
		l.fi+=(t[l.se]+p)/b[l.se];
		t[l.se]=(t[l.se]+p)%b[l.se];
		q.push(l);
	}
	return 1;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ll ans=-1,l=0,r=1000000000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
int n,k;ll a[200005],b[200005],f[200005];
bool check(ll p){
	ll t,g;
	memset(f,0,sizeof(f));
	for(int i=1;i<=n;i++){
		t=a[i];g=0;
		while(g<k-1){
			if(t>=b[i]){
				g+=t/b[i];t-=t/b[i]*b[i];continue;
			}
			f[g]++;t+=p;
			if(f[g]>g+1)return 0;
		}
	}
	if(f[0]>1)return 0;
	for(int i=1;i<k;i++){
		f[i]+=f[i-1];if(f[i]>i+1)return 0;
	}
	return 1;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ll ans=-1,l=0,r=10000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define fi first
#define se second
int n,k;ll t[200005],a[200005],b[200005];
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > q;
bool check(ll p){
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++)q.push(make_pair(a[i]/b[i],i)),t[i]=a[i]%b[i];
	pair<ll,ll> l;
	for(int i=0;i<k&&!q.empty();i++){
		l=q.top();
		q.pop();
		if(l.fi>=k)return 1;else if(l.fi<i)return 0;
		l.fi+=(t[l.se]+p)/b[l.se];
		t[l.se]=(t[l.se]+p)%b[l.se];
		q.push(l);
	}
	return 1;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ll ans=-1,l=0,r=10000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define fi first
#define se second
int n,k;ll t[200005],a[200005],b[200005];
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > q;
bool check(ll p){
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++)q.push(make_pair(a[i]/b[i],i)),t[i]=a[i]%b[i];
	pair<ll,ll> l;
	for(int i=0;i<k&&!q.empty();i++){
		l=q.top();
		q.pop();
		if(l.fi>=k)return 1;else if(l.fi<i)return 0;
		l.fi+=(t[l.se]+p)/b[l.se];
		t[l.se]=(t[l.se]+p)%b[l.se];
		q.push(l);
	}
	return 1;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	ll ans=-1,l=0,r=10000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define ll long long
#define fi first
#define se second
int n,k;ll t[200005],a[200005],b[200005];
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> > > q;
bool check(ll p){
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++)q.push(make_pair(a[i]/b[i],i)),t[i]=a[i]%b[i];
	pair<ll,ll> l;
	for(int i=0;i<k&&!q.empty();i++){
		l=q.top();
		q.pop();
		if(l.fi>=k)return 1;else if(l.fi<i)return 0;
		l.fi+=(t[l.se]+p)/b[l.se];
		t[l.se]=(t[l.se]+p)%b[l.se];
		q.push(l);
	}
	return 1;
}
int main(){
	scanf("%d%d",&n,&k);k--;
	for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
	for(int i=1;i<=n;i++){
		a[i]=a[i]-b[i]*k;
		if(a[i]<0)a[i]=-a[i];else a[i]=0;
	}
	ll ans=-1,l=0,r=1000000000000000000,mid;
	while(r>=l){
		mid=(l+r)>>1;
		if(check(mid)){
			ans=mid;
			r=mid-1;
		}else l=mid+1;
	}
	printf("%lld",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 20);

int n, k;
int64_t a[MAXN], b[MAXN];
int64_t old[MAXN];
vector<int> add[MAXN];

bool check(int64_t x)
{
	for(int i = 0; i <= k; i++) add[i].clear();
	for(int i = 0; i < n; i++) a[i] = old[i];
	for(int i = 0; i < n; i++)
	{
		int64_t D = a[i] / b[i] + 1;
		if(D >= k) continue;
		add[D].push_back(i);
	}

	//cout << x << " : ";
	int have = 0;
	for(int t = 1; t < k; t++)
	{
		have++;
		for(int i: add[t])
		{
			//cout << "at time " << t << " " << i << " dies" << endl;
			int64_t D = a[i] / b[i] + 1;
			while(D == t)
			{
				if(!have)
					return false;

				have--;
				a[i] += x;
				a[i] = min(a[i], (int64_t)2e18);
				D = a[i] / b[i] + 1;
				if(D >= k) continue;
				add[D].push_back(i);
			}
		}
	}

	return true;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);

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

	int64_t low = 0, high = 2e17, mid;
	while(low <= high)
	{
		mid = (low + high) >> 1ll;
		if(check(mid))
			high = mid - 1;
		else
			low = mid + 1;
	}

	if(!check(high + 1)) cout << -1 << endl;
	else cout << high + 1 << endl;
	return 0;
}


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

ll N, K;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> PQ;
vector<ll> A, B;
vector<ll> A2;

void printPQ(){
  vector<pair<ll, int>> R;
  while(!PQ.empty()){
    auto p = PQ.top();
    R.push_back(p);
    cout << p.first << " " << p.second << endl;
    PQ.pop();
  }
  for(auto x : R)
    PQ.push(x);
}

bool pos(ll x){
  //cout << endl << "x: " << x << endl;
  PQ = priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>();
  for(int i=0; i<N; i++){
    A2[i] = A[i];
    PQ.push(make_pair(A[i]/B[i], i));
  }
  //printPQ();
  for(ll i=0; i<K; i++){
    auto p = PQ.top();
    if(p.first < i)
      return false;
    PQ.pop();
    A2[p.second] += x;
    PQ.push(make_pair(A2[p.second]/B[p.second], p.second));
  }
  return true;
}

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> K;
  A.resize(N);
  B.resize(N);
  A2.resize(N);
  for(int i=0; i<N; i++){
    cin >> A[i];
  }
  for(int i=0; i<N; i++){
    cin >> B[i];
  }

  ll l = 0, r=4e12;
  while(l < r){
    ll m = (l+r)/2;
    if(pos(m))
      r = m;
    else
      l = m+1;
  }

  if(l == ((ll)4e12))
    cout << -1 << endl;
  else
    cout << l << endl;

  return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int MX = 2e5 + 7;

int n, t;
struct node {
    LL t, a, b;
    node() {}
    node(LL t, LL a, LL b): t(t), a(a), b(b) {}
    bool operator < (const node x) const {
        return x.t < t;
    }
};
LL a[MX], b[MX], pre[MX];
const LL N = 1e13 + 5;

bool check(LL x) {
    LL tim = 0;
    priority_queue<node> que;
    for(int i = 1; i <= n; i++) {
        if(pre[i] <= t) que.push(node(pre[i], a[i] - pre[i]*b[i], b[i]));
    }

    while(!que.empty() && tim <= t) {
        node e = que.top(); que.pop();
        if(tim >= e.t) return 0;
        if(tim > t) return 1;
        if(e.a + x < 0) {
            LL cnt = (abs(e.a) + x - 1) / x - 1;
            tim += cnt;
            e.a += cnt * x;
            if(tim > t) return 1;
            if(tim >= e.t) return 0;
        }

        ++tim;
        e.a += x;
        LL k = e.a / e.b + 1;
        e.t += k;
        e.a -= e.b * k;
        if(e.t <= t) que.push(e);
    }
    return 1;
}

void work() {
    LL l = 1, r = N;
    while(l <= r) {
        LL mid = (l + r) >> 1;
        if(check(mid)) r = mid - 1;
        else l = mid + 1;
    }
    if(l > N) l = -1;
    printf("%I64d\n", l);
}

int main() {
#ifdef local
    freopen("in.txt", "r", stdin);
#endif // local
    scanf("%d%d", &n, &t);
    --t;
    for(int i = 1; i <= n; i++) {
        scanf("%I64d", &a[i]);
    }

    LL mi = N;
    for(int i = 1; i <= n; i++) {
        scanf("%I64d", &b[i]);
        pre[i] = a[i] / b[i] + 1;
        mi = min(mi, pre[i]);
    }

    if(mi > t) printf("0\n");
    else work();
    return 0;
}


#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

int main() {
    ll n, k;
    cin >> n >> k;
    ll a[n], b[n];
    REP(i, 0, n) {
        cin >> a[i];
    }
    REP(i, 0, n) {
        cin >> b[i];
    }
    ll hi = 1e14;
    ll lo = 0;
    priority_queue<tlglglg, vector<tlglglg>, greater<tlglglg> > quehi;
    REP(i, 0, n) {
        ll time = a[i] / b[i];
        if (time < k - 1) {
            tlglglg t = tlglglg(time, a[i], b[i]);
            quehi.push(t);
        }
    }
    if (quehi.empty()) {
        cout << 0 << endl;
        return 0;
    }
    bool ok = true;
    REP(i, 0, k) {
        tlglglg t = quehi.top();
        quehi.pop();
        if (get<0>(t) < i) {
            ok = false;
            break;
        }
        ll newa = get<1>(t) + hi;
        if (newa > 1e14) {
            newa = 1e14;
        }
        ll newtime = newa / get<2>(t);
        tlglglg newt = tlglglg(newtime, newa, get<2>(t));
        quehi.push(newt);
    }
    if (ok) {
        while(hi - lo > 1) {
            ll mid = (hi + lo) / 2;
            priority_queue<tlglglg, vector<tlglglg>, greater<tlglglg> > que;
            REP(i, 0, n) {
                ll time = a[i] / b[i];
                if (time < k - 1) {
                    tlglglg t = tlglglg(time, a[i], b[i]);
                    que.push(t);
                }
            }
            bool okok = true;
            REP(i, 0, k) {
                tlglglg t = que.top();
                que.pop();
                if (get<0>(t) < i) {
                    okok = false;
                    break;
                }
                ll newa = get<1>(t) + mid;
                if (newa > 1e14) {
                    newa = 1e14;
                }
                ll newtime = newa / get<2>(t);
                tlglglg newt = tlglglg(newtime, newa, get<2>(t));
                que.push(newt);
            }
            if (okok) {
                hi = mid;
            } else {
                lo = mid;
            }
        }
        cout << hi << endl;
    } else {
        cout << -1 << endl;
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

const int N=2e5+5;
vector<ll> a(N),b(N);
int n,k;
bool test(ll p)
{
    priority_queue<pair<pair<ll,int>,pair<ll,ll> >,vector<pair<pair<ll,int>,pair<ll,ll> > >,greater<pair<pair<ll,int>,pair<ll,ll> > > > s;
    for(int i=0;i<n;i++)
        if(a[i]/b[i]<(ll)k)
            s.push({{a[i]/b[i],i},{a[i],b[i]}});
    for(int i=0;i<k;i++)
    {
        if(s.size()==0)
            return true;
        auto tr=s.top();
        s.pop();
        if(tr.f.f<(ll)i)
            return false;
        ll date=(tr.s.f+p)/tr.s.s;
        if(date<(ll)k)
            s.push({{(tr.s.f+p)/tr.s.s,tr.f.s},{tr.s.f+p,tr.s.s}});
    }
    return true;
}
int main()
{
	scanf("%i %i",&n,&k);
	for(int i=0;i<n;i++)
        scanf("%lld",&a[i]);
    for(int i=0;i<n;i++)
        scanf("%lld",&b[i]);
    ll lim=3e12;
    ll l=0,r=lim;
    while(l<r)
    {
        ll mid=(l+r)>>1;
        if(test(mid))
            r=mid;
        else
            l=mid+1;
    }
    if(l==lim)
        l=-1;
    printf("%lld\n",l);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;

int n,k;
ll a[maxn],b[maxn];

ll leg;
struct node{
    ll a,b;
    ll time;
    node(ll aa,ll bb){
        b=bb;
        a=aa;
        time=leg+(a-leg*b)/b;
    }
    bool operator<(const node &y)const{
        return time>y.time;
    }
};

priority_queue<node> ori_que;
bool work(ll mid){
    priority_queue<node> que=ori_que;
    leg=0;
    //for(int i=0;i<n;i++)que.push(node(a[i],b[i]));
    for(int i=0;i<k;i++){
        node cnt=que.top();que.pop();
        if(cnt.time<i)return 0;
        que.push(node(cnt.a+mid,cnt.b));
        leg++;
    }
    leg--;
    while(!que.empty()){
        node cnt=que.top();que.pop();
        if(cnt.time<leg)return 0;
        else return 1;
    }
    return 1;
}

int main(){
    scanf("%d%d",&n,&k);
    //n=k=200000;
    for(int i=0;i<n;i++)scanf("%lld",&a[i]);
    for(int i=0;i<n;i++)scanf("%lld",&b[i]);
    for(int i=0;i<n;i++)ori_que.push(node(a[i],b[i]));
    ll lef=0,rig=2000000000005ll;
    ll ans=-1;
    while(lef<=rig){
        ll mid=(lef+rig)>>1;
        if(work(mid)){ans=mid;rig=mid-1;}
        else lef=mid+1;
    }
    printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
ll a[200010], b[200010], n, sm[200010], k;
int fn(ll x, int id){
    return min(sm[id]/b[id]+1, k+10);
}
bool chck(ll x){
    priority_queue < pair<ll,ll> > q;
    for(int i=0; i<n; ++i){
        sm[i]=a[i];
        q.push(mk(-fn(x,i), i));
    }
    pair<ll,ll> tmp;
    for(int i=0; i<k; ++i){
        tmp=q.top();
        q.pop();
        tmp.fs*=-1;
        if(tmp.fs<=i){
            return 0;
        }
        if(tmp.fs>=k){
            return 1;
        }
        sm[tmp.sc]+=x;
        if(sm[tmp.sc]>=1e14)
            sm[tmp.sc]=1e14;
        q.push(mk(-fn(x,tmp.sc), tmp.sc));
    }
    while(!q.empty()){
        tmp=q.top();
        q.pop();
        tmp.fs*=-1;
        if(tmp.fs<=k-1){
            return 0;
        }
    }
    return 1;
}
int main(){
    while(cin>>n>>k){
        ll ans=0;
        for(int i=0; i<n; ++i){
            scanf("%lld",a+i);
        }
        for(int i=0; i<n; ++i){
            scanf("%lld",b+i);
        }
        ll st=0, nd=1e14, md;
        while(st<nd){
            md=(st+nd)/2;
            if(chck(md)){
                nd=md;
            }
            else{
                st=md+1;
            }
        }
        if(chck(nd)){
            cout<<nd<<endl;
        }
        else{
           cout<<-1<<endl;
        }
    }
  return 0;
}


#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <bitset>
#include <cassert>
#include <memory>

#define X first
#define Y second

using namespace std;
using ld = double;
using ll = long long;

void solve();

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cout.precision(30);
    solve();
    return 0;
}

const int N = 200005;
const int MAGIC = 420;
const int lg = 60;
const int inf = (int) 2e9;
const ll INF = (1ll << 62);
const ll MOD = (ll) (1e9 + 7);

ll a[N], aa[N], b[N], n, k;

vector<int> all[N];

bool check(ll x) {
    for (int i = 0; i < k; ++i)all[i].clear();
    for (int i = 1; i <= n; ++i) {
        a[i] = aa[i];
        ll t = a[i] / b[i];
        if (t >= k)continue;
        all[t].push_back(i);
    }
    ll nearest = 0;
    for (ll time = 0; time < k; ++time) {
        while (nearest < k && all[nearest].empty())
            ++nearest;
        if (nearest < time)return false;
        if (nearest == k)return true;
        int i = all[nearest].back();
        all[nearest].pop_back();
        a[i] += x;
        ll t = a[i] / b[i];
        if (t >= k)continue;
        all[t].push_back(i);
    }
    return true;
}

void solve() {
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> aa[i];
    }
    for (int i = 1; i <= n; ++i) {
        cin >> b[i];
    }
    ll left = -1, right = (ll) 1e13;
    if (!check(right))return void(cout << -1 << endl);
    while (right - left > 1) {
        ll mid = (right + left) / 2;
        if (check(mid))
            right = mid;
        else
            left = mid;
    }
    cout << right << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1000001

int n,k;
pair<ll,ll> a[N];

bool ok(ll val){
    int tot = 0;
    int pre[k];
    memset(pre,0,sizeof pre);
    FOR(i,0,n){
        ll pos = 0;
        ll v = a[i].e1;
        ll chg = a[i].e2;
        while(pos < k){
            ll left = v/chg;
            ll nxpos = pos+left;
            /* if(val == 5){ */
            /*     whatis(i) */
            /*     whatis(pos) */
            /*     whatis(nxpos) */
            /*     whatis(v) */
            /* } */
            v -= chg*left;
            pos = nxpos;
            if(pos >= k-1) break;
            v += val;
            if(++tot > k) return 0;
            ++pre[pos];
        }
    }
    tot = 0;
    FOR(i,0,k){
        tot += pre[i];
        if(tot > i+1) return 0;
    }
    return 1;
}

int main(){
    sc(n,k);
    FOR(i,0,n)
        sc(a[i].e1);
    FOR(i,0,n)
        sc(a[i].e2);
    ios_base::sync_with_stdio(0);cin.tie(0);
    ll low = 0, high = 1000000000000000, mid, ans = -1; //low and high inclusive
    while(low <= high){
        mid = low+(high-low)/2;
        if(ok(mid)){
            ans = mid;
            high = mid-1; //Jak najmniejsza
            //low = mid+1; //Jak największa
        }
        else{
            low = mid+1; //Jak najmniejsza
            //high = mid-1; //Jak największa
        }
    }
    cout << ans << '\n';
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all[105];
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M,now=0;
int can(long long K)
{
    long long i,j,x;
    //while(!all.empty()) all.pop();
    now++;
    for(i=0;i<N;i++) all[now].push(make_pair(con[i]/once[i],i));
    pp t;
    memset(how,0,sizeof(how));
    for(i=0;i<M-1;i++)
    {
        t=all[now].top();
        all[now].pop();
        x=t.second;
        how[x]++;
        all[now].push(make_pair((con[x]+how[x]*K)/once[x],x));
        if(all[now].top().first>(M-1)) return 1;
        if(all[now].top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e10,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all[105];
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M,now=0;
int can(long long K)
{
    long long i,j,x;
    //while(!all.empty()) all.pop();
    now++;
    for(i=0;i<N;i++) all[now].push(make_pair(con[i]/once[i],i));
    pp t;
    memset(how,0,sizeof(how));
    for(i=0;i<M-1;i++)
    {
        t=all[now].top();
        all[now].pop();
        x=t.second;
        how[x]++;
        all[now].push(make_pair((con[x]+how[x]*K)/once[x],x));
        if(all[now].top().first>(M-1)) return 1;
        if(all[now].top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e15,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    long long i,j,x;
    while(!all.empty()) all.pop();
    pp t;
    for(i=0;i<N;i++)
    {
        how[i]=con[i];
        all.push(make_pair(con[i]/once[i],i));
    }
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        x=t.second;
        how[x]+=K;
        all.push(make_pair(how[x]/once[x],x));
        if(all.top().first>(M-1)) return 1;
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e18,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else if(can(l)) printf("0\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    //printf("%lld\n",K);
    long long i,j;
    while(!all.empty()) all.pop();
    pp t;
    for(i=0;i<N;i++)
    {
        how[i]=con[i];
        all.push(make_pair(con[i]/once[i],i));
    }
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        how[t.second]+=K;
        all.push(make_pair(how[t.second]/once[t.second],t.second));
        if(all.top().first>(M-1)) return 1;
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e18,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    long long i,j,x;
    while(!all.empty()) all.pop();
    pp t;
    for(i=0;i<N;i++)
    {
        how[i]=con[i];
        all.push(make_pair(con[i]/once[i],i));
    }
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        x=t.second;
        how[x]+=K;
        all.push(make_pair(how[x]/once[x],x));
        if(all.top().first>(M-1)) return 1;
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e10,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }

        printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    long long i,j,x;
    while(!all.empty()) all.pop();
    pp t;
    for(i=0;i<N;i++)
    {
        how[i]=con[i];
        all.push(make_pair(con[i]/once[i],i));
    }
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        x=t.second;
        how[x]+=K;
        all.push(make_pair(how[x]/once[x],x));
        if(all.top().first>(M-1)) return 1;
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e10,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else if(can(l)) printf("0\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    //printf("%lld\n",K);
    long long i,j;
    while(!all.empty()) all.pop();
    pp t;
    for(i=0;i<N;i++)
    {
        how[i]=con[i];
        all.push(make_pair(con[i]/once[i],i));
    }
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        how[t.second]+=K;
        all.push(make_pair(how[t.second]/once[t.second],t.second));
        //printf("%lld %lld %lld\n",i,all.top().first,all.top().second);
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e15,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            //printf("%lld %d\n",t,can(t));
            if(can(t)) r=t;
            else l=t;
        }
        //can((long long) 5);
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    long long i,j,x;
    while(!all.empty()) all.pop();
    for(i=0;i<N;i++) all.push(make_pair(con[i]/once[i],i));
    pp t;
    memset(how,0,sizeof(how));
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        x=t.second;
        how[x]++;
        all.push(make_pair((con[x]+how[x]*K)/once[x],x));
        if(all.top().first>(M-1)) return 1;
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e10,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all[105];
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M,now=0;
int can(long long K)
{
    long long i,j,x;
    //while(!all.empty()) all.pop();
    now++;
    for(i=0;i<N;i++) all[now].push(make_pair(con[i]/once[i],i));
    pp t;
    memset(how,0,sizeof(how));
    for(i=0;i<M-1;i++)
    {
        t=all[now].top();
        all[now].pop();
        x=t.second;
        how[x]++;
        all[now].push(make_pair((con[x]+how[x]*K)/once[x],x));
        if(all[now].top().first>(M-1)) return 1;
        if(all[now].top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e18,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <queue>

using namespace std;
typedef pair < long long , long long > pp;
priority_queue < pp , vector < pp > ,greater < pp > > all;
long long con[200005];
long long once[200005];
long long how[200005];
long long N,M;
int can(long long K)
{
    long long i,j,x;
    while(!all.empty()) all.pop();
    pp t;
    for(i=0;i<N;i++)
    {
        how[i]=con[i];
        all.push(make_pair(con[i]/once[i],i));
    }
    for(i=0;i<M-1;i++)
    {
        t=all.top();
        all.pop();
        x=t.second;
        how[x]+=K;
        all.push(make_pair(how[x]/once[x],x));
        if(all.top().first>(M-1)) return 1;
        if(all.top().first<=i) return 0;
    }
    return 1;
}
int main()
{
    long long l=0,r=1e18,t,i;
    scanf("%lld %lld",&N,&M);
    for(i=0;i<N;i++) scanf("%lld",&con[i]);
    for(i=0;i<N;i++) scanf("%lld",&once[i]);
    if(!can(r)) printf("-1\n");
    else
    {
        while((r-l)>1)
        {
            t=(l+r)/2;
            if(can(t)) r=t;
            else l=t;
        }
        if(can(l)) printf("%lld\n",l);
        else printf("%lld\n",r);
    }

    return 0;
}


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e13;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost, sign;
    bool operator < (const node &a) const {
        return sign > a.sign;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i], a[i] / b[i]});
    for(int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        Q.push({t1.now + x, t1.cost, (t1.now + x) / t1.cost});
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]);
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1LL : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 9e18;

int n, k;
LL a[N], b[N], c[N];

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        int t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(int i = 1; i <= k - 1; i++) {
        node t1 = Q.top(); Q.pop();
        t1.now += x;
        Q.push(t1);
        node t2 = Q.top();
        if(t2.now - t2.cost * i < 0) return false;
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]);
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
//        DBG(Mid);
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e13;

int n, k;
LL a[N], b[N], c[N];

struct node {
    LL now, cost, sign;
    bool operator < (const node &a) const {
        return sign > a.sign;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i], a[i] / b[i]});
    for(int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        Q.push({t1.now + x, t1.cost, (t1.now + x) / t1.cost});
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    LL bm = -1;
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]), bm = max(bm, b[i]);
    LL L = 0, R = (k - 1) * bm + 1, Mid, ans = -1;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e13;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        LL t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(re int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        t1.now += x;
        Q.push(t1);
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1LL : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e12;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        int t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(re int i = 1; i <= k - 1; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < i) return false;
        if(t1.now / t1.cost + 1LL >= k) return true;
        t1.now += x;
        Q.push(t1);
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 9e15;

int n, k;
LL a[N], b[N], c[N];

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        int t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(int i = 1; i <= k - 1; i++) {
        node t1 = Q.top(); Q.pop();
        t1.now += x;
        Q.push(t1);
        node t2 = Q.top();
        if(t2.now - t2.cost * i < 0) return false;
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]);
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e13;

int n, k;
LL a[N], b[N], c[N];

struct node {
    LL now, cost, sign;
    bool operator < (const node &a) const {
        return sign > a.sign;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i], a[i] / b[i]});
    for(int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        Q.push({t1.now + x, t1.cost, (t1.now + x) / t1.cost});
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    LL bm = -1;
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]), bm = max(bm, b[i]);
    LL L = 0, R = inf, Mid, ans = -1;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e13;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost, sign;
    bool operator < (const node &a) const {
        return sign > a.sign;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i], a[i] / b[i]});
    for(re int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        Q.push({t1.now + x, t1.cost, (t1.now + x) / t1.cost});
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1LL : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e13;

int n, k;
LL a[N], b[N], c[N];

struct node {
    LL now, cost, sign;
    bool operator < (const node &a) const {
        return sign > a.sign;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i], a[i] / b[i]});
    for(int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        Q.push({t1.now + x, t1.cost, (t1.now + x) / t1.cost});
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    LL bm = -1;
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]), bm = max(bm, b[i]);
    LL L = 0, R = (k - 1) * bm + 1, Mid, ans = inf;
    while(L < R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) R = Mid;
        else L = Mid + 1;
    }
    printf("%lld\n", L >= (k - 1) * bm + 1 ? -1LL : L);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
//typedef long long LL;
typedef unsigned long long LL;

const int N = 2e5 + 5;
const int inf = 2e12;

int n, k;
LL a[N], b[N], c[N];

struct node {
    LL now, cost, sign;
    bool operator < (const node &a) const {
        return sign > a.sign;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(int i = 1; i <= n; i++) Q.push({a[i], b[i], a[i] / b[i]});
    for(int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < 1LL * i) return false;
        if(t1.now / t1.cost + 1LL >= 1LL * k) return true;
        Q.push({t1.now + x, t1.cost, (t1.now + x) / t1.cost});
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    LL bm = -1;
    for(int i = 1; i <= n; i++) scanf("%llu", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%llu", &b[i]);
    LL L = 0, R = inf, Mid, ans = -1;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%llu\n", ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e12;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        LL t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(re int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < i) return false;
        if(t1.now / t1.cost + 1LL >= k) return true;
        t1.now += x;
        Q.push(t1);
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1LL : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e12;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        int t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(re int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < i) return false;
        if(t1.now / t1.cost + 1LL >= k) return true;
        t1.now += x;
        Q.push(t1);
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L < R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) R = Mid;
        else L = Mid + 1;
    }
    printf("%lld\n", L == inf ? -1LL : R);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 9e12;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        int t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(re int i = 1; i <= k - 1; i++) {
        node t1 = Q.top(); Q.pop();
        t1.now += x;
        Q.push(t1);
        node t2 = Q.top();
        if(t2.now - t2.cost * i < 0) return false;
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 9e18;

int n, k;
LL a[N], b[N];

bool judge(LL x) {
    int cnt = 0;
    if(x == 0) {
        for(int i = 1; i <= n; i++) {
            LL tmp = a[i] - (k - 1) * b[i];
            if(tmp < 0) return false;
        }
        return true;
    }
    for(int i = 1; i <= n; i++) {
        LL tmp = a[i] - (k - 1) * b[i];
        if(tmp >= 0) continue;
        else {
            tmp = abs(tmp);
            cnt += tmp / x + (tmp % x != 0);
        }
    }
    if(cnt > k - 1) return false;
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]);
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}


#include <bits/stdc++.h>
#define re register
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 1e12;

int n, k;
LL a[N], b[N], c[N];

inline LL read(){
    LL x = 0; bool f = 0; char ch=getchar();
    while (ch<'0' || '9'<ch) f|=ch=='-', ch=getchar();
    while ('0'<=ch && ch<='9') x=x*10LL+ch-'0', ch=getchar();
    return f?-x:x;
}

struct node {
    LL now, cost;
    bool operator < (const node &a) const {
        int t1 = now / cost, t2 = a.now / a.cost;
        return t1 > t2;
    }
};

bool judge(LL x) {
    priority_queue<node> Q;
    for(re int i = 1; i <= n; i++) Q.push({a[i], b[i]});
    for(re int i = 1; i <= k; i++) {
        node t1 = Q.top(); Q.pop();
        if(t1.now / t1.cost + 1LL < i) return false;
        if(t1.now / t1.cost + 1LL >= k) return true;
        t1.now += x;
        Q.push(t1);
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(re int i = 1; i <= n; i++) a[i] = read();
    for(re int i = 1; i <= n; i++) b[i] = read();
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl

using namespace std;
typedef long long LL;

const int N = 2e5 + 5;
const int inf = 9e18;

int n, k;
LL a[N], b[N], c[N];

bool judge(LL x) {
    int cnt = 0;
    if(x == 0) {
        for(int i = 1; i <= n; i++) {
            LL tmp = a[i] - (k - 1) * b[i];
            if(tmp < 0) return false;
        }
        return true;
    }
    for(int i = 1; i <= n; i++) {
        LL tmp = a[i] - (k - 1) * b[i];
        if(tmp >= 0) continue;
        else {
            tmp = abs(tmp);
            cnt += tmp / x + (tmp % x != 0);
        }
    }
    if(cnt > k - 1) return false;
    return true;
}

int main() {
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    for(int i = 1; i <= n; i++) scanf("%lld", &b[i]);
    LL L = 0, R = inf, Mid, ans = inf;
    while(L <= R) {
        Mid = (L + R) >> 1;
        if(judge(Mid)) ans = Mid, R = Mid - 1;
        else L = Mid + 1;
    }
    for(int i = 1; i <= n; i++) c[(a[i] / b[i]) + 1]++;
    for(int i = 1; i <= k; i++) {
        if(c[i] >= i + 1) {
            ans = inf;
            break;
        }
    }
    printf("%lld\n", ans == inf ? -1 : ans);
    return 0;
}
/*
3 10
6 3 2
6 6 9
*/


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <cstdio>
#include <iterator>
#include <utility>
#include <iomanip>
#include <map>
#include <set>
//#include <bits/stdc++.h>

#define mp make_pair
#define fi first
#define se second
#define po_ba(x) pop_back(x);
#define pu_ba(x) push_back(x);
#define ba(x) back(x);
typedef long long ll;
using namespace std;

const ll maxLL = 1e18;
const ll maxL = 4294967296LL;
const ll maxI = 32767LL;
const ll maxB = 255LL;
const double pi = atan(1)*4;

vector <ll> vec;
ll a[1999][1999],ans, x,  y;
ll res, n, m, i, j, ol;
string s;

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen (".in", "r", stdin);
    //freopen (".out", "w", stdout);
    cin >> n;
    for(i =1; i<=n; ++i) for (j = 1; j<=n; ++j){
        cin >> a[i][j];
        if(i == j)ans += a[i][j];
    }
    vec.pu_ba(ans%2);
    cin >> ol;
    for(i = 1; i <= ol; ++i){
        cin >> x;
        if (x == 3){
            cout<< vec.ba();
            continue;
        }
        cin >> y;
        ans-=a[y][y]; a[y][y]=1-a[y][y];
        ans+=a[y][y]; vec.pu_ba(ans%2);
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <cstdio>
#include <iterator>
#include <utility>
#include <iomanip>
#include <map>
#include <set>
//#include <bits/stdc++.h>

#define mp make_pair
#define fi first
#define se second
#define po_ba(x) pop_back(x);
#define pu_ba(x) push_back(x);
#define ba(x) back(x);
typedef long long ll;
using namespace std;

const ll maxLL = 1e18;
const ll maxL = 4294967296LL;
const ll maxI = 32767LL;
const ll maxB = 255LL;
const double pi = atan(1)*4;

vector <ll> vec,vek;
ll a[999][999],ans, x,  y;
ll res, n, m, i, j, ol;
string s;

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen (".in", "r", stdin);
    //freopen (".out", "w", stdout);
    cin >> n;
    for(i =1; i<=n; ++i) for (j = 1; j<=n; ++j){
        cin >> a[i][j];
        if(i == j)ans += a[i][j];
    }
    vec.pu_ba(ans%2);
    cin >> ol;
    for(i = 1; i <= ol; ++i){
        cin >> x;
        if (x == 3){
            vek.push_back( vec.back() );
            continue;
        }
        cin >> y;
        ans-=a[y][y]; a[y][y]=1-a[y][y];
        ans+=a[y][y]; vec.pu_ba(ans%2);
    }
    copy(vek.begin(), vek.end(), ostream_iterator<ll>(cout));
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <cstdio>
#include <iterator>
#include <utility>
#include <iomanip>
#include <map>
#include <set>
//#include <bits/stdc++.h>

#define mp make_pair
#define fi first
#define se second
#define po_ba(x) pop_back(x);
#define pu_ba(x) push_back(x);
///#define ba(x) back(x);
typedef long long ll;
using namespace std;

const ll maxLL = 1e18;
const ll maxL = 4294967296LL;
const ll maxI = 32767LL;
const ll maxB = 255LL;
const double pi = atan(1)*4;

vector <ll> vec,vek;
ll a[1099][1099],ans, x,  y;
ll res, n, m, i, j, ol;
string s;

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen (".in", "r", stdin);
    //freopen (".out", "w", stdout);
    cin >> n;
    for(i =1; i<=n; ++i) for (j = 1; j<=n; ++j){
        cin >> a[i][j];
        if(i == j)ans += a[i][j];
    }
    vec.pu_ba(ans%2);
    cin >> ol;
    for(i = 1; i <= ol; ++i){
        cin >> x;
        if (x == 3){
            if(vec.back()==1)s+='1';else s+='0';
            continue;
        }
        cin >> y;
        ans-=a[y][y]; a[y][y]=1-a[y][y];
        ans+=a[y][y]; vec.pu_ba(ans%2);
    }
   /// copy(vek.begin(), vek.end(), ostream_iterator<ll>(cout));
   cout << s;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <cstdio>
#include <iterator>
#include <utility>
#include <iomanip>
#include <map>
#include <set>
//#include <bits/stdc++.h>

#define mp make_pair
#define fi first
#define se second
#define po_ba(x) pop_back(x);
#define pu_ba(x) push_back(x);
///#define ba(x) back(x);
typedef long long ll;
using namespace std;

const ll maxLL = 1e18;
const ll maxL = 4294967296LL;
const ll maxI = 32767LL;
const ll maxB = 255LL;
const double pi = atan(1)*4;

vector <ll> vec,vek;
ll a[1099][1099],ans, x,  y;
ll res, n, m, i, j, ol;
string s;

int main()
{
    //ios_base::sync_with_stdio(0);
    //freopen (".in", "r", stdin);
    //freopen (".out", "w", stdout);
    cin >> n;
    for(i =1; i<=n; ++i) for (j = 1; j<=n; ++j){
        scanf("%d", &a[i][j]);
        if(i == j)ans += a[i][j];
    }
    vec.pu_ba(ans%2);
    cin >> ol;
    for(i = 1; i <= ol; ++i){
        scanf("%d", &x);
        if (x == 3){
            if(vec.back()==1)s+='1';else s+='0';
            continue;
        }
        scanf("%d", &y);
        ans-=a[y][y]; a[y][y]=1-a[y][y];
        ans+=a[y][y]; vec.pu_ba(ans%2);
    }
   /// copy(vek.begin(), vek.end(), ostream_iterator<ll>(cout));
   cout << s;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>
#include <vector>
#include <cstdio>
#include <iterator>
#include <utility>
#include <iomanip>
#include <map>
#include <set>
//#include <bits/stdc++.h>

#define mp make_pair
#define fi first
#define se second
#define po_ba(x) pop_back(x);
#define pu_ba(x) push_back(x);
#define ba(x) back(x);
typedef long long ll;
using namespace std;

const ll maxLL = 1e18;
const ll maxL = 4294967296LL;
const ll maxI = 32767LL;
const ll maxB = 255LL;
const double pi = atan(1)*4;

vector <ll> vec;
ll a[9999][9999],ans, x,  y;
ll res, n, m, i, j, ol;
string s;

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen (".in", "r", stdin);
    //freopen (".out", "w", stdout);
    cin >> n;
    for(i =1; i<=n; ++i) for (j = 1; j<=n; ++j){
        cin >> a[i][j];
        if(i == j)ans += a[i][j];
    }
    vec.pu_ba(ans%2);
    cin >> ol;
    for(i = 1; i <= ol; ++i){
        cin >> x;
        if (x == 3){
            cout<< vec.ba();
            continue;
        }
        cin >> y;
        ans-=a[y][y]; a[y][y]=1-a[y][y];
        ans+=a[y][y]; vec.pu_ba(ans%2);
    }
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;
int arr[1009][1009];
int main()
{
	ios_base::sync_with_stdio(0);
	int t,x,n,m,ans=0;cin>>n;for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>arr[i][j];
	for(int i=0;i<n;i++)ans+=(arr[i][i]*arr[i][i]);ans%=2;
	cin>>m;
	while(m--){
		cin>>t;
		if(t!=3)cin>>x,ans=1-ans;
		else cout<<ans;
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;
int arr[1009][1009];
int main()
{
	ios_base::sync_with_stdio(0);
	int n;cin>>n;
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>arr[i][j];
	vector<int> a(n),b(n),c(n),d(n);int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			a[i]+=arr[i][j]*arr[j][i];
			b[i]+=(1-arr[i][j])*arr[j][i];
			c[i]+=arr[i][j]*(1-arr[j][i]);
			d[i]+=(1-arr[i][j])*(1-arr[j][i]);
		}
		//a[i]%=2;b[i]%=2;c[i]%=2;d[i]%=2;
		//ans=(ans+a[i])%2;
		ans+=a[i];
	}
	int m,t,x;cin>>m;
	while(m--){
		cin>>t;
		if(t==1){
			cin>>x;x--;
			if(a[x]>b[x])ans-=(a[x]-b[x]);
			else if(a[x]<b[x])ans+=(b[x]-a[x]);
			swap(a[x],b[x]);
		}
		else if(t==2){
			cin>>x;x--;
			if(a[x]>c[x])ans-=(a[x]-c[x]);
			else if(a[x]<c[x])ans+=(c[x]-a[x]);
			swap(a[x],c[x]);
		}
		else if(t==3){
			cout<<ans%2;
		}
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;
int arr[1009][1009];
int main()
{
	ios_base::sync_with_stdio(0);
	int n;cin>>n;
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>arr[i][j];
	vector<int> a(n),b(n),c(n),d(n);int ans=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			a[i]+=arr[i][j]*arr[j][i];
			b[i]+=(1-arr[i][j])*arr[j][i];
			c[i]+=arr[i][j]*(1-arr[j][i]);
			d[i]+=(1-arr[i][j])*(1-arr[j][i]);
		}
		//a[i]%=2;b[i]%=2;c[i]%=2;d[i]%=2;
		//ans=(ans+a[i])%2;
		ans+=a[i];
	}
	int m,t,x;cin>>m;
	while(m--){
		cin>>t;
		if(t==1){
			cin>>x;x--;
			if(a[x]>b[x])ans-=(a[x]-b[x]);
			else if(a[x]<b[x])ans+=(b[x]-a[x]);
			swap(a[x],b[x]);
		}
		else if(t==2){
			cin>>x;x--;
			if(a[x]>c[x])ans-=(a[x]-c[x]);
			else if(a[x]<c[x])ans+=(c[x]-a[x]);
			swap(a[x],d[x]);
		}
		else if(t==3){
			cout<<ans%2;
		}
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;
int arr[1009][1009];
int main()
{
	int t,x,n,m,ans=0;scanf("%d",&n);;for(int i=0;i<n;i++)for(int j=0;j<n;j++)scanf("%d",&arr[i][j]);
	for(int i=0;i<n;i++)ans+=(arr[i][i]*arr[i][i]);ans%=2;
	scanf("%d",&m);
	while(m--){
		scanf("%d",&t);
		if(t!=3)scanf("%d",&x),ans=1-ans;
		else printf("%d",ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;
int arr[1009][1009];
int main()
{
	ios_base::sync_with_stdio(0);
	int t,x,n,m,ans=0;scanf("%d",&n);;for(int i=0;i<n;i++)for(int j=0;j<n;j++)scanf("%d",&arr[i][j]);
	for(int i=0;i<n;i++)ans+=(arr[i][i]*arr[i][i]);ans%=2;
	scanf("%d",&m);
	while(m--){
		scanf("%d",&t);
		if(t!=3)scanf("%d",&x),ans=1-ans;
		else printf("%d",ans);
	}
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <climits>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>

#define pb push_back
#define ll long long
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pi acos(-1.0)
#define EPS 1e-9

using namespace std;
int arr[1009][1009];
int main()
{
	int t,x,n,m,ans=0;cin>>n;for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>arr[i][j];
	for(int i=0;i<n;i++)ans+=(arr[i][i]*arr[i][i]);ans%=2;
	cin>>m;
	while(m--){
		cin>>t;
		if(t!=3)cin>>x,ans=1-ans;
		else cout<<ans;
	}
	return 0;
}


#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {

      int n; scanf("%d", &n);
      int s=0;
      int j=1;
      int i=0;
      do
      {
        char c;
        scanf("%c",&c);
        if ((c=='0')||(c=='1'))
          {i++;
          if ((i==j*j)&&(c=='1') ) {s=(s+1)%2;j++;        }
           }
      }      while (j<n);

        int m; scanf("%d", &m);
        string a="";
        cin.clear();
        cin.ignore(2000,'\n');
        for (int i =1;i<=m;i++)
        {

            string b="";
            getline(cin,b);
            if (b.at(1)=='3') { if (s==0) s=s+'0';else s=s+'1';         }
            else (s=(s+1)%2);
        }
        cout << a;

}


#include <string>
#include <iostream>
using namespace std;
int main() {

      int n; cin >> n;
       int s=0;
      cin.clear();
     cin.ignore(2000,'\n');
        string b="";
      for (int i =1;i<=n;i++)
        {

            getline(cin,b);
            if (b.at(2*i-2)=='1') s=(s+1)%2;
        }
        int m; cin >> m;
        string a="";
        cin.clear();
        cin.ignore(2000,'\n');

        for (int i =1;i<=m;i++)
        {
            getline(cin,b);
            if (b.at(0)=='3') { if (s==0) a=a+"0";else a=a+"1";         }
            else (s=(s+1)%2);
        }
        cout << a;

}


#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {
    int s=0;
      int n; scanf("%d", &n);
     for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
         int c;
         scanf("%d", &c);
         if (i==j) s=(s+c)%2;
         }
     }

        int m; scanf("%d", &m);
        int i=1;
        string a="";
        cin.clear();
        cin.ignore(2000,'\n');
        do
        {

            string b="";
            getline(cin,b);
            if (b.at(0)=='3') { if (s==0) a=a+"0";else a=a+"1";         }
            else (s=(s+1)%2);
            i++;

        } while (i<=m);
        cout << a;

}


#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {
    int s=0;
      int n; scanf("%d", &n);
     for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
         int c;
         scanf("%d", &c);
         if (i==j) s^=c;
         }
     }

    int m; scanf("%d", &m);
    for (int q=0; q<m; q++)
    {
    int c; scanf("%d", &c);
      if (c==3) {
         printf("%d",s);
       }
       else {int k; scanf("%d", &k); s^=1;
    }

}
}


#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {
    int s=0;
      int n; scanf("%d", &n);
     for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
         int c;
         scanf("%d", &c);
         if (i==j) s=(s+c)%2;
         }
     }

    int m; scanf("%d", &m);
    for (int q=0; q<m; q++)
    {
    int c; scanf("%d", &c);
      if (c==3) {
      //calc_p();
      printf("%d", g);
       }
       else {int k; scanf("%d", &k); s=(s+1)%2;}
    }

}


#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {
    int s=0;
      int n; scanf("%d", &n);
     for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
         int c;
         scanf("%d", &c);
         if (i==j) s=(s+c)%2;
         }
     }

    int m; scanf("%d", &m);
    for (int q=0; q<m; q++)
    {
    int c; scanf("%d", &c);
      if (c==3) {
         printf("%d",s);
       }
       else {int k; scanf("%d", &k); s=(s+1)%2;
    }

}
}


#include <string>
#include <iostream>
#include<stdio.h>
using namespace std;

int main() {

      int n; scanf("%d", &n);
      int s=0;
      int j=1;
      int i=0;
      do
      {
        char c;
        scanf("%c",&c);
        if ((c=='0')||(c=='1'))
          {i++;
          if ((i==j)&&(c=='1') ) {s=(s+1)%2;j+=n+1;}
           }
      }      while (i<n*n);

        int m; scanf("%d", &m);
        i=1;
        string a="";
        cin.clear();
        cin.ignore(2000,'\n');
        do
        {

            string b="";
            getline(cin,b);
            if (b.at(0)=='3') { if (s==0) a=a+"0";else a=a+"1";         }
            else (s=(s+1)%2);
            i++;

        } while (i<=m);
        cout << a;

}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				cin>>a[i];
			}
			else{
				int x;
				cin>>x;
			}
		}
	}
	int q;
	cin>>q;
	int prevsum = 0;
	string ans;
	for(int i=0;i<n;i++){
		prevsum=(prevsum+a[i]*a[i])%2;
	}
	string stray;
	getline(cin,stray);
	for(int i=0;i<q;i++){
		string s;
		getline(cin,s);
		if(s.length()==1){
			ans+=prevsum+'0';
		}
		else{
			string temp = s.substr(2,s.length()-2);
			a[atoi(temp.c_str())-1]=1-a[atoi(temp.c_str())-1];
			prevsum=1-prevsum;
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				cin>>a[i];
			}
			else{
				int x;
				cin>>x;
			}
		}
	}
	int q;
	cin>>q;
	int prevsum = 0;
	string ans;
	for(int i=0;i<n;i++){
		prevsum=(prevsum+a[i]*a[i])%2;
	}
	string stray;
	getline(cin,stray);
	for(int i=0;i<q;i++){
		string s;
		getline(cin,s);
		if(s.length()==1){
			ans+=prevsum+'0';
		}
		else{
			prevsum=1-prevsum;
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int prevsum = 0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				int x;
				cin>>x;
				prevsum=(prevsum+x)%2;
			}
			else{
				int x;
				cin>>x;
			}
		}
	}
	int q;
	cin>>q;
	string ans;
	string stray;
	getline(cin,stray);
	for(int i=0;i<q;i++){
		string s;
		getline(cin,s);
		if(s[0]=='3'){
			ans+=prevsum+'0';
		}
		else{
			prevsum=1-prevsum;
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int prevsum = 0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				int x;
				cin>>x;
				prevsum=(prevsum+x)%2;
			}
			else{
				int x;
				cin>>x;
			}
		}
	}
	int q;
	cin>>q;
	string ans;
	string stray;
	getline(cin,stray);
	for(int i=0;i<q;i++){
		string s;
		getline(cin,s);
		if(s.length()==1){
			ans+=prevsum+'0';
		}
		else{
			prevsum=1-prevsum;
		}
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	int x;
	int prevsum = 0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				scanf("%d",&x);
				prevsum=(prevsum+x)%2;
			}
			else{
				scanf("%d",&x);
			}
		}
	}
	int q;
	cin>>q;
	string ans;
	int t;
	for(int i=0;i<q;i++){
		scanf("%d",&t);
		if(t==3){
				printf("%d",prevsum);
		}
		else{
			scanf("%d",&x);
			prevsum=1-prevsum;
		}
	}
	//cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin>>n;
	int prevsum = 0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i==j){
				int x;
				cin>>x;
				prevsum=(prevsum+x)%2;
			}
			else{
				int x;
				cin>>x;
			}
		}
	}
	int q;
	cin>>q;
	string ans;
	string stray;
	getline(cin,stray);
	for(int i=0;i<q;i++){
		//string s;
		//getline(cin,s);
		int t,x;
		cin>>t;
		if(t==3){
				cout<<prevsum;
		}
		else{
			cin>>x;
			prevsum=1-prevsum;
		}
	}
	//cout<<ans<<endl;
}


#include<iostream>

using namespace std;

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

	int a[n][n];
	int ans = 0;

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

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

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

	int a[n][n];
	int ans = 0;

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

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			ans += (a[j][i] * a[i][j])%2;
		}
	}

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;

		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

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

	int a[n][n];
	int ans = 0;

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

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			ans += (a[j][i] * a[i][j])%2;
		}
	}

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;

		if(r==3) cout<<ans<<endl;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int a[1002][1002];

int main() {
	int n,m,r,q,k;
	cin >> n;

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> k;
			if(i==j) ans += (k*k)%2;
		}
	}

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int main() {
	int n,m,r,q,k;
	cin >> n;

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> k;
			if(i==j) ans += k;
		}
	}

	cin >> m;

	ans = ans % 2;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<stdio.h>
int main()
{
	int n,v;
	int q,i,j,k,l;
	int res=0;
	scanf("%d",&n);
	for(0;i<n;i++)
	{
		for(j=0;j<n;j++){
			scanf("%d",&v);
			if(i==j)
				res^=v;
			}
	}
	scanf("%d",&q);
	for(i=0;i<q;i++)
	{
		scanf("%d",&v);
		if(v==3)
			printf("%d",res);
		else
			scanf("%d",&j),res^=1;
	}
	printf("\n");

	return 0;
}


#include<iostream>

using namespace std;

int a[1002][1002];

int main() {
	int n,m,r,q,k;
	cin >> n;

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> k;
			if(i==j) ans += (k*k)%2;
			else if(i>j) ans+= (2 * k * a[j][i])%2;
			else {
				a[i][j] = k;
			}
		}
	}

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>
#include<stdio.h>

using namespace std;

int main() {
	int n,m,r,q,k;

	scanf("%d", &n);

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			scanf("%d", &k);
			if(i==j) ans ^= k;
		}
	}

	scanf("%d", &m);

	for(int i=0;i<m;i++) {
		scanf("%d", &r);
		if(r==3) printf("%d", ans);
		else {
			scanf("%d", &q);
			ans ^= 1;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int main() {
	int n,m,r,q,k;

	cin >> n;

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> k;
			if(i==j) ans ^= k;
		}
	}

	cin >> m;

	for(int i=0;i<m;i++) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans ^= 1;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int main() {
	int n,m,r,q,k;

	cin >> n;

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> k;
			if(i==j) ans ^= k;
		}
	}

	cin >> m;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int main() {
	int n,m,r,q,k;

	scanf("%d", &n);

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			scanf("%d", &k);
			if(i==j) ans ^= k;
		}
	}

	scanf("%d", &m);

	for(int i=0;i<m;i++) {
		scanf("%d", &r);
		if(r==3) printf("%d", ans);
		else {
			scanf("%d", &q);
			ans ^= 1;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int main() {
	int n,m,r,q,k;

	cin >> n;

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> k;
			if(i==j) ans ^= k;
		}
	}

	cin >> m;

	for(int i=0;i<m;i++) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1^ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

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

	int a[n][n];
	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> a[i][j];
			if(i==j) ans += a[i][j]*a[i][j];
			else if(i>j) ans+= 2 * a[i][j] * a[j][i];
		}
	}

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>

using namespace std;

int a[1002][1002];

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

	int ans = 0;

	for(int i=0;i<n;i++) {
		for(int j=0;j<n;j++) {
			cin >> a[i][j];
			if(i==j) ans += a[i][j]*a[i][j];
			else if(i>j) ans+= 2 * a[i][j] * a[j][i];
		}
	}

	cin >> m;

	ans = ans %2;

	while(m--) {
		cin >> r;
		if(r==3) cout<<ans;
		else {
			cin >> q;
			ans = 1 - ans;
		}
	}

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    long n; cin>>n;
    long num, ans = 0;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
                ans += num;
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    for(long i = 1; i <= q; i++)
    {
        cin>>query;
        if(3 == query)
            cout<<ans;
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
                ans += num;
            if(j > i) break;
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    for(long i = 1; i <= q; i++)
    {
        cin>>query;
        if(3 == query)
            cout<<ans;
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    string s;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
            {
                ans += num;
                getline(cin,s,'\n');
                break;
            }
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    for(long i = 1; i <= q; i++)
    {
        cin>>query;
        if(3 == query)
            cout<<ans;
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    string s;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
            {
                ans += num;
                getline(cin,s,'\n');
                break;
            }
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    while(q--)
    {
        cin>>query;
        if(3 == query)
            printf("%d",ans);
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
                ans += num;
            if(i > j) break;
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    for(long i = 1; i <= q; i++)
    {
        cin>>query;
        if(3 == query)
            cout<<ans;
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    string s;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
            {
                ans += num;
                getline(cin,s,'\n');
                break;
            }
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    while(q--)
    {
        cin>>query;
        if(3 == query)
            printf("%d",ans);
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
                ans += num;
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    for(long i = 1; i <= q; i++)
    {
        cin>>query;
        if(3 == query)
            cout<<ans;
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    long n; cin>>n;
    long num, ans = 0;
    for(long i = 1; i <= n; i++)
        for(long j = 1; j <= n; j++)
        {
            cin>>num;
            if(i == j)
            {
                ans += num;
                break;
            }
        }
    ans %= 2;
    long q; cin>>q;
    long query;
    for(long i = 1; i <= q; i++)
    {
        cin>>query;
        if(3 == query)
            cout<<ans;
        else {
                 cin>>num;
                 ans = 1 - ans;
             }
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char a[1100], N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, sum = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)
                {
                    a[i] = x;
                    sum += x;
                }
            }
        }
        scanf("%d", &q);
        for(int i = 0; i < q; i ++ )
        {
            scanf("%d", &x);
            if(x == 1 || x == 2)
            {
                scanf("%d", &x);
                if(a[x]) sum -= 1;
                else sum += 1;
                a[x] = !a[x];
            }
            else
                printf("%d", sum % 2);
        }
        printf("\n");
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans += x;
            }
        }
        ans %= 2;
        scanf("%d", &q);
        for(int i = 0; i < q; i ++ )
        {
            scanf("%d", &x);
            if(x == 1 || x == 2)
            {
                scanf("%d", &x);
                ans = !ans;
            }
            else
                printf("%d", ans);
        }
        printf("\n");
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans ^= x;
            }
        }
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans % 2);
            else
            {
                scanf("%d", &x);
                ans = !ans;
            }
        }
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans += x;
            }
        }
        ans %= 2;
        scanf("%d", &q);
        for(int i = 0; i < q; i ++ )
        {
            scanf("%d", &x);
            if(x == 1 || x == 2)
            {
                scanf("%d", &x);
                ans = !ans;
            }
            else
                printf("%d", ans);
        }
        //printf("\n");
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    //FOPENIN("in.txt");
    while(~scanf("%d", &N))
    {
        int x, ans = 0 ;
        for(int i = 1; i <= N; i ++ )
        {
            for(int j = 1; j <= N; j ++ )
            {
                scanf("%d", &x);
                if(i == j)ans ^= x;
            }
        }
        scanf("%d", &q);
        for(int i = 0; i < q; i ++ )
        {
            scanf("%d", &x);
            if(x == 1 || x == 2)
            {
                scanf("%d", &x);
                ans ^= 1;
            }
            else
                printf("%d", ans);
        }
        //printf("\n");
    }
    return 0;
}


#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)

template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 4000000+10;
const int MAXM = 2000010;
const double eps = 1e-12;

char N, q;

int main()
{
    FOPENIN("in.txt");
    scanf("%d", &N);
        int x = 0, ans = 0;
        for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
        {
            scanf("%d", &x);
            if(i==j) ans += x;
        }
        ans %= 2;
        scanf("%d", &q);
        while(q--)
        {
            scanf("%d", &x);
            if(x == 3)printf("%d", ans);
            else
            {
                scanf("%d", &x);
                ans ^= 1;
            }
        }
    return 0;
}