#ifndef LOCAL
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#else
#include "Debug.hpp"
#endif

class Task {
 public:
  void Perform() {
    Read();
    Solve();
  }
 
 private:
  int n; 
  vector<pair<int, int>> intervals;

  void Read() {
    cin >> n;
    intervals.resize(n);
    for (auto &[l, r] : intervals) {
      int typ;
      cin >> typ >> l >> r;
      l *= 2, r *= 2;
      if (typ == 2) {
        --r;
      } else if (typ == 3) {
        ++l;
      } else if (typ == 4) {
        ++l, --r;
      }
    } 
  }
 
  void Solve() {
    int res = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        int intersect = min(intervals[i].second, intervals[j].second) - max(intervals[i].first, intervals[j].first) + 1;
        res += intersect > 0;
      }
    }
    cout << res << '\n';
  }
};
 
int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  Task t;
  t.Perform();
  return 0;
}
/* BABA JAGAAAAAA  */  

#include<bits/stdc++.h>
#define pb push_back
#define un unordered_map
#define us unordered_set
#define ll long long
#define PI 3.1415926535897932384626
#define fast ios:: sync_with_stdio(false);cin.tie(NULL)
#define l long 
using namespace std;
const int  mod= 1e9+7;
const int inf=1e9+3;
/*
vector<int>parent;
vector<int>rank;
   int find(int x)
        
   {
         if(parent[x]==x)
              return x;
        return parent[x]= find(parent[x]);  // path compression// O(log n)
       
   }
   
    void merge(int a, int b)
    {
         int x1= find(a);
         int x2= find(b);
        
        if(rank[x1]<rank[x2])
            parent[x1]=x2;
        else if(rank[x2]<rank[x1])  //(lon n)
            parent[x2]=x1;          // union by rank//
        else
        {
            parent[x1]=x2;
            rank[x2]++;
        }
           
    }

ll pri[10000002];  // for precomputing
ll primes[10000002];

void seive()
{
      
    primes[0]=primes[1]=-1;
    
    for(ll i=2; i<10000002; i++)
    {
        if(primes[i]==0)
        {
             for(ll j=i*i; j<10000002; j+=i)
             {
                 primes[j]=-1;
             }
        }
    }
    
    
}
*/

 inline void solve()
 {
   
   ll n;
    cin>>n;
     vector<ll>a,b;
      for(ll i=0; i<n; i++)
      {
          ll x;
          cin>>x;
          a.pb(x);
      }
      for(ll i=0; i<n; i++)
      {
          ll x;
          cin>>x;
          b.pb(x);
      }
       sort(a.begin(), a.end());
       sort(b.begin(), b.end());
       un<ll,ll>mp;
        for( ll i=0; i<n; i++)
        {
            ll mini= min(a[i],b[i]);
            ll maxi= max(a[i],b[i]);
            for(ll j=mini; j<=maxi; j++)
            {
                mp[j]++;
            }
        }
        
          ll count=0;
           for(auto it=mp.begin(); it!=mp.end(); it++)
           {
               if(it->second==n)
               count++;
           }
            cout<<count;
             return;
 }
 int main()
 {
     fast;  
     int t=1;
      //cin>>t;
     // seive();
      while(t--)
      {
        solve();  
         cout<<"\n";   
      }
       return 0;
     
 }
  