(1회차 시도 성공!)
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
boolean[] arr = new boolean[1000];
for (int i = 123; i < 988; i++) {
String s = String.valueOf(i);
if(s.charAt(0) == '0' || s.charAt(1) == '0' || s.charAt(2) == '0'){
continue;
}
if(s.charAt(0) == s.charAt(1) || s.charAt(1) == s.charAt(2) || s.charAt(0) == s.charAt(2)){
continue;
}
arr[i] = true;
}
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int num = Integer.parseInt(st.nextToken());
int strike = Integer.parseInt(st.nextToken());
int ball = Integer.parseInt(st.nextToken());
for (int j = 123; j < 988; j++) {
if(arr[j]){
int strikeCount = 0;
int ballCount = 0;
for (int k = 0; k < 3; k++) {
String s1 = String.valueOf(num);
for (int l = 0; l < 3; l++) {
String s2 = String.valueOf(j);
if(s1.charAt(k) == s2.charAt(l) && k == l){
strikeCount++;
}
if(s1.charAt(k) == s2.charAt(l) && k!=l){
ballCount++;
}
}
}
if(strike == strikeCount && ball == ballCount){
arr[j] = true;
}else{
arr[j] = false;
}
}
}
}
int ans = 0;
for (int i = 123; i < 988; i++) {
if(arr[i]){
ans++;
}
}
bw.write(ans+"");
br.close();
bw.close();
}
}