rt
#include<bits/stdc++.h>
using namespace std;
stack<int>q[15];
int ans,cnt,k[5],s[15];
int main(){
ios::sync_with_stdio(NULL);
cin.tie(0);cout.tie(0);
for(int i=1;i<=12;i++)
for(int j=1;j<=4;j++){
char c;cin>>c;int t;
if(c>'0'&&c<='9') t=c-'0';
else if(c=='0') t=10;
else if(c=='J') t=11;
else if(c=='Q') t=12;
else if(c=='K') t=13;
q[i].push(t);
}
for(int i=1;i<=4;i++){
char c;cin>>c;
if(c>'0'&&c<='9') k[i]=c-'0';
else if(c=='0') k[i]=10;
else if(c=='J') k[i]=11;
else if(c=='Q') k[i]=12;
else if(c=='K') k[i]=13;
}
while((cnt+1)<5){
int t=k[++cnt];
while(t<13){
ans+=(++s[t]==4);
int temp=t;
t=q[temp].top();
q[temp].pop();
}
}cout<<ans;
return 0;
}