#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int c; int n; #define MAX 100002 vector v; vector > dp[100002]; int su[MAX]; int main(){ scanf("%d", &c); scanf("%d", &n); for (int i = 0; i < n; i++){ int a; scanf("%d", &a); v.push_back(a); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()),v.end()); dp[0].push_back(make_pair(0, 0)); for (int i = 0; i < v.size(); i++){ for (int j = 0; j <= c; j++){ int ind = j%v[i]; int ind2 = upper_bound(dp[ind].begin(), dp[ind].end(), make_pair(j, INT_MAX)) - dp[ind].begin(); ind2--; if (ind2 < 0){ su[j] = INT_MAX; continue; } int val = dp[ind][ind2].first; int sa = abs(val - j); sa /= v[i]; sa += dp[ind][ind2].second; su[j] = sa; } for (int i = 0; i < MAX; i++){ dp[i].clear(); } if (i + 1 == v.size()){ break; } for (int j = 0; j <= c; j++){ if (su[j] == INT_MAX){ continue; } dp[j%v[i + 1]].push_back(make_pair(j,su[j])); } for (int j = 0; j < v[i + 1]; j++){ sort(dp[j].begin(), dp[j].end()); for (int k = 1; k < dp[j].size(); k++){ dp[j][k].second = min(dp[j][k].second, dp[j][k - 1].second + abs(dp[j][k - 1].first - dp[j][k].first) / v[i + 1]); } } } int ans = su[c]; if (ans == INT_MAX){ puts("-1"); return 0; } cout << ans << endl; return 0; }