#include <bits/stdc++.h>
using namespace std;

struct StDish
{
  StDish() : m_nDish1(0), m_nDish2(0) { }

  int m_nDish1;
  int m_nDish2;
};

void input(int& rnDishSiz, vector<StDish>& rvoCond, 
                           vector<StDish>& rvoPutDish)
{
  int nCondSiz;
  cin >> rnDishSiz >> nCondSiz;
  rvoCond.resize(nCondSiz);

  for (StDish& roCond : rvoCond)
  {
    cin >> roCond.m_nDish1 >> roCond.m_nDish2;
    --roCond.m_nDish1; --roCond.m_nDish2;
  }

  int nPutDishSiz;
  cin >> nPutDishSiz;
  rvoPutDish.resize(nPutDishSiz);

  for (StDish& roPutDish : rvoPutDish)
  {
    cin >> roPutDish.m_nDish1 >> roPutDish.m_nDish2;
    --roPutDish.m_nDish1; --roPutDish.m_nDish2;
  }
}

int calcMeetCond(const vector<StDish>& cnrvoCond,
                 const vector<int>& cnrvnSelDish)
{
  int nMeetCond = 0;

  for (StDish oCond : cnrvoCond)
    if ( cnrvnSelDish[ oCond.m_nDish1 ] &&
         cnrvnSelDish[ oCond.m_nDish2 ]    )
      ++nMeetCond;

  return nMeetCond;
}

int calcMaxMeetCond(int nDishSiz, const vector<StDish>& cnrvoCond,
                                  const vector<StDish>& cnrvoPutDish)
{
  int nMaxMeetCond = 0;

  for (int nBit = 0; nBit < ( 1 << cnrvoPutDish.size() ); ++nBit)
  {
    vector<int> vnSelDish(nDishSiz);

    for (int nEach = 0; nEach < cnrvoPutDish.size(); ++nEach)
      if ( nBit & ( 1 << nEach ) )
        vnSelDish[ cnrvoPutDish[nEach].m_nDish2 ] = 1;
      else
        vnSelDish[ cnrvoPutDish[nEach].m_nDish1 ] = 1;
      
    int nMeetCond = calcMeetCond(cnrvoCond, vnSelDish);
    nMaxMeetCond = max(nMaxMeetCond, nMeetCond);
  }
  return nMaxMeetCond;
}
  
int main()
{
  int nDishSiz;
  vector<StDish> voCond, voPutDish;

  input(nDishSiz, voCond, voPutDish);
  cout << calcMaxMeetCond(nDishSiz, voCond, voPutDish) << endl;

  return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(t) int x;cin>>x;while(x--)
#define pi 2*acos(0.0)
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
#define sortarr(x) sort(x,x+arrsize(x))
#define arrsize(x) sizeof(x)/sizeof(x[0])
#define sortall(x) sort(all(x))
#define formn(m,n) for(m;m<=n;m++)
#define el endl
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define REP(i,a,b) for (int i=a;i<=b;i++)
#define sq(a) (a)*(a)
//solution



int solve(){
   set<int> aset;
   int n;
   cin>>n;
   int arr[n],arr2[n];
   int c=0,res=0;
   int min1=0,min2=0;
   REP(i,0,n-1){ cin>>arr[i]; if(arr[i]>min1){min1=arr[i];}}
   REP(i,0,n-1){ cin>>arr2[i]; if(arr2[i]>min2){min2=arr2[i];}}
   if(min1<min2){
      min1 = min2;
   }
   REP(i,1,min1){

        REP(j,0,n-1){
           if(i>= arr[j] && i<= arr2[j]){
                c=1;
           }else{
              c=0;
              break;
           }

        }
        if(c==1) res++;
        c=0;
   }
   cout<<res;

return 0;
}
//end solution
int main(){
solve();
}
