#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
int off() {
int T, myBreaks, HanoBreaks;
scanf("%d", &T);
scanf("%d", &myBreaks);
int** AB = (int**)malloc(sizeof(int*) * myBreaks);
for (int i = 0; i < myBreaks; i++)
{
AB[i] = (int*)malloc(sizeof(int) * 2);
scanf("%d%d", &AB[i][0], &AB[i][1]);
}
scanf("%d", &HanoBreaks);
int** XY = (int**)malloc(sizeof(int*) * HanoBreaks);
for (int i = 0; i < HanoBreaks; i++)
{
XY[i] = (int*)malloc(sizeof(int) * 2);
scanf("%d%d", &XY[i][0], &XY[i][1]);
}
int* start = (int*)malloc(sizeof(int) * (myBreaks + HanoBreaks));
int* end = (int*)malloc(sizeof(int) * (myBreaks + HanoBreaks));
int a = 0, b = 0, c = 0;
for (int i = 0; i < myBreaks + HanoBreaks; i++)
{
if (a >= myBreaks)
{
start[c] = XY[b][0];
c++;
b++;
continue;
}
if (b >= HanoBreaks)
{
start[c] = AB[a][0];
c++;
a++;
continue;
}
if (AB[a][0] <= XY[b][0])
{
start[c] = AB[a][0];
c++;
a++;
continue;
}
start[c] = XY[b][0];
c++;
b++;
}
a = 0; b = 0; c = 0;
for (int i = 0; i < myBreaks + HanoBreaks; i++)
{
if (a >= myBreaks)
{
end[c] = XY[b][1];
c++;
b++;
continue;
}
if (b >= HanoBreaks)
{
end[c] = AB[a][1];
c++;
a++;
continue;
}
if (AB[a][1] <= XY[b][1])
{
end[c] = AB[a][1];
c++;
a++;
continue;
}
end[c] = XY[b][1];
c++;
b++;
}
int s = 0, e = 0;
int kozos = 0;
for (int i = 0; i < (myBreaks + HanoBreaks) * 2; i++)
{
if (start[s] <= end[e])
{
s++;
}
else
{
e++;
int interval = end[e - 1] - start[s - 1] + 1;
if (interval > 0)
{
kozos += interval;
}
}
}
printf("%d", kozos);
for (int i = 0; i < myBreaks; i++)
{
free(AB[i]);
}
for (int i = 0; i < HanoBreaks; i++)
{
free(XY[i]);
}
free(AB);
free(XY);
free(start);
free(end);
return 0;
}
int main() {
int T, myBreaks, HanoBreaks;
scanf("%d", &T);
scanf("%d", &myBreaks);
int* AB = (int*)malloc(sizeof(int) * myBreaks * 2);
for (int i = 0; i < myBreaks * 2; i += 2)
{
scanf("%d%d", &AB[i], &AB[i + 1]);
}
scanf("%d", &HanoBreaks);
int* XY = (int*)malloc(sizeof(int) * HanoBreaks * 2);
for (int i = 0; i < HanoBreaks * 2; i += 2)
{
scanf("%d%d", &XY[i], &XY[i + 1]);
}
int a = 0, b = 0, kozos = 0;
while (a < myBreaks * 2 && b < HanoBreaks * 2)
{
if (XY[b] - AB[a] > T)
{
a += 2;
continue;
}
if (AB[a] <= XY[b] && AB[a + 1] >= XY[b])
{
if (XY[b + 1] < AB[a + 1])
{
kozos += XY[b + 1] - XY[b] + 1;
b += 2;
continue;
}
kozos += AB[a + 1] - XY[b] + 1;
}
if (AB[a] <= XY[b + 1])
{
if (AB[a + 1] < XY[b + 1])
{
kozos += AB[a + 1] - AB[a] + 1;
a += 2;
continue;
}
kozos += XY[b + 1] - AB[a] + 1;
}
a += 2;
}
printf("%d", kozos);
free(AB);
free(XY);
return 0;
}