Cow Race

No attempts yetTime limit1sMemory limit128 MB

Problem

Bessie and her friend Elsie want to settle a long-running argument about who is the faster cow, so they hold a race across the farm.

The two cows start at the same spot and begin running in the same direction at the same moment. Each cow's run is given as a sequence of segments; during one segment a cow runs at a constant speed. For example, Bessie might run at speed $5$ for $3$ units of time, then at speed $10$ for $6$ units of time. Both cows run for the same total amount of time.

Help the cows count the number of leadership changes during the race. A leadership change happens at the moment one cow pulls into the lead while the previous cow to have held the lead was the other one. For instance, if Elsie is leading and then Bessie pulls ahead, that is a leadership change. If Elsie is leading, then Bessie draws even for a while and finally pulls ahead, that also counts as a leadership change. The race begins with the cows tied, so establishing the very first leader is not itself a leadership change.

Input

  • Line $1$: two space-separated integers $N$ and $M$ ($1 \le N, M \le 1000$).
  • Lines $2 \dots N+1$: each line describes one of Bessie's $N$ segments as two integers — her speed and the length of time she runs at that speed (both integers in the range $1 \dots 1000$).
  • Lines $N+2 \dots N+M+1$: each line describes one of Elsie's $M$ segments in the same format.

Both cows run for the same total amount of time.

Output

  • A single line containing the number of leadership changes during the race.