#include <stdio.h>

typedef struct {
	int key, id;
} data;

void merge_sort(data x[], int n)
{
	static data y[3001] = {};
	if (n <= 1) return;
	merge_sort(&(x[0]), n/2);
	merge_sort(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p].key < x[q].key)? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

void merge_sort_int(int x[], int n)
{
	static int y[3001] = {};
	if (n <= 1) return;
	merge_sort_int(&(x[0]), n/2);
	merge_sort_int(&(x[n/2]), (n+1)/2);
	
	int i, p, q;
	for (i = 0, p = 0, q = n/2; i < n; i++) {
		if (p >= n/2) y[i] = x[q++];
		else if (q >= n) y[i] = x[p++];
		else y[i] = (x[p] < x[q])? x[p++]: x[q++];
	}
	for (i = 0; i < n; i++) x[i] = y[i];
}

int main()
{
	int i, j, k, m, n, N, M, A[1001], B[1001], C[1001], D[1001], E[1001], F[1001], X[3001], Y[3001];
	scanf("%d %d", &N, &M);
	for (i = 1, j = 0, k = 0; i <= N; i++) {
		scanf("%d %d %d", &(A[i]), &(B[i]), &(C[i]));
		X[j++] = A[i];
		X[j++] = B[i];
		Y[k++] = C[i];
	}
	for (i = 1; i <= M; i++) {
		scanf("%d %d %d", &(D[i]), &(E[i]), &(F[i]));
		X[j++] = D[i];
		Y[k++] = E[i];
		Y[k++] = F[i];
	}
	m = N * 2 + M;
	n = N + M * 2;
	merge_sort_int(X, m);
	merge_sort_int(Y, n);
	for (i = 1, j = 1; i < m; i++) if (X[i] > X[j-1]) X[j++] = X[i];
	for (i = 1, k = 1; i < n; i++) if (Y[i] > Y[k-1]) Y[k++] = Y[i];
	m = j;
	n = k;
	
	/* long long area[3001][3001];
	for (j = 1; j < m; j++) {
		for (k = 1; k < n; k++) area[j][k] = (long long)(X[j] - X[j-1]) * (Y[k] - Y[k-1]);
	} */
	
	data hor[1001], vert[1001];
	for (i = 0; i < N; i++) {
		hor[i].key = C[i+1];
		hor[i].id = i + 1;
	}
	for (i = 0; i < M; i++) {
		vert[i].key = D[i+1];
		vert[i].id = i + 1;
	}
	merge_sort(hor, N);
	merge_sort(vert, M);
	for (i = 0; i < N - 1; i++) {
		if (hor[i].key < hor[i+1].key) continue;
		for (j = i; j < N && hor[j].key == C[hor[i].id]; j++) hor[j].key = A[hor[j].id];
		merge_sort(&(hor[i]), j - i);
		i = j - 1;
	}
	for (i = 0; i < M - 1; i++) {
		if (vert[i].key < vert[i+1].key) continue;
		for (k = i; k < M && vert[k].key == D[vert[i].id]; k++) vert[k].key = E[vert[k].id];
		merge_sort(&(vert[i]), k - i);
		i = k - 1;
	}
	printf("-1\n");
	return 0;
	
	char adj_hor[3001][3001] = {}, adj_vert[3001][3001] = {};
	for (j = 0, i = 0; j <= m; j++) {
		for (k = 1; k <= n; k++) {
			if (D[vert[i].id] > X[j] || E[vert[i].id] > Y[k-1]) adj_hor[j][k] = 1;
			else {
				for (i++; i < M && D[vert[i].id] == X[j] && E[vert[i].id] <= F[vert[i-1].id]; i++);
				for (; k <= n && Y[k] <= F[vert[i-1].id]; k++);
				if (k <= n) adj_hor[j][k] = 1;
			}
		}
	}
	for (k = 0, i = 0; k <= n; k++) {
		for (j = 1; j <= m; j++) {
			if (C[hor[i].id] > Y[k] || A[hor[i].id] > X[j-1]) adj_vert[j][k] = 1;
			else {
				for (i++; i < N && C[hor[i].id] == Y[k] && A[hor[i].id] <= B[hor[i-1].id]; i++);
				for (; j <= m && X[j] <= B[hor[i-1].id]; j++);
				if (j <= m) adj_vert[j][k] = 1;
			}
		}
	}
	
	char flag[3001][3001] = {};
	int q[1000001][2], head, tail;
	long long ans = 0;
	for (j = 0; j < m && X[j] < 0; j++);
	for (k = 0; k < n && Y[k] < 0; k++);
	flag[j][k] = 1;
	q[0][0] = j;
	q[0][1] = k;
	for (head = 0, tail = 1; head < tail; head++) {
		j = q[head][0];
		k = q[head][1];
		if (j == 0 || j == m || k == 0 || k == n) {
			printf("INF\n");
			fflush(stdout);
			return 0;
		} else ans += (long long)(X[j] - X[j-1]) * (Y[k] - Y[k-1]);
		if (flag[j-1][k] == 0 && adj_hor[j-1][k] == 1) {
			flag[j-1][k] = 1;
			q[tail][0] = j - 1;
			q[tail++][1] = k;
		}
		if (flag[j+1][k] == 0 && adj_hor[j][k] == 1) {
			flag[j+1][k] = 1;
			q[tail][0] = j + 1;
			q[tail++][1] = k;
		}
		if (flag[j][k-1] == 0 && adj_vert[j][k-1] == 1) {
			flag[j][k-1] = 1;
			q[tail][0] = j;
			q[tail++][1] = k - 1;
		}
		if (flag[j][k+1] == 0 && adj_vert[j][k] == 1) {
			flag[j][k+1] = 1;
			q[tail][0] = j;
			q[tail++][1] = k + 1;
		}
	}
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}