//红太阳学长的代码片
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int a[20];
struct joker{
int p[20];
int ans;
void print()
{
for(int i=0;i<=14;i++)
for(int j=this->p[i];j>=1;j--)
printf("%d ",i);
printf("\n");
}
}A[105];
int Ans;
queue<joker> q;
void bfs()
{
joker B;
while(!q.empty())
{
B=q.front();q.pop();
if(B.ans>=Ans) return ;
bool emp=0;
for(int i=0;i<=14;i++)
if(B.p[i]){emp=1;break;}
if(!emp){Ans=min(Ans,B.ans);break;}
B.ans++;bool h=0;
for(int i=3;i<=10;i++)//单顺子
{
int j=0;
while(true)
{
j++;
if(!B.p[i+j-1])
break;
}j--;
if(j>=5)
{
int k=1;
while(k<=j)
{
B.p[i+k-1]--;
if(k>=5)q.push(B);
k++;
}k--;
while(k>=1)
{
B.p[i+k-1]++;
k--;
}
}
}
for(int i=3;i<=12;i++)//双顺子
{
int j=0;
while(true)
{
j++;
if(B.p[i+j-1]<2)
break;
}j--;
if(j>=3)
{
int k=1;
while(k<=j)
{
B.p[i+k-1]-=2;
if(k>=3)q.push(B);
k++;
}k--;
while(k>=1)
{
B.p[i+k-1]+=2;
k--;
}
}
}
for(int i=3;i<=13;i++)//三顺子
{
int j=0;
while(true)
{
j++;
if(B.p[i+j-1]<3)
break;
}j--;
if(j>=2)
{
int k=1;
while(k<=j)
{
B.p[i+k-1]-=3;
if(k>=2)q.push(B);
k++;
}k--;
while(k>=1)
{
B.p[i+k-1]+=3;
k--;
}
}
}
for(int i=2;i<=14;i++)//san dai
{
if(B.p[i]>=3)
{
B.p[i]-=3;
for(int j=0;j<=14;j++)//san dai 1
{
if(B.p[j])
{
B.p[j]--;
q.push(B);
B.p[j]++;
}
}
for(int j=0;j<=14;j++)//san dai 2
{
if(B.p[j]>=2)
{
B.p[j]-=2;
q.push(B);
B.p[j]+=2;
}
}
B.p[i]+=3;
}
}
for(int i=2;i<=14;i++)//si dai 2
{
if(B.p[i]>=4)
{
B.p[i]-=4;
for(int j=0;j<=14;j++)//san dai dan
{
if(B.p[j])
{
B.p[j]--;
for(int k=0;k<=j;k++)
{
if(B.p[k])
{
B.p[k]--;
q.push(B);
B.p[k]++;
}
}
B.p[j]++;
}
}
for(int j=0;j<=14;j++)//san dai shuang
{
if(B.p[j]>=2)
{
B.p[j]-=2;
for(int k=0;k<=j;k++)
{
if(B.p[k]>=2)
{
B.p[k]-=2;
q.push(B);
B.p[k]+=2;
}
}
B.p[j]+=2;
}
}
B.p[i]+=4;
}
}
int pi=0;
for(int i=0;i<=14;i++)
if(B.p[i]) pi++;
Ans=min(Ans,B.ans+pi-1);
}
}
void read(int &a)
{
a=0;
char c=getchar();
while(c>'9'||c<'0')
c=getchar();
while(c>='0'&&c<='9')
{
a*=10;
a+=c-'0';
c=getchar();
}
}
int main()
{
// freopen("landlords.in","r",stdin);
// freopen("landlords.out","w",stdout);
int T,n,x;
scanf("%d%d",&T,&n);
if(n==2)
{
while(T--)
{
scanf("%d%d",&a[1],&x);
scanf("%d%d",&a[2],&x);
if(a[1]==a[2])
puts("1");
else puts("2");
}
}
if(n==3)
{
while(T--)
{
scanf("%d%d",&a[1],&x);
scanf("%d%d",&a[2],&x);
scanf("%d%d",&a[3],&x);
if(a[1]==a[2]&&a[1]==a[3])
{ puts("1");continue; }
sort(a+1,a+3+1);
if(a[1]==a[2]||a[2]==a[3])
{ puts("2");continue; }
puts("3");
}
}
if(n==4)
{
while(T--)
{
scanf("%d%d",&a[1],&x);
scanf("%d%d",&a[2],&x);
scanf("%d%d",&a[3],&x);
scanf("%d%d",&a[4],&x);
if(a[1]==a[2]&&a[1]==a[3]&&a[1]==a[4])
{ puts("1");continue; }
sort(a+1,a+4+1);
if(a[1]==a[2]&&a[1]==a[3])
{ puts("1");continue;}
if(a[2]==a[3]&&a[2]==a[4])
{ puts("1");continue;}
if(a[1]==a[2]&&a[3]==a[4])
{ puts("2");continue;}
if(a[1]==a[2]||a[2]==a[3]||a[3]==a[4])
{ puts("3");continue;}
puts("4");
}
}
if(n>=5)
{
while(T--)
{
for(int i=1;i<=n;i++)
{
read(x);if(x==1) x=14;
A[T].p[x]++;
read(x);
}
while(!q.empty()) q.pop();
q.push(A[T]);Ans=n;
bfs();
printf("%d\n",Ans);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}