Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.util.HashSet;
Class Main {static char c[];
static String S1;
static int count = 0;
Static hashset<string> set = new hashset<> (); public static void Main (string[] args) throws IOException {BufferedReader br = new BufferedReader (New Inputstreamre
Ader (system.in));
S1 = Br.readline ();
int n = integer.parseint (Br.readline ());
while (n-->0) {String s2 = br.readline ();
c = S2.tochararray ();
Boolean b [] = new Boolean [8];
Char d[] = new CHAR[8];
f (d,b,0);
Count+=set.size ();
Set.clear ();
} System.out.println (count);
} private static void F (char[] D, boolean[] b, int i) {if (i==8) {String s = new String (d);
if (S1.contains (s)) Set.add (s);
Return
} hashset<character> Set2 = new hashset<> (); for (int j = 0; J < B.length; J +) {if (!b[j] && set2.add (c[j]) {B[j] = true;
D[i] = C[j];
f (d,b,i+1);
B[J] = false; }
}
}
}