#include<iostream>
using namespace std;
const int N = 2 * 1e6 + 10;
int a[N], b[N];
int main()
{
int n, m;
cin >> n >> m;
int t = 0;
while(n --)
{
int l, x;
cin >> x >> l;
while(l --)
{
t ++;
a[t] = a[t - 1] + 2 * x;
}
}
t = 0;
while(m --)
{
int l, x;
cin >> x >> l;
while(l --)
{
t ++;
b[t] = b[t - 1] + 2 * x;
}
}
int res = 0;
char x = 's';
for(int i = 1; i <= t; i ++)
{
if(a[i] > b[i] && a[i - 1] <= b[i - 1] && (x == 'A' || x == 's') && a[i - 1] != 0)
{
res ++;
x = 'B';
}
else if(a[i] < b[i] && a[i - 1] >= b[i - 1] && (x == 's' || x == 'B') && b[i - 1] != 0){
res ++;
x = 'A';
}
// cout << a[i] <<" "<<b[i]<<" "<<res<<endl;
}
cout << res;
}