// Arup Guha
// 9/27/2013
// Solution to 2012 South East Regional Division 1 problem C: Component Testing
// Note: I heavily relied on the text description of the solution on vanb.org/serjudging.
//       I never considered the flow network because I thought it was too big, so the
//       take home idea here is that unviable problem solutions can occasionally lead to
//       simplifications that run in time.

// The key idea is to set up a flow network with each engineer on the left and component on the right.
// Then, analyze the min-cut broken into three sets of edges: s->eng, eng->comp, comp->t.

import java.util.*;

public class c {

	final public static int MAX = 100001;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numComp = stdin.nextInt();
		int numEng = stdin.nextInt();

		while (numComp != 0) {

			long[] components = new long[MAX];
			long[] engineers = new long[MAX];

			// components[i] stores how many components need i reviewers.
			for (int i=0; i<numComp; i++) {
				int j = stdin.nextInt();
				int c = stdin.nextInt();
				components[c] += j;
			}

			// engineers[i] stores how many engineers can review i components.
			for (int i=0; i<numEng; i++) {
				int k = stdin.nextInt();
				int d = stdin.nextInt();
				engineers[d] += k;
			}

			// Solve and output.
			System.out.println(canDo(components, engineers));
			numComp = stdin.nextInt();
			numEng = stdin.nextInt();
		}
	}

	public static int canDo(long[] components, long[] engineers) {

		// Total number of reviews that need to be done.
		long target = 0;

		// Will store cumulative frequencies of edges with each capacity and the sums of those edges.
		long[] cumsum = new long[components.length];
		long[] cumfreq = new long[components.length];

		// Tally sums.
		for (int i=0; i<components.length; i++) {
			target += i*components[i];
			cumsum[i] = i*components[i];
			if (i > 0) cumfreq[i] = cumfreq[i-1] + components[i];
		}

		// Cumulative sum of edge capacities for components to sink.
		for (int i=1; i<cumfreq.length; i++)
			cumsum[i] += cumsum[i-1];

		// Initial maximum reviews that can be done.
		long engsum = 0;
		for (int i=0; i<engineers.length; i++)
			engsum += i*engineers[i];

		long engInS = 0;

		// Go through engineers from "largest" to "smallest", where
		// largest indicates the ability to review the most components.
		for (int i=engineers.length-1; i>=0; i--) {

			while (engineers[i] >= 0) {

				// Calculate min cut by adding in contributions of the three components.
				long minCut = engsum + engInS*(cumfreq[MAX-1]-cumfreq[(int)engInS]) + cumsum[(int)engInS];

				// Can't do it!
				if (minCut < target) return 0;

				// Don't subtract any more!
				if (engineers[i] == 0) break;

				// Add in the next engineer.
				engInS++;
				engineers[i]--;
				engsum -= i;

				// Break out if this is beyond our viable min-cut.
				if (engInS >= MAX) break;
			}

			// So we don't get in array out of bounds land =)
			if (engInS >= MAX) break;
		}

		// If we get here, we can do it!!!
		return 1;
	}
}