링크 : https://www.acmicpc.net/problem/1107
/*
문제 : 리모컨
링크 : https://www.acmicpc.net/problem/1107
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<bool> v(10, false);
bool check(int n){
string s = to_string(n);
for(int i = 0; i < s.length(); i++){
if(v[s[i] - '0']) return false;
}
return true;
}
int main(){
int target;
cin >> target;
int n;
cin >> n;
for(int i = 0, tmp; i < n; i++){
cin >> tmp;
v[tmp] = true;
}
int min_num = abs(target-100);
for(int i = 0; i <= 1000000; i++){
if(check(i)){
int tmp = abs(target-i) + to_string(i).length();
min_num = min(min_num, tmp);
}
}
cout << min_num;
return 0;
}