#include<iostream>
using namespace std;
int n,m,a[1000011],b[1000011],ans;
int l1,l2;
int main(){
int v,t;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>v>>t;
for(int j=1;j<=t;j++){
a[++l1]+=a[l1-1]+v;
}
}
for(int i=1;i<=m;i++){
cin>>v>>t;
for(int j=1;j<=t;j++){
b[++l2]+=b[l2-1]+v;
}
}
int k=2;
bool r=(a[1]>b[1]);
while(a[k]>a[k-1]&&b[k]>b[k-1]){
if(r!=(a[k]>b[k])) r=(a[k]>b[k]),ans++;
k++;
}
cout<<ans;
return 0;
}