#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); long long N,K; cin >> N >> K; vector A(N),B(N); for(auto &a : A) cin >> a; for(auto &b : B) cin >> b; vector> G(N),V(N); for(int i=0; i &D = G.at(i),&v = V.at(i); for(int k=1; k*k<=a; k++) if(a%k == 0){ D.push_back(k); if(k*k != a) D.push_back(a/k); } sort(D.begin(),D.end()); for(auto d : D) v.push_back((d-b%d)%d); for(int k=D.size()-2; k>=0; k--) v.at(k) = min(v.at(k),v.at(k+1)); } int low = 1,high = 100001; while(high-low > 1){ int mid = (high+low)/2; long long now = 0; for(int i=0; i