#include <iostream>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<math.h>
#define int long long
const int inf = 1e17;
const int maxn = 300005;
const int mod = (int)1e9 + 7;
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m; cin >> n >> m;
vector<pair<int, int>> d;
vector<int> ans(n + 1, inf);
for (int i = 0; i < m; ++i) {
int x, y; cin >> x >> y;
if (i == 1) ans[x] = 0;
d.push_back({ x, y });
}
for (int i = 0; i < n; ++i)
for(auto x: d)
if (ans[x.first] == inf) {
for (int i = x.first, val = 0; i >= 0; i -= x.second, ++val)
ans[x.first] = min(ans[x.first], ans[i] + val);
for (int i = x.first, val = 0; i < n; i += x.second, ++val)
ans[x.first] = min(ans[x.first], ans[i] + val);
}
cout << ((ans[d[0].first] == inf) ? -1 : ans[d[0].first]);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |