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;
    }
}

'Problem Solving > BOJ' 카테고리의 다른 글

[Silver 1] 6064번 카잉 달력  (0) 2022.04.01
[Gold 5] 14500번 테트로미노  (0) 2022.04.01
[Silver 5] 1476번 날짜 계산  (0) 2022.04.01
[Silver 3] 3085번 사탕 게임  (0) 2022.04.01
[Gold 4] 17404번 RGB거리 2  (0) 2022.03.29