#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
long int str[100010];
int n;
int x,y;
int dp[10];
void work(int i)
{
y=str[i]-(str[i]/10)*10;
int a=str[i];
while(a>=10)
{
a=a/10;
}
x=a;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%ld",&str[i]);
for(int i=1;i<=n;i++)
{
work(i);
dp[y]=max(dp[y],dp[x]+1);
}
int max1=0;
for(int i=1;i<=9;i++)
max1=max(max1,dp[i]);
cout<<n-max1;
}