# include <cstdio>
# include <iostream>
# include <algorithm>
# include <cstring>
# include <queue>
using namespace std;
const int N = 150, Inf = 0x3f3f3f3f;
struct node {
int x1, y1, x2, y2, co;
} a[N];
int n;
int mark[N][N], ans = Inf, vis[N];
bool ok[N];
bool pd(int x)
{
if(a[x].x1 == 1) return true;
for(int i = a[x].y1; i <= a[x].y2; i ++) {
if(!ok[mark[a[x].x1 - 1][i]]) return false;
}
return true;
}
void dfs(int co, int s, int step)
{
if(step >= ans) return;
if(s == (1 << (n - 1)) - 1) {
ans = min(ans, step);
return;
}
for(int i = 1; i <= n; i ++) {
if(pd(i) && (s & (1 << (i - 1))) == 0) {
ok[i] = true;
dfs(a[i].co, s | (1 << (i - 1)), step + (co == a[i].co ? 0 : 1));
ok[i] = false;
}
}
return;
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >>n;
for(int i = 1; i <= n; i ++) {
cin >>a[i].y1 >>a[i].x1 >>a[i].y2 >>a[i].x2 >>a[i].co;
a[i].x1 ++; a[i].y1 ++;
for(int j = a[i].x1; j <= a[i].x2; j ++) {
for(int k = a[i].y1; k <= a[i].y2; k ++) {
mark[j][k] = i;
}
}
}
for(int i = 1; i <= n; i ++) {
if(a[i].x1 == 1) {
ok[i] = true;
dfs(a[i].co, 1 << (i - 1), 1);
memset(ok, 0, sizeof(ok));
}
}
cout <<ans <<endl;
return 0;
}