#include #include using namespace atcoder; using namespace std; using ll=long long; using ld=long double; ld pie=3.141592653589793; ll mod=998244353; ll inf=1009999999999999990; int main(){ ll n,m,p; cin >> n >> m >> p; vectora(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } vectorb; mapmmm; for (ll i = 0; i < n; i++) { if (a[i]!=1&&mmm[a[i]]==0) { b.push_back(a[i]); mmm[a[i]]=1; } } n=b.size(); a=b; mapmemo; memo[1]=1; priority_queuepq; mapmm; pq.push(1); ll ans=32; ll now=0; while (!pq.empty()) { now++; if (now>n*30) { cout << -1 << endl; return 0; } ll v=pq.top(); pq.pop(); mm[v]=0; if (v>=m) { ans=min(ans,memo[v]); } if (ans<=memo[v]) { continue; } if (v%p==0) { v/=p; ll x=memo[v]; if (x==0||x>memo[v*p]+1) { memo[v]=memo[v*p]+1; if (mm[v]==0) { pq.push(v); mm[v]++; } } continue; } for (ll i = 0; i < n; i++) { ll x=memo[v*a[i]]; if (x==0||x>memo[v]+1) { memo[v*a[i]]=memo[v]+1; if (mm[v*a[i]]==0) { pq.push(v*a[i]); mm[v*a[i]]++; } } } } if (ans==32) { cout << -1 << endl; }else{ cout << ans-1 << endl; } }