/* Problem URL: https://codeforces.com/problemset/problem/1775/D */ #include #include #include using namespace std; using namespace __gnu_pbds; template > using ordered_set = tree; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } const int oo = INT32_MAX >> 1; const ll OO = INT64_MAX >> 1; const int MAXN = 3e5 + 1; vi primes; int di[MAXN]; void pre() { fill(di, di + MAXN, 1); nrep(i, 2, MAXN) { if (di[i] != 1) { continue; } for (int j = i; j < MAXN; j += i) { di[j] = i; } } } #define TEST 0 void solve() { int n; cin >> n; vi a(n); cin >> a; int s, t; cin >> s >> t; s--, t--; if (s == t) { cout << "1\n"; cout << s + 1 << '\n'; return; } if (a[s] == 1 || a[t] == 1) { cout << "-1\n"; return; } if (a[s] == a[t]) { cout << "2\n"; cout << s + 1 << ' ' << t + 1 << '\n'; return; } V used(3e5 + 1); used[a[s]] = true; used[a[t]] = true; vvi graph(MAXN + n); auto getgraph = [&](int i) { int now = a[i]; while (now > 1) { int prev = di[now]; while (di[now] == prev) { now /= di[now]; } graph[prev].push_back(i + MAXN); graph[i + MAXN].push_back(prev); } }; getgraph(s); getgraph(t); rep(i, n) { // if ((i != s && a[i] == a[s]) || (i != t && a[i] == a[t])) { // continue; // } // pos[a[i]] = i; if (used[a[i]]) { continue; } used[a[i]] = true; getgraph(i); } // nrep(i, 2, 3e5 + 1) { // if (pos[i] == -1) { // continue; // } // // for (int j = i * 2; j <= 3e5; j += i) { // if (pos[j] == -1) { // continue; // } // // graph[pos[i]].push_back(pos[j]); // graph[pos[j]].push_back(pos[i]); // } // } vi dis(MAXN + n, oo); vi parent(MAXN + n, -1); queue q; dis[s + MAXN] = 0; q.push(s + MAXN); while (!q.empty()) { auto i = q.front(); q.pop(); for (auto j : graph[i]) { if (dis[j] > dis[i] + 1) { dis[j] = dis[i] + 1; parent[j] = i; q.push(j); } } } if (dis[t + MAXN] == oo) { cout << "-1\n"; return; } vi ans; int now = t + MAXN; while (parent[now] != -1) { if (now >= MAXN) { ans.push_back(now - MAXN + 1); } now = parent[now]; } ans.push_back(s + 1); reverse(all(ans)); cout << ans.size() << '\n'; cout << ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); pre(); int t; (TEST && cin >> t) || (t = 1); while (t--) { solve(); } }