#include using namespace std; using ll = long long; using ld = long double; #define all(s) (s).begin(),(s).end() #define vcin(n) for(ll i=0;i>n[i] #define rever(vec) reverse(vec.begin(), vec.end()) #define sor(vec) sort(vec.begin(), vec.end()) #define fi first #define se second const ll mod = 998244353; //const ll mod = 1000000007; const ll inf = 2000000000000000000ll; static const long double pi = 3.141592653589793; template void chmax(T& t,const U& u){if(t void chmin(T& t,const U& u){if(t>u) t=u;} ll modPow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } int main() { /* mod は 1e9+7 */ ios::sync_with_stdio(false); std::cin.tie(nullptr); cout<< fixed << setprecision(10); ll n; cin>>n; assert(2<=n&&n<=100000); ll a[5][n-1]; for(int i=0;i<5;i++){ for(int j=0;j w; map ww; vector l(5),r(5); for(int i=0;i<5;i++){ cin>>l[i]>>r[i]; assert(1<=l[i]&&l[i]>a[i][j]; assert(1<=a[i][j]&&a[i][j]<=100000); if(i!=0&&a[i-1][j]!=0){ assert(a[i-1][j]>m; vector> s(5); for(int i=0;i>b>>si; assert(1<=b&&b<=5&&1<=si&&si<=1000000); b--; s[b].push_back(si); } for(int i=0;i<5;i++){ sor(s[i]); } ll ans=0; for(int i=4;i>=0;i--){ for(int j=i-1;j>=0;j--){ if(l[i]>r[j]||l[j]>r[i]){ continue; } if(l[i]<=l[j]&&r[j]<=r[i]){ for(int k=0;k