import java.io.*;
public class Main {
public static boolean[] broken = new boolean[10];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
int ans = Math.abs(100-n);
if(m==0) {
ans = Math.min(ans, String.valueOf(n).length());
System.out.println(ans); return;
}
String[] arr = br.readLine().split(" ");
for(int i=0; i<m; i++) { broken[Integer.parseInt(arr[i])] = true; }
for(int i=0; i<=1_000_000; i++) {
if(check(i)==0) { continue; }
ans = Math.min(ans, Math.abs(i-n)+check(i));
}
System.out.println(ans);
}
public static int check(int i) {
int check = 0;
if(i==0) {
if(broken[i]) { return 0; }
else { return 1; }
}
while(i>0) {
int temp = i%10;
if(broken[temp]) { return 0; }
check++;
i /= 10;
}
return check;
}
}