#include <bits/stdc++.h>
using namespace std;
int cnt1,nex1[500005],las1[500005],Arrive1[500005],cnt2,nex2[500005],las2[500005],Arrive2[500005];
int N,M,Q,Bcc_cnt;
int w[500005];
multiset<int> S[500005];
int dfn[500005],Low[500005],Timew;
int stk[500005],tp;
void jt1(int x,int y){
	cnt1++;
	nex1[cnt1]=las1[x];
	las1[x]=cnt1;
	Arrive1[cnt1]=y;
}
void jt2(int x,int y){
	cnt2++;
	nex2[cnt2]=las2[x];
	las2[x]=cnt2;
	Arrive2[cnt2]=y;
}
void Tarjan(int Now){
	//cout<<Now<<endl;
	Low[Now]=dfn[Now]=++Timew;
	stk[++tp]=Now;
	for (int i=las1[Now];i;i=nex1[i]){
		if (!dfn[Arrive1[i]]){
			Tarjan(Arrive1[i]);
			Low[Now]=min(Low[Now],Low[Arrive1[i]]);
			if (Low[Arrive1[i]]==dfn[Now]){
				++Bcc_cnt;
				for (int x=0;x!=Arrive1[i];--tp){
					x=stk[tp];
					jt2(Bcc_cnt,x);
					jt2(x,Bcc_cnt);
				}
				jt2(Bcc_cnt,Now);
				jt2(Now,Bcc_cnt);
			}
		}
		else Low[Now]=min(Low[Now],dfn[Arrive1[i]]);
	}
}
int idf[500005],fa[500005],Siz[500005],dep[500005],Son[500005],top[500005],dfc;
void dfs1(int Now,int Fa){
	fa[Now]=Fa;dep[Now]=dep[Fa]+1;Siz[Now]=1;
	for (int i=las2[Now];i;i=nex2[i])
		if (Arrive2[i]!=Fa){
			dfs1(Arrive2[i],Now);
			Siz[Now]+=Siz[Arrive2[i]];
			if (Siz[Son[Now]]<Siz[Arrive2[i]]) Son[Now]=Arrive2[i];
		}
}
void dfs2(int Now,int Fa,int tp){
	dfn[Now]=++dfc,idf[dfc]=Now,top[Now]=tp;
	if (Son[Now]) dfs2(Son[Now],Now,tp);
	for (int i=las2[Now];i;i=nex2[i])
		if (Arrive2[i]!=Fa&&Arrive2[i]!=Son[Now])
			dfs2(Arrive2[i],Now,Arrive2[i]);
}
int Tree[1000005];
void Build(int Now,int l,int r){
	if (l==r) {Tree[Now]=w[idf[l]]; return;}
	int mid=(l+r)>>1;
	Build(Now<<1,l,mid),Build(Now<<1|1,mid+1,r);
	Tree[Now]=min(Tree[Now<<1],Tree[Now<<1|1]);
}
void Modify(int Now,int l,int r,int Pos,int x){
	if (l==r) {Tree[Now]=x;return;}
	int mid=(l+r)>>1;
	if (Pos<=mid) Modify(Now<<1,l,mid,Pos,x);
	else Modify(Now<<1|1,mid+1,r,Pos,x);
	Tree[Now]=min(Tree[Now<<1],Tree[Now<<1|1]);
}
int query(int Now,int l,int r,int L,int R){
	if (r<L||l>R) return 1e9+7;
	if (L<=l&&r<=R) return Tree[Now];
	int mid=(l+r)>>1;
	return min(query(Now<<1,l,mid,L,R),query(Now<<1|1,mid+1,r,L,R));
}
int main(){
	scanf("%d%d",&N,&M);
	scanf("%d",&Q);
	for (int i=1;i<=N;i++)
		scanf("%d",&w[i]);
	Bcc_cnt=N;
	for (int i=1;i<=M;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		jt1(u,v);
		jt1(v,u);
	}
	Tarjan(1),dfs1(1,0),dfc=0,dfs2(1,0,1);
	for (int i=1;i<=N;i++)
		if (fa[i]) S[fa[i]].insert(w[i]);
	for (int i=N+1;i<=Bcc_cnt;i++)
		w[i]=*S[i].begin();
	Build(1,1,Bcc_cnt);
	while (Q--){
		char opt[3];int x,y;
		scanf("%s%d%d",opt,&x,&y);
		if (*opt=='C'){
			Modify(1,1,Bcc_cnt,dfn[x],y);
			if (fa[x]){
				int u=fa[x];
				S[u].erase(S[u].lower_bound(w[x]));
				S[u].insert(y);
				if (w[u]!=*S[u].begin()){
					w[u]=*S[u].begin();
					Modify(1,1,Bcc_cnt,dfn[u],w[u]);
				}
			}
			w[x]=y;
		}
		else{
			int Ans=1e9+7;
			while (top[x]!=top[y]){
				if (dep[top[x]]<dep[top[y]]) swap(x,y);
			Ans=min(Ans,query(1,1,Bcc_cnt,dfn[top[x]],dfn[x]));
			x=fa[top[x]];
		  }
		  if (dfn[x]>dfn[y]) swap(x,y);
		  Ans=min(Ans,query(1,1,Bcc_cnt,dfn[x],dfn[y]));
		  if (x>N)
		  Ans=min(Ans,w[fa[x]]);
		  printf("%d\n",Ans);
		}
	}
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <fstream>

using namespace std;

#define LL long long
#define ull unsigned long long
#define N 310000
#define M 6100
#define MP make_pair
#define Pi acos(-1.0)
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#define ls (rt << 1)
#define rs (ls | 1)
#define md ((ll+rr)/2)
#define lson ls, ll, md
#define rson rs, md+1, rr
#define mod 1000000007
#define inf 0x3f3f3f3f
const LL INF = 1LL<<60;
#define sqr(x) ((x)*(x))
#define eps 1e-9
#define uLL unsidisned londis londis
#define ui unsidisned int
LL powmod(LL a,LL b) {LL res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
#define F(x) ((x)/3+((x)%3 == 1 ? 0 : tb))
#define dis(x) ((x)<tb ? (x)*3+1 : ((x) - tb)*3+2)
#define lowbit(x) ((x)&(-x))
#define fi first
#define se second
#define pii pair<int,int>
#define pli pair<LL,int>
#define pll pair<LL,LL>
#define PB push_back
#define mp make_pair
#define SZ(x) x.size()
#define tiii tuple<int,int,int>

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int ch[N][2], pre[N], rev[N], key[N], mi[N];
vector<int> g[N];

int n;

void update_rev(int x) {
    if(!x) return;
    swap(ch[x][0], ch[x][1]);
    rev[x] ^= 1;
}

bool rt(int x) {
    return pre[x] == 0 || (ch[pre[x]][0] != x && ch[pre[x]][1] != x);
}

void push_down(int x) {
    if(rev[x]) {
        update_rev(ch[x][0]);
        update_rev(ch[x][1]);
        rev[x] = 0;
    }
}

void P(int x) {
    if(!rt(x)) P(pre[x]);
    push_down(x);
}

void push_up(int x) {
    mi[x] = min(key[x], min(mi[ch[x][0]], mi[ch[x][1]]));
}

void rot(int x) {
    int y = pre[x], d = ch[y][1] == x;
    if(!rt(y)) ch[pre[y]][ch[pre[y]][1]==y] = x;

    ch[y][d] = ch[x][!d];
    pre[ch[x][!d]] = y;
    ch[x][!d] = y;
    pre[x] = pre[y];
    pre[y] = x;
    push_up(y);
}
void splay(int x) {
    P(x);
    while(!rt(x)) {
        int f = pre[x], ff = pre[f];
        if(rt(f)) rot(x);
        else if((ch[ff][1] == f) == (ch[f][1] == x))
            rot(f), rot(x);
        else rot(x), rot(x);
    }
    push_up(x);
}

void Access(int x) {
    for(int y = 0; x; y = x, x = pre[x]) {
        splay(x);
        //维护子树需要修改信息
        ch[x][1] = y;
        push_up(x);
    }
}

void make_root(int x) {
    Access(x);
    splay(x);
    update_rev(x);
}

void link(int x, int y) {
    make_root(x);

    // 维护子树需要修改信息
    pre[x] = y;
}

bool judge(int x, int y) {
    while(pre[x]) x = pre[x];
    while(pre[y]) y = pre[y];
    return x == y;
}

void cut(int x, int y) {
    make_root(x);

    Access(y);
    splay(y);

    pre[ch[y][0]] = 0;
    ch[y][0] = 0;
    pre[y] = 0;
    push_up(y);
}

//调用后x是原来x和y的lca,y和ch[x][1]分别存着lca的2个儿子
//(原来x和y所在的2颗子树)
//可用于不换根的边权LCT

void update(int x, int w) {
    //cout  << x << " -- " << w << endl;
    Access(x);
    splay(x);
    key[x] = w;
    push_up(x);
    return;
}

int qquery(int x, int y) {
    //cout << x << " -- " << y << endl;
    make_root(x);
    Access(y);
    splay(y);
    return mi[y];
}

void init()
{
    for(int i = 1; i <= n; i++) g[i].clear();
}

int fa[N], _fa[N], belong[N];
vector<int> lcaq[N];
vector<pii> al[N];

struct query
{
    int type, u, v;
    query(){}
    query(int _t, int _u, int _v) : type(_t), u(_u), v(_v) {}
}qq[N];

int dfn[N], low[N], lab[N];

int find_fa(int u)
{
    return _fa[u] == u ? _fa[u] : _fa[u] = find_fa(_fa[u]);
}

int find_be(int u)
{
    return belong[u] == u ? belong[u] : belong[u] = find_be(belong[u]);
}

int lca[N];

int in[N], tot, val[N], now, bbc[N];
multiset<int> ss[N];

void dfs(int u, int f = 0)
{
    in[u] = 1;
    dfn[u] = low[u] = ++tot;
    lab[tot] = u;
    fa[u] = f;
    if(f) pre[u] = f;

    for(auto x : al[u])
    {
        if(!dfn[x.fi]) continue;

        if(in[x.fi]) lca[x.se] = x.fi;
        else
        {
            int t = find_fa(x.fi);
            lca[x.se] = fa[t];
            if(low[t] < dfn[lca[x.se]]) bbc[x.se] = 1;
        }
        lcaq[lca[x.se]].PB(x.se);
    }

    lcaq[u].clear();

    for(auto v : g[u])
    {
        if(v == f) continue;
        if(!dfn[v])
        {
            dfs(v, u);
            chkmin(low[u], low[v]);
            if(low[v] < dfn[u])
            {
                for(auto w : lcaq[u]) bbc[w] = 1;
                belong[v] = u;
            }
            else
            {
                ++now;
                belong[now] = belong[v] = now;
                pre[v] = now;
                pre[now] = u;

            }
            lcaq[u].clear();
        }
        else chkmin(low[u], dfn[v]);
    }

    for(auto v : g[u])
    {
        if(fa[v] == u) _fa[v] = u;
    }
    in[u] = 0;
}

int main()
{
    memset(mi, 0x3f, sizeof mi);
    memset(key, 0x3f, sizeof key);
    int m , q;
    scanf("%d%d%d",&n,&m,&q);
    for(int i = 1; i <= n; i++)
        scanf("%d", &val[i]);
    for(int i = 0; i < m; i++)
    {
        int u, v;
        scanf("%d%d",&u,&v);
        g[u].PB(v);
        g[v].PB(u);
    }

    for(int i = 0; i < q; i++)
    {
        char s[10];
        scanf(" %s",s);
        if(s[0] == 'A')
        {
            qq[i].type = 0;
            scanf("%d%d",&qq[i].u,&qq[i].v);
            al[qq[i].u].PB({qq[i].v, i});
            al[qq[i].v].PB({qq[i].u, i});
        }
        else
        {
            qq[i].type = 1;
            scanf("%d%d",&qq[i].u,&qq[i].v);
        }
    }

    for(int i = 1; i <= n; i++) _fa[i] = i;
    tot = 0;
    now = n;
    belong[1] = 1;
    dfs(1);

    for(int i = 1; i <= now; i++) find_be(i);

    for(int i = 1; i <= n; i++) ss[belong[i]].insert(val[i]);
    for(int i = 1; i <= now; i++)
        key[i] = (belong[i] == i ? *ss[i].begin() : val[i]),
        push_up(i);
    //for(int i = 1; i <= now; i++)
        //cout << key[i] << endl;
    for(int i = 0; i < q; i++)
    {
        if(qq[i].type)
        {
            int tmp = belong[qq[i].u];
            update(qq[i].u, qq[i].v);
            int a = *ss[tmp].begin();
            ss[tmp].erase(ss[tmp].find(val[qq[i].u]));
            val[qq[i].u] = qq[i].v;
            ss[tmp].insert(val[qq[i].u]);
            int b = *ss[tmp].begin();
            if(a != b) update(tmp, b);
        }
        else
        {
            int ans = qquery(qq[i].u, qq[i].v);
            //cout << ans << endl;
            //cout << lca[i] << endl;
            //cout << *ss[belong[lca[i]]].begin() << " " << val[lab[low[lca[i]]]] << endl;
           // cout << bbc[i] << endl;
            if (bbc[i]) chkmin(ans, min(*ss[belong[lca[i]]].begin(), val[lab[low[lca[i]]]]));
            printf("%d\n", ans);

        }
    }

}


#include <bits/stdc++.h>
using namespace std;

#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9, MOD = INF + 7;

/////////////////////////////////////////////////////////////////////

const int N = 1e3, M = 2e3 + 5;

bool marked[M];
int dis[M];

int main() {

	sync;

	int n, k; cin >> n >> k;
	vector<int> cok;
	for(int i = 0; i < k; i++) {
		int a; cin >> a; a = a - n + N;
		if (!marked[a]) cok.push_back(a);
		marked[a] = true;
	}

	memset(dis, 63, sizeof dis);
	dis[0 + N] = 0;
	queue<int> q; q.push(0 + N);
	while(!q.empty()) {
		int v = q.front(); q.pop();
		for(int u : cok) {
			int w = v + u - N;
			if (w == 0 + N) die(dis[v] + 1);
			if (dis[v] + 1 < dis[w]) {
				dis[w] = dis[v] + 1;
				q.push(w);
			}
		}
	}
	cout << -1 << endl;

  	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9, MOD = INF + 7;

/////////////////////////////////////////////////////////////////////

const int N = 1e3, M = 2e3 + 5;

bool marked[M];
int dis[2 * M];

int main() {

	sync;

	int n, k; cin >> n >> k;
	vector<int> cok;
	for(int i = 0; i < k; i++) {
		int a; cin >> a; a = a - n + N;
		if (!marked[a]) cok.push_back(a);
		marked[a] = true;
	}

	memset(dis, 63, sizeof dis);
	dis[0 + N] = 0;
	queue<int> q; q.push(0 + N);
	while(!q.empty()) {
		int v = q.front(); q.pop();
		for(int u : cok) {
			int w = v + u - N;
			if (w == 0 + N) die(dis[v] + 1);
			if (dis[v] + 1 < dis[w]) {
				dis[w] = dis[v] + 1;
				q.push(w);
			}
		}
	}
	cout << -1 << endl;

  	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define sync ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p) cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9, MOD = INF + 7;

/////////////////////////////////////////////////////////////////////

const int N = 1e3, M = 2e3 + 5;

bool marked[M];
int dis[M];

int main() {

	sync;

	int n, k; cin >> n >> k;
	vector<int> cok;
	for(int i = 0; i < k; i++) {
		int a; cin >> a; a = a - n + N;
		if (!marked[a]) cok.push_back(a);
		marked[a] = true;
	}

	memset(dis, 63, sizeof dis);
	dis[0 + N] = 0;
	queue<int> q; q.push(0 + N);
	while(!q.empty()) {
		int v = q.front(); q.pop();
		for(int u : cok) {
			int w = v + u - N;
			if (w == 0 + N) die(dis[v] + 1);
			if (w < M && w >= 0 && dis[v] + 1 < dis[w]) {
				dis[w] = dis[v] + 1;
				q.push(w);
			}
		}
	}
	cout << -1 << endl;

  	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double lld;

#define F first
#define S second
#define sz(x) (int)x.size()
#define MP make_pair
#define PB push_back

int inf=1e9+7;

int n, k;
set <int> s;
map <int, int> p;

void bfs(int v){
	queue <int> q;
	q.push(v);
	while (!q.empty()){
		int v=q.front();
		q.pop();
		if (p[v]>2000)
			break;
		for (auto dif : s){
			int to=v+dif;
			if (!to){
				printf("%d", p[v]+1);
				exit(0);
			}
			if (p[to])
				continue;
			p[to]=p[v]+1;
			q.push(to);
		}
	}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d %d", &n, &k);
	for (int i=0; i<k; ++i){
		int x;
		scanf("%d", &x);
		s.insert(x-n);
	}
	bfs(0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double lld;

#define F first
#define S second
#define sz(x) (int)x.size()
#define MP make_pair
#define PB push_back

int inf=1e9+7;

int n, k;
set <int> s;
map <int, int> p;

void bfs(int v){
	queue <int> q;
	q.push(v);
	while (!q.empty()){
		int v=q.front();
		q.pop();
		if (p[v]>1000)
			break;
		for (auto dif : s){
			int to=v+dif;
			if (!to){
				printf("%d", p[v]+1);
				exit(0);
			}
			if (p[to])
				continue;
			p[to]=p[v]+1;
			q.push(to);
		}
	}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d %d", &n, &k);
	for (int i=0; i<k; ++i){
		int x;
		scanf("%d", &x);
		s.insert(x-n);
	}
	bfs(0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double lld;

#define F first
#define S second
#define sz(x) (int)x.size()
#define MP make_pair
#define PB push_back

int inf=1e9+7;

int n, k;
set <int> s;
map <int, int> p;

void bfs(int v){
	queue <int> q;
	q.push(v);
	while (!q.empty()){
		int v=q.front();
		q.pop();
		for (auto dif : s){
			int to=v+dif;
			if (!to){
				printf("%d", p[v]+1);
				exit(0);
			}
			if (p[to] || abs(to)>1000)
				continue;
			p[to]=p[v]+1;
			q.push(to);
		}
	}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d %d", &n, &k);
	for (int i=0; i<k; ++i){
		int x;
		scanf("%d", &x);
		s.insert(x-n);
	}
	bfs(0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double lld;

#define F first
#define S second
#define sz(x) (int)x.size()
#define MP make_pair
#define PB push_back

int inf=1e9+7;

int n, k;
set <int> s;
map <int, int> p;

void bfs(int v){
	queue <int> q;
	q.push(v);
	while (!q.empty()){
		int v=q.front();
		q.pop();
		for (auto dif : s){
			int to=v+dif;
			if (!to){
				printf("%d", p[v]+1);
				exit(0);
			}
			if (p[to])
				continue;
			p[to]=p[v]+1;
			q.push(to);
		}
	}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d %d", &n, &k);
	for (int i=0; i<k; ++i){
		int x;
		scanf("%d", &x);
		s.insert(x-n);
	}
	bfs(0);
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double lld;

#define F first
#define S second
#define sz(x) (int)x.size()
#define MP make_pair
#define PB push_back

int inf=1e9+7;

int n, k;
set <int> s;
map <int, int> p;

void bfs(int v){
	queue <int> q;
	q.push(v);
	while (!q.empty()){
		int v=q.front();
		q.pop();
		if (p[v]>500)
			break;
		for (auto dif : s){
			int to=v+dif;
			if (!to){
				printf("%d", p[v]+1);
				exit(0);
			}
			if (p[to])
				continue;
			p[to]=p[v]+1;
			q.push(to);
		}
	}
}

int main(){
	#ifdef ADIKON
		freopen("problem.in", "r", stdin);
		freopen("problem.out", "w", stdout);
	#endif
	scanf("%d %d", &n, &k);
	for (int i=0; i<k; ++i){
		int x;
		scanf("%d", &x);
		s.insert(x-n);
	}
	bfs(0);
	puts("-1");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define dbg(x)  cout<<#x<<"="<<x<<endl
#define N 600006
#define MOD 1e18
#define pb push_back
#define iosbase  ios_base::sync_with_stdio(false)
#define dbg(x)  cout<<#x<<"="<<x<<endl

int pos[N];
queue <int> q;
vector<int> adj;

int A(int k){
	return k+2000;
}
int main(){

	iosbase;
	int n,k,p;
	cin>>n>>k;
	for(int i=0;i<k;i++){

		cin>>p;
		if(pos[A(p-n)]==1)
			continue;
		adj.push_back((p-n));
		pos[A(p-n)]=1;
		q.push(A(p-n));
	}

	while(!q.empty()){

		p=q.front();
		q.pop();

		for(int i=0;i<adj.size();i++){
			int nxt=p+adj[i];
			if( nxt <A(-1000) || nxt >A(1000) || pos[nxt]!=0)
				continue;
			pos[nxt]=pos[p]+1;
			q.push(nxt);
		}
	}
	if(pos[A(0)]==0)
		cout<<"-1\n";
	else
		cout<<pos[A(0)]<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define dbg(x)  cout<<#x<<"="<<x<<endl
#define N 600006
#define MOD 1e18
#define pb push_back
#define iosbase  ios_base::sync_with_stdio(false)
#define dbg(x)  cout<<#x<<"="<<x<<endl

int pos[N];
queue <int> q;
vector<int> adj;

int A(int k){
	return k+2000;
}
int main(){

	int n,k,p;
	cin>>n>>k;
	for(int i=0;i<k;i++){

		cin>>p;
		if(pos[A(p-n)]==1)
			continue;
		adj.push_back((p-n));
		pos[A(p-n)]=1;
		q.push(A(p-n));
	}

	while(!q.empty()){

		p=q.front();
		q.pop();

		for(int i=0;i<adj.size();i++){
			int nxt=p+adj[i];
			if( nxt <A(-1000) || nxt >A(1000) || pos[nxt]!=0)
				continue;
			pos[nxt]=pos[p]+1;
			q.push(nxt);
		}
	}
	if(pos[A(0)]==0)
		cout<<"-1\n";
	else
		cout<<pos[A(0)]<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define dbg(x)  cout<<#x<<"="<<x<<endl
#define N 600006
#define MOD 1e18
#define pb push_back
#define iosbase  ios_base::sync_with_stdio(false)
#define dbg(x)  cout<<#x<<"="<<x<<endl

int pos[N];
queue <int> q;
vector<int> adj;

int A(int k){
	return k+2000;
}
int main(){

	iosbase;
	int n,k,p;
	cin>>n>>k;
	for(int i=0;i<k;i++){

		cin>>p;
		adj.push_back((p-n));
		pos[A(p-n)]=1;
		q.push(A(p-n));
	}

	while(!q.empty()){

		p=q.front();
		q.pop();

		for(int i=0;i<adj.size();i++){
			int nxt=p+adj[i];
			if( nxt <A(-1000) || nxt >A(1000) || pos[nxt]!=0)
				continue;
			pos[nxt]=pos[p]+1;
			q.push(nxt);
		}
	}
	if(pos[A(0)]==0)
		cout<<"-1\n";
	else
		cout<<pos[A(0)]<<endl;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define dbg(x)  cout<<#x<<"="<<x<<endl
#define N 600006
#define MOD 1e18
#define pb push_back
#define iosbase  ios_base::sync_with_stdio(false)
#define dbg(x)  cout<<#x<<"="<<x<<endl

int pos[N];
queue <int> q;
vector<int> adj;

int A(int k){
	return k+2000;
}
int main(){

	int n,k,p;
	cin>>n>>k;
	for(int i=0;i<k;i++){

		cin>>p;
		adj.push_back((p-n));
		pos[A(p-n)]=1;
		q.push(A(p-n));
	}

	while(!q.empty()){

		p=q.front();
		q.pop();

		for(int i=0;i<adj.size();i++){
			int nxt=p+adj[i];
			if( nxt <A(-1000) || nxt >A(1000) || pos[nxt]!=0)
				continue;
			pos[nxt]=pos[p]+1;
			q.push(nxt);
		}
	}
	if(pos[A(0)]==0)
		cout<<"-1\n";
	else
		cout<<pos[A(0)]<<endl;
}


#include <bits/stdc++.h>

using namespace std;

bool ap[2005];
int n,k;

int gcd(int a,int b) {return b ? gcd(b,a % b) : a;}

int main()
{
	scanf("%d%d", &n, &k);
	int ad = 1000;
	for(int i = 1,v;i <= k;i ++)
		scanf("%d", &v), ap[v - n + ad] = 1;
	static int que[2005],d[2005];
	memset(d,60,sizeof d);
	d[0 + ad] = 0;
	que[1] = 0 + ad;
	for(int fi = 1,en = 1;fi <= en;fi ++)
	{
		int u = que[fi];
		for(int i = -1000;i <= 1000;i ++)
			if (ap[i + ad] && u + i <= 2000 && u + i >= 0)
			{
				if (u + i == ad) {printf("%d\n", d[u] + 1);return 0;}
				if (d[u] + 1 < d[u + i]) d[u + i] = d[u] + 1,que[++ en] = u + i;
			}
	}
	printf("-1\n");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool ap[1005];
int n,k;

int gcd(int a,int b) {return b ? gcd(b,a % b) : a;}

int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 1,v;i <= k;i ++)
		scanf("%d", &v), ap[v] = 1;
	if (!n) {printf("0\n");return 0;}
	if (ap[n]) {printf("1\n");return 0;} else
	{
		int ans = (1 << 30);
		for(int i = 0;i < n;i ++)
			if (ap[i])
				for(int j = n + 1;j <= 1000;j ++)
					if (ap[j])
					{
						int x = n - i,y = j - n;
						ans = min(ans,(x + y) / gcd(x,y));
					}
		if (ans == (1 << 30)) ans = -1;
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool ap[2005];
int n,k;

int gcd(int a,int b) {return b ? gcd(b,a % b) : a;}

int main()
{
	scanf("%d%d", &n, &k);
	int ad = 1000;
	for(int i = 1,v;i <= k;i ++)
		scanf("%d", &v), ap[v - n + ad] = 1;
	static int que[2005],d[2005];
	memset(d,60,sizeof d);
	d[0 + ad] = 0;
	que[1] = 0 + ad;
	for(int fi = 1,en = 1;fi <= en;fi ++)
	{
		int u = que[fi];
		for(int i = -1000;i <= 1000;i ++)
			if (ap[i + ad])
			{
				if (u + i == ad) {printf("%d\n", d[u] + 1);return 0;}
				if (d[u] + 1 < d[u + i]) d[u + i] = d[u] + 1,que[++ en] = u + i;
			}
	}
	printf("-1\n");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool ap[1005];
int n,k;

int gcd(int a,int b) {return b ? gcd(b,a % b) : a;}

int main()
{
	scanf("%d%d", &n, &k);
	for(int i = 1,v;i <= k;i ++)
		scanf("%d", &v), ap[v] = 1;
	if (ap[n]) {printf("1\n");return 0;} else
	{
		int ans = (1 << 30);
		for(int i = 0;i < n;i ++)
			if (ap[i])
				for(int j = n + 1;j <= 1000;j ++)
					if (ap[j])
					{
						int x = n - i,y = j - n;
						ans = min(ans,(x + y) / gcd(x,y));
					}
		if (ans == (1 << 30)) ans = -1;
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

static char fjf[1010];
char gc() {static char *L = fjf, *R = L;
	if(L == R) {int n = fread(fjf, 1, 1010, stdin);
		if(!n) return -1; L = fjf, R = L + n;
	} return *L++;
}
void gn(int &x) {char c;
	while(c = gc(), (c < 48 || 57 < c) && ~c);
	for(x = c - 48; c = gc(), 47 < c && c < 58; x = 10 * x + c - 48);
}

const int NN = 1010, N = 1000, inf = 1e9;
int a[NN * NN], L[NN][NN << 1], R[NN][NN << 1];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	srand(time(0));

	int k, n, i, j, J, u;
	gn(k), gn(n); if(!k) puts("0"), exit(0);
	for(i = 1; i <= n; i++) {
		gn(a[i]), a[i] -= k;
		if(!a[i]) puts("1"), exit(0);
	}
	sort(a + 1, a + n + 1);
	n = unique(a + 1, a + n + 1) - a - 1;
	random_shuffle(a + 1, a + n + 1);

	for(i = -N; i <= N; i++) L[0][NN + i] = R[n + 1][NN + i] = inf;
	L[0][NN] = R[n + 1][NN] = 0;

	for(i = 1; i <= n; i++) {
		for(j = -N; j <= N; j++) L[i][NN + j] = L[i - 1][NN + j];
		for(j = -N; j <= N; j++) {
			for(k = 1, u = a[i]; ; k++) {
				J = j - u; if(J < -N || N < J) break; u += a[i];
				L[i][NN + j] = min(L[i][NN + j], L[i - 1][NN + J] + k);
			}
		}
	}
	for(i = n; i; i--) {
		for(j = -N; j <= N; j++) R[i][NN + j] = R[i + 1][NN + j];
		for(j = -N, J; j <= N; j++) {
			for(k = 1, u = a[i]; ; k++) {
				J = j - u; if(J < -N || N < J) break; u += a[i];
				R[i][NN + j] = min(R[i][NN + j], R[i + 1][NN + J] + k);
			}
		}
	}

	int ans = inf;
	for(i = 1; i < n; i++) {
		for(j = -N; j <= N; j++) if(j) {
			ans = min(ans, L[i][NN + j] + R[i + 1][NN - j]);
		}
	}
	printf("%d", ans < inf ? ans : -1);
}


#include <bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;

const int NN = 1010100;

map<int, int> mp;
int a[NN], dp[NN];
queue<pii> q;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int p, n; cin>>p>>n;
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	sort(a+1, a+n+1);
	n = unique(a+1, a+n+1) - a-1;
	for(int i=1; i<=n; i++) a[i] -= p;

	for(int i=1; i<=n; i++) {
		if(!a[i]) {
			puts("1");
			return 0;
		}
		q.push(pii(a[i], 1));
		mp[a[i]] = 1;
	}

	while(!q.empty()) {
		auto p = q.front(); q.pop();
		int u = p.first, w = p.second;
		for(int i=1; i<=n; i++) {
			int v = u + a[i];
			if(v <= 1000 && v >= -1000) {
				if(!mp[v]) q.push(pii(v, w+1));
				mp[v] = 1;
				if(!v) {
					cout<<w+1<<endl;
					return 0;
				}
			}
		}
	}

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

static char fjf[1010];
char gc() {static char *L = fjf, *R = L;
	if(L == R) {int n = fread(fjf, 1, 1010, stdin);
		if(!n) return -1; L = fjf, R = L + n;
	} return *L++;
}
void gn(int &x) {char c;
	while(c = gc(), (c < 48 || 57 < c) && ~c);
	for(x = c - 48; c = gc(), 47 < c && c < 58; x = 10 * x + c - 48);
}

const int NN = 1010, N = 1000, inf = 1e9;
int a[NN * NN], L[NN][NN << 1], R[NN][NN << 1];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int k, n, i, j, J, u;
	gn(k), gn(n); if(!k) puts("0"), exit(0);
	for(i = 1; i <= n; i++) {
		gn(a[i]), a[i] -= k;
		if(!a[i]) puts("1"), exit(0);
	}
	sort(a + 1, a + n + 1);
	n = unique(a + 1, a + n + 1) - a - 1;

	for(i = -N; i <= N; i++) L[0][NN + i] = R[n + 1][NN + i] = inf;
	L[0][NN] = R[n + 1][NN] = 0;

	for(i = 1; i <= n; i++) {
		for(j = -N; j <= N; j++) L[i][NN + j] = inf;
		for(j = -N; j <= N; j++) {
			for(k = u = 0; ; k++) {
				J = j - u; if(J < -N || N < J) break; u += a[i];
				L[i][NN + j] = min(L[i][NN + j], L[i - 1][NN + J] + k);
			}
		}
	}
	for(i = n; i; i--) {
		for(j = -N; j <= N; j++) R[i][NN + j] = inf;
		for(j = -N, J; j <= N; j++) {
			for(k = u = 0; ; k++) {
				J = j - u; if(J < -N || N < J) break; u += a[i];
				R[i][NN + j] = min(R[i][NN + j], R[i + 1][NN + J] + k);
			}
		}
	}

	int ans = inf;
	for(i = 1; i < n; i++) {
		for(j = -N; j <= N; j++) if(j) {
			ans = min(ans, L[i][NN + j] + R[i + 1][NN - j]);
		}
	}
	printf("%d", ans < inf ? ans : -1);
}


#include<bits/stdc++.h>

using namespace std;

using INT = long long;

#define NN 2020
#define inf 0x3f3f3f3f

int N = 1000;
int dp[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n, k;
	cin >> n >> k;
	for(int i=0; i<NN; i++) {
		dp[i] = inf;
	}

	queue<int> q;
	vector<int> tot;
	for(int i=1; i<=k; i++) {
		int u; scanf("%d", &u);
		if(dp[u-n+N] == inf) {
			tot.push_back(u - n);
			dp[u - n + N] = 1;
			q.push(u - n + N);
		}
	}

	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int x : tot) {
			if(x + u >= 0 and x + u < NN and dp[x+u] > dp[u] + 1) {
				dp[x+u] = dp[u] + 1;
				q.push(x+u);
			}
		}
	}

	if(dp[N] == inf) cout << -1 << endl;
	else cout << dp[N] << endl;
}


#include<bits/stdc++.h>

using namespace std;

static char fjf[1010];
char gc() {static char *L = fjf, *R = L;
	if(L == R) {int n = fread(fjf, 1, 1010, stdin);
		if(!n) return -1; L = fjf, R = L + n;
	} return *L++;
}
void gn(int &x) {char c;
	while(c = gc(), (c < 48 || 57 < c) && ~c);
	for(x = c - 48; c = gc(), 47 < c && c < 58; x = 10 * x + c - 48);
}

const int NN = 1010, N = 1000, inf = 1e9;
int a[NN * NN], L[NN][NN << 1], R[NN][NN << 1];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int k, n, i, j, J, u;
	gn(k), gn(n); if(!k) puts("0"), exit(0);
	for(i = 1; i <= n; i++) {
		gn(a[i]), a[i] -= k;
		if(!a[i]) puts("1"), exit(0);
	}
	sort(a + 1, a + n + 1);
	n = unique(a + 1, a + n + 1) - a - 1;

	for(i = -N; i <= N; i++) L[0][NN + i] = R[n + 1][NN + i] = inf;
	L[0][NN] = R[n + 1][NN] = 0;

	for(i = 1; i <= n; i++) {
		for(j = -N; j <= N; j++) L[i][NN + j] = L[i - 1][NN + j];
		for(j = -N; j <= N; j++) {
			for(k = u = 0; ; k++) {
				J = j - u; if(J < -N || N < J) break; u += a[i];
				L[i][NN + j] = min(L[i][NN + j], L[i - 1][NN + J] + k);
			}
		}
	}
	for(i = n; i; i--) {
		for(j = -N; j <= N; j++) R[i][NN + j] = R[i + 1][NN + j];
		for(j = -N, J; j <= N; j++) {
			for(k = u = 0; ; k++) {
				J = j - u; if(J < -N || N < J) break; u += a[i];
				R[i][NN + j] = min(R[i][NN + j], R[i + 1][NN + J] + k);
			}
		}
	}

	int ans = inf;
	for(i = 1; i < n; i++) {
		for(j = -N; j <= N; j++) if(j) {
			ans = min(ans, L[i][NN + j] + R[i + 1][NN - j]);
		}
	}
	printf("%d", ans < inf ? ans : -1);
}


#include<bits/stdc++.h>
#define NN 1001
#define inf 1e9

using namespace std;

map<int, int> mp;
int a[NN];
queue<int> que;

bool out(int val){
	return val < -1000 || val > 1000;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n, k;
	cin >> n >> k;
	for(int i = 1; i <= k; i++) cin >> a[i], a[i] -= n;
	sort(a + 1, a + k);
	k = unique(a + 1, a + k + 1) - a - 1;
	for(int i = -1000; i <= 1000; i++) mp[i] = inf;
	for(int i = 1; i <= k; i++) mp[a[i]] = 1, que.push(a[i]);
	while(!que.empty()){
		int u = que.front();
		que.pop();
		for(int i = 1; i <= k; i++){
			int v = u + a[i];
			if(out(v)) continue;
			if(mp[v] > mp[u] + 1){
				mp[v] = mp[u] + 1;
				que.push(v);
			}
		}
	}
	if(mp[0] == inf) cout<<"-1"<<endl;
	else cout<<mp[0]<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline void in(int&x){char c;while(c=getchar(),c<=' ');bool sign(c=='-');if(sign)c=getchar();x=(c-'0');while(c=getchar(),!(c<'0'|c>'9'))x=10*x+c-'0';if(sign)x=-x;}
const int N = 2001;
int a[N], c[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n, k; in(n), in(k);
	queue<pair<int, int>> q;
	for(int i=0, j; i<k; i++) {
		in(j);
		if(!c[j+1000-n]) {
			c[j+1000-n] = 1;
			a[j+1000-n] = 1;
			q.push({j+1000-n, 1});
		}
	}
	while(!q.empty()) {
		int u = q.front().first, d = q.front().second; q.pop();
		if(u == 1000) {
			cout<<d<<endl;
			return 0;
		}
		for(int i=0, j; i<N; i++) if(c[i]) {
			j = u+i-1000;
			if(0<=j && j<N && !a[j]) {
				a[j] = 1;
				q.push({j, d+1});
			}
		}
	}
	puts("-1");
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline void in(int&x){char c;while(c=getchar(),c<=' ');bool sign(c=='-');if(sign)c=getchar();x=(c-'0');while(c=getchar(),!(c<'0'|c>'9'))x=10*x+c-'0';if(sign)x=-x;}
const int N = 2001;
int a[N], c[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n, k; in(n), in(k);
	queue<pair<int, int>> q;
	for(int i=0, j; i<k; i++) {
		in(j);
		if(!c[j+1000-n]) {
			c[j+1000-n] = 1;
			a[j+1000-n] = 1;
			q.push({j+1000-n, 1});
		}
	}
	while(1) {
		int u = q.front().first, d = q.front().second; q.pop();
		if(u == 1000) {
			cout<<d<<endl;
			return 0;
		}
		for(int i=0, j; i<N; i++) if(c[i]) {
			j = u+i-1000;
			if(0<=j && j<N && !a[j]) {
				a[j] = 1;
				q.push({j, d+1});
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define NN 2010
#define inf 1e9

using namespace std;

map<int, int> mp;
int a[NN];
queue<int> que;

bool out(int val){
	return val < -1000 || val > 1000;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base :: sync_with_stdio(0);cin.tie(0);
	int n, k;
	cin >> n >> k;
	for(int i = 1; i <= k; i++) cin >> a[i], a[i] -= n;
	sort(a + 1, a + k);
	k = unique(a + 1, a + k + 1) - a - 1;
	for(int i = -1000; i <= 1000; i++) mp[i] = inf;
	for(int i = 1; i <= k; i++) mp[a[i]] = 1, que.push(a[i]);
	while(!que.empty()){
		int u = que.front();
		que.pop();
		for(int i = 1; i <= k; i++){
			int v = u + a[i];
			if(out(v)) continue;
			if(mp[v] > mp[u] + 1){
				mp[v] = mp[u] + 1;
				que.push(v);
			}
		}
	}
	if(mp[0] == inf) cout<<"-1"<<endl;
	else cout<<mp[0]<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;

#define NN 1000010
int a[NN], b[NN];
int f[NN], g[NN];
int tmp[NN];
int ansf[NN], ansg[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int sv, n, m;
	scanf("%d %d", &sv, &n);
	int p = 0, q = 0;
	for(int i=1; i<=n; i++){
		int x;
		scanf("%d", &x);
		x -= sv;
		if(x == 0){
			puts("1");
			return 0;
		}
		int y = abs(x);
		if(x < 0) a[++p] = y;
		else b[++q] = y;
	}

	if(!p || !q){
		puts("-1");
		return 0;
	}

	sort(a + 1, a + p + 1);
	p = unique(a + 1, a + p + 1) - a - 1;

	sort(b + 1, b + q + 1);
	q = unique(b + 1, b + q + 1) - b - 1;

	n = 0;
	for(int i=1; i<=p; i++){
		f[++n] = a[i];
		ansf[a[i]] = 1;
	}

	for(int i=1; i<1005; i++){
		int N = 0;
		for(int j=1; j<=n; j++){
			for(int k=1; k<=p; k++){
				int x = f[j] + a[k];
				if(!ansf[x]){
					ansf[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		n = N;
		for(int j=1; j<=n; j++) f[j] = tmp[j];
	}

	m = 0;
	for(int i=1; i<=q; i++){
		g[++m] = b[i];
		ansg[b[i]] = 1;
	}

	for(int i=1; i<1005; i++){
		int N = 0;
		for(int j=1; j<=m; j++){
			for(int k=1; k<=q; k++){
				int x = g[j] + b[k];
				if(!ansg[x]){
					ansg[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		m = N;
		for(int j=1; j<=m; j++) g[j] = tmp[j];
	}

	int res = inf;
	for(int i=1; i<NN; i++){
		if(ansf[i] && ansg[i]) res = min(res, ansf[i] + ansg[i]);
	}

	cout << res << endl;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;

#define NN 1010
int a[NN], b[NN];
int f[NN*NN], g[NN*NN];
int tmp[NN*NN];
int ansf[NN*NN], ansg[NN*NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int sv, n, m;
	scanf("%d %d", &sv, &n);
	int p = 0, q = 0;
	for(int i=1; i<=n; i++){
		int x;
		scanf("%d", &x);
		x -= sv;
		if(x == 0){
			puts("1");
			return 0;
		}
		int y = abs(x);
		if(x < 0) a[++p] = y;
		else b[++q] = y;
	}
	if(!p || !q){
		puts("-1");
		return 0;
	}
	sort(a + 1, a + p + 1);
	p = unique(a + 1, a + p + 1) - a - 1;

	sort(b + 1, b + q + 1);
	q = unique(b + 1, b + q + 1) - b - 1;

	n = 0;
	for(int i=1; i<=p; i++){
		f[++n] = a[i];
		ansf[a[i]] = 1;
	}

	for(int i=1; i<NN; i++){
		int N = 0;
		for(int j=1; j<=n; j++){
			for(int k=1; k<=p; k++){
				int x = f[j] + a[k];
				if(!ansf[x]){
					ansf[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		n = N;
		for(int j=1; j<=n; j++) f[j] = tmp[j];
	}

	m = 0;
	for(int i=1; i<=q; i++){
		g[++m] = b[i];
		ansg[b[i]] = 1;
	}

	for(int i=1; i<NN; i++){
		int N = 0;
		for(int j=1; j<=m; j++){
			for(int k=1; k<=q; k++){
				int x = g[j] + b[k];
				if(!ansg[x]){
					ansg[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		n = N;
		for(int j=1; j<=n; j++) f[j] = tmp[j];
	}

	int res = inf;
	for(int i=1; i<NN*NN; i++){
		if(ansf[i] && ansg[i]) res = min(res, ansf[i] + ansg[i]);
	}
	cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;

const int NN = 1010100;

map<int, int> mp;
int a[NN], dp[NN];
queue<pii> q;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int p, n; cin>>p>>n;
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	sort(a+1, a+n+1);
	n = unique(a+1, a+n+1) - a-1;
	for(int i=1; i<=n; i++) a[i] -= p;

	for(int i=1; i<=n; i++) {
		if(a[i]==0) {
			puts("1");
			return 0;
		}
		q.push(pii(a[i], 1));
		mp[a[i]] = 1;
	}

	while(!q.empty()) {
		auto p = q.front(); q.pop();
		int u = p.first, w = p.second;
		for(int i=1; i<=n; i++) {
			int v = u + a[i];
			if(v <= 1000 && v >= -1000) {
				if(!mp[v]) q.push(pii(v, w+1));
				mp[v] = 1;
				if(v==0) {
					cout<<w+1<<endl;
					return 0;
				}
			}
		}
	}

    return 0;
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;

#define NN 1010
int a[NN], b[NN];
int f[NN*NN], g[NN*NN];
int tmp[NN*NN];
int ansf[NN*NN], ansg[NN*NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int sv, n, m;
	scanf("%d %d", &sv, &n);
	int p = 0, q = 0;
	for(int i=1; i<=n; i++){
		int x;
		scanf("%d", &x);
		x -= sv;
		if(x == 0){
			puts("1");
			return 0;
		}
		int y = abs(x);
		if(x < 0) a[++p] = y;
		else b[++q] = y;
	}

	sort(a + 1, a + p + 1);
	p = unique(a + 1, a + p + 1) - a - 1;

	sort(b + 1, b + q + 1);
	q = unique(b + 1, b + q + 1) - b - 1;

	n = 0;
	for(int i=1; i<=p; i++){
		f[++n] = a[i];
		ansf[a[i]] = 1;
	}

	for(int i=1; i<NN; i++){
		int N = 0;
		for(int j=1; j<=n; j++){
			for(int k=1; k<=p; k++){
				int x = f[j] + a[k];
				if(!ansf[x]){
					ansf[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		n = N;
		for(int j=1; j<=n; j++) f[j] = tmp[j];
	}

	m = 0;
	for(int i=1; i<=q; i++){
		g[++m] = b[i];
		ansg[b[i]] = 1;
	}

	for(int i=1; i<NN; i++){
		int N = 0;
		for(int j=1; j<=m; j++){
			for(int k=1; k<=q; k++){
				int x = g[j] + b[k];
				if(!ansg[x]){
					ansg[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		n = N;
		for(int j=1; j<=n; j++) f[j] = tmp[j];
	}

	int res = inf;
	for(int i=1; i<NN*NN; i++){
		if(ansf[i] && ansg[i]) res = min(res, ansf[i] + ansg[i]);
	}
	cout << res << endl;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2001;
int a[N], c[N], n, k;
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin>>n>>k;
	queue<pair<int, int>> q;
	vector<int> v;
	for(int i=0, j; i<k; i++) {
		scanf("%d", &j), j += 1000-n;
		if(!c[j]) c[j] = a[j] = 1, q.push({j, 1}), v.push_back(j);
	}
	for(int j; !q.empty();) {
		int u = q.front().first, d = q.front().second; q.pop();
		if(u == 1000) return cout<<d<<endl, 0;
		for(auto& i:v) if(0<=(j = i+u-1000) && j<N && !a[j]) a[j] = 1, q.push({j, d+1});
	} puts("-1");
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int NN = 2000, N0 = 1010;

bool flag[N0];
int a[N0 * N0];

struct Bitset {unsigned long long b[32];
	int len = 32;

	Bitset() {}
	void set(int i) {b[i >> 6] |= 1ULL << (i & 63);}
	void reset() {int i = len; while(i--) b[i] = 0;}
	bool operator [](int i) {return b[i >> 6] >> (i & 63) & 1;}
	Bitset operator | (const Bitset &a) {Bitset ans; int i = len;
		for(ans.len = len; i--; ans.b[i] = b[i] | a.b[i]);
		return ans;
	}
	Bitset operator << (int k) {Bitset ans; ans.len = len;
		int m = k >> 6, i = 0; k &= 63; while(i < m) ans.b[i++] = 0;
		ans.b[m] = b[0] << k; while(++i < len) {ans.b[i] = b[i - m] << k;
			if(k) ans.b[i] |= b[i - m - 1] >> 64 - k;
		} return ans;
	}
	Bitset operator >> (int k) {Bitset ans; ans.len = len;
		int m = k >> 6, i = len; k &= 63; while(i-- > len - m) ans.b[i] = 0;
		ans.b[len - m - 1] = b[len - 1] >> k; while(i--) {
			ans.b[i] = b[i + m] >> k; if(k) ans.b[i] |= b[i + m + 1] << 64 - k;
		} return ans;
	}
} s[2];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int k, n, f = 0;
	scanf("%d %d", &k, &n);
	for(; n; n--) {
		int x; scanf("%d", &x);
		if(x == k) puts("1"), exit(0);
		flag[x] = 1;
	}
	for(int i = 1001; i--; ) if(flag[i]) {
		s[f].set(N0 + (a[n++] = k - i));
	}

	for(int i = 1, c; i < NN; i++, f = c) {c = f ^ 1;
		if(s[f][N0]) printf("%d", i), exit(0);
		s[c].reset();
		for(int j = n; j--; ) {
			if(a[j] > 0) s[c] = s[c] | s[f] << a[j];
			else s[c] = s[c] | s[f] >> -a[j];
		}
	} puts("-1");
}


#include<bits/stdc++.h>

using namespace std;
const int NN = 2000, N0 = 1010;

bool flag[N0];
int a[N0 * N0];

bitset<2020> s[2];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int k, n, f = 0;
	scanf("%d %d", &k, &n);
	for(; n; n--) {
		int x; scanf("%d", &x);
		if(x == k) puts("1"), exit(0);
		flag[x] = 1;
	}
	for(int i = 1001; i--; ) if(flag[i]) {
		s[f].set(N0 + (a[n++] = k - i));
	}

	for(int i = 1, c; i < NN; i++, f = c) {c = f ^ 1;
		if(s[f][N0]) printf("%d", i), exit(0);
		s[c].reset();
		for(int j = n; j--; ) {
			if(a[j] > 0) s[c] = s[c] | s[f] << a[j];
			else s[c] = s[c] | s[f] >> -a[j];
		}
	} puts("-1");
}


#include <bits/stdc++.h>
#define inf 0x3f3f3f3f

using namespace std;

#define NN 1010
int a[NN], b[NN];
int f[NN*NN], g[NN*NN];
int tmp[NN*NN];
int ansf[NN*NN], ansg[NN*NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int sv, n, m;
	scanf("%d %d", &sv, &n);
	int p = 0, q = 0;
	for(int i=1; i<=n; i++){
		int x;
		scanf("%d", &x);
		x -= sv;
		if(x == 0){
			puts("1");
			return 0;
		}
		int y = abs(x);
		if(x < 0) a[++p] = y;
		else b[++q] = y;
	}

	if(!p || !q){
		puts("-1");
		return 0;
	}

	sort(a + 1, a + p + 1);
	p = unique(a + 1, a + p + 1) - a - 1;

	sort(b + 1, b + q + 1);
	q = unique(b + 1, b + q + 1) - b - 1;

	n = 0;
	for(int i=1; i<=p; i++){
		f[++n] = a[i];
		ansf[a[i]] = 1;
	}

	for(int i=1; i<NN; i++){
		int N = 0;
		for(int j=1; j<=n; j++){
			for(int k=1; k<=p; k++){
				int x = f[j] + a[k];
				if(!ansf[x]){
					ansf[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		n = N;
		for(int j=1; j<=n; j++) f[j] = tmp[j];
	}

	m = 0;
	for(int i=1; i<=q; i++){
		g[++m] = b[i];
		ansg[b[i]] = 1;
	}

	for(int i=1; i<NN; i++){
		int N = 0;
		for(int j=1; j<=m; j++){
			for(int k=1; k<=q; k++){
				int x = g[j] + b[k];
				if(!ansg[x]){
					ansg[x] = i + 1;
					tmp[++N] = x;
				}
			}
		}
		m = N;
		for(int j=1; j<=m; j++) g[j] = tmp[j];
	}

	int res = inf;
	for(int i=1; i<NN*NN; i++){
		if(ansf[i] && ansg[i]) res = min(res, ansf[i] + ansg[i]);
	}

	cout << res << endl;
}


#include<bits/stdc++.h>

using namespace std;

const int NN = 1010, N = 1000;
const int inf = 1e9;
int a[NN * NN], L[NN][NN << 1], R[NN][NN << 1];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int k, n; scanf("%d %d", &k, &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", a + i), a[i] -= k;
		if(!a[i]) puts("1"), exit(0);
	}
	sort(a + 1, a + n + 1);
	n = unique(a + 1, a + n + 1) - a - 1;

	for(int i = -N; i <= N; i++) L[0][NN + i] = inf;
	L[0][NN + 0] = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = -N; j <= N; j++) L[i][NN + j] = inf;
		for(int j = -N, J; j <= N; j++) {
			for(int k = 0; ; k++) {
				J = j - a[i] * k; if(J < -N || N < J) break;
				L[i][NN + j] = min(L[i][NN + j], L[i - 1][NN + J] + k);
			}
		}
	}

	for(int i = -N; i <= N; i++) R[n + 1][NN + i] = inf;
	R[n + 1][NN + 0] = 0;
	for(int i = n; i >= 1; i--) {
		for(int j = -N; j <= N; j++) R[i][NN + j] = inf;
		for(int j = -N, J; j <= N; j++) {
			for(int k = 0; ; k++) {
				J = j - a[i] * k; if(J < -N || N < J) break;
				R[i][NN + j] = min(R[i][NN + j], R[i + 1][NN + J] + k);
			}
		}
	}

	int ans = inf;
	for(int i = 1; i < n; i++) {
		for(int j = -N; j <= N; j++) if(j) {
			ans = min(ans, L[i][NN + j] + R[i + 1][NN - j]);
		}
	}
	printf("%d", ans < inf ? ans : -1);
}


#include<bits/stdc++.h>
#define NN 1010000
#define inf 1e9

using namespace std;

map<int, int> mp;
int a[NN];
queue<int> que;

bool out(int val){
	return val < -1000 || val > 1000;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	ios_base :: sync_with_stdio(0); cin.tie(0);
	int n, k;
	cin >> n >> k;
	for(int i = 1; i <= k; i++) cin >> a[i], a[i] -= n;
	sort(a + 1, a + k);
	k = unique(a + 1, a + k + 1) - a - 1;
	for(int i = -1000; i <= 1000; i++) mp[i] = inf;
	for(int i = 1; i <= k; i++) mp[a[i]] = 1, que.push(a[i]);
	while(!que.empty()){
		int u = que.front();
		que.pop();
		for(int i = 1; i <= k; i++){
			int v = u + a[i];
			if(out(v)) continue;
			if(mp[v] > mp[u] + 1){
				mp[v] = mp[u] + 1;
				que.push(v);
			}
		}
	}
	if(mp[0] == inf) cout << "-1" << endl;
	else cout << mp[0] << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2001;
int a[N], c[N], n, k;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	cin>>n>>k;
	queue<pair<int, int>> q;
	for(int i=0, j; i<k; i++) {
		scanf("%d", &j), j += 1000-n;
		if(!c[j]) c[j] = a[j] = 1, q.push({j, 1});
	}
	while(!q.empty()) {
		int u = q.front().first, d = q.front().second; q.pop();
		if(u == 1000) return cout<<d<<endl, 0;
		for(int i=0, j; i<N; i++) if(c[i]) if(0<=(j = i+u-1000) && j<N && !a[j]) a[j] = 1, q.push({j, d+1});
	} puts("-1");
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
const int NN = 2000, N0 = 1010;

bool flag[N0];
int a[N0 * N0];

struct Bitset {unsigned long long b[32];
	int len = 32;

	Bitset() {}
	void set(int i) {b[i >> 6] |= 1ULL << (i & 63);}
	void reset() {int i = len; while(i--) b[i] = 0;}
	bool operator [](int i) {return b[i >> 6] >> (i & 63) & 1;}
	Bitset operator | (Bitset a) {Bitset ans; int i = len;
		for(ans.len = len; i--; ans.b[i] = b[i] | a.b[i]);
		return ans;
	}
	Bitset operator << (int k) {Bitset ans; ans.len = len;
		int m = k >> 6, i = 0; k &= 63; while(i < m) ans.b[i++] = 0;
		ans.b[m] = b[0] << k; while(++i < len) {ans.b[i] = b[i - m] << k;
			if(k) ans.b[i] |= b[i - m - 1] >> 64 - k;
		} return ans;
	}
	Bitset operator >> (int k) {Bitset ans; ans.len = len;
		int m = k >> 6, i = len; k &= 63; while(i-- > len - m) ans.b[i] = 0;
		ans.b[len - m - 1] = b[len - 1] >> k; while(i--) {
			ans.b[i] = b[i + m] >> k; if(k) ans.b[i] |= b[i + m + 1] << 64 - k;
		} return ans;
	}
} s[2];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int k, n, f = 0;
	scanf("%d %d", &k, &n);
	for(; n; n--) {
		int x; scanf("%d", &x);
		if(x == k) puts("1"), exit(0);
		flag[x] = 1;
	}
	for(int i = 1001; i--; ) if(flag[i]) {
		s[f].set(N0 + (a[n++] = k - i));
	}

	for(int i = 1, c; i < NN; i++, f = c) {c = f ^ 1;
		if(s[f][N0]) printf("%d", i), exit(0);
		s[c].reset();
		for(int j = n; j--; ) {
			if(a[j] > 0) s[c] = s[c] | s[f] << a[j];
			else s[c] = s[c] | s[f] >> -a[j];
		}
	} puts("-1");
}


#include<bits/stdc++.h>
using namespace std;
inline void in(int&x){char c;while(c=getchar(),c<=' ');bool sign(c=='-');if(sign)c=getchar();x=(c-'0');while(c=getchar(),!(c<'0'|c>'9'))x=10*x+c-'0';if(sign)x=-x;}
const int N = 2001;
bitset<N> a, b, c;
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n, k; in(n), in(k);
	for(int i=0, j; i<k; i++) in(j), c[j+1000-n] = 1;
	a.set(N>>1);
	for(int f=1; ; f++) {
		b = a; a.reset();
		for(int i=0; i<N; i++) if(c[i]) a |= (i>=1000) ? (b<<i-1000) : (b>>1000-i);
		if(a[N>>1]) {
			cout<<f<<endl;
			return 0;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2001;
bitset<N> a, b, c;
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n, k; cin>>n>>k;
	for(int i=0, j; i<k; i++) scanf("%d", &j), c[j+1000-n] = 1;
	a.set(N>>1);
	for(int f=1; ; f++) {
		b = a; a.reset();
		for(int i=0; i<N; i++) if(c[i]) a |= (i>=1000) ? (b<<i-1000) : (b>>1000-i);
		if(a[N>>1]) {
			cout<<f<<endl;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;

const int NN = 1010100;

map<int, int> mp;
int a[NN], dp[NN];
queue<pii> q;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int p, n; cin>>p>>n;
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	sort(a+1, a+n+1);
	n = unique(a+1, a+n+1) - a-1;
	for(int i=1; i<=n; i++) a[i] -= p;

	for(int i=1; i<=n; i++) {
		if(a[i]==0) {
			puts("1");
			return 0;
		}
		q.push(pii(a[i], 1));
		mp[a[i]] = 1;
	}

	while(!q.empty()) {
		auto p = q.front(); q.pop();
		int u = p.first, w = p.second;
		for(int i=1; i<=n; i++) {
			int v = u + a[i];
			if(v <= 1000 && v >= -1000) {
				if(!mp[v]) q.push(pii(v, w+1));
				mp[v] = 1;
				if(v==0) {
					cout<<w+1<<endl;
					return 0;
				}
			}
		}
	}
	puts("-1");
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;

map < long ,vector < long > > ad;
map < long , long > visi,cnt;

long bfs()
{
    long n,i;
    list < long > q;
    q.pb(0);
    while(!q.empty())
    {
        n=q.front();
        q.pop_front();
        for(i=0;i<ad[n].size();++i)
        {
            if(visi[ad[n][i]] && ad[n][i]==0) return cnt[n]+1;
            else if(!visi[ad[n][i]])
            {
                visi[ad[n][i]]=1;
                q.pb(ad[n][i]);
                cnt[ad[n][i]]=cnt[n]+1;
            }
        }
    }
    return -1;
}
int main()
{
    long n,k,a[1003]={0},i,x,j;
    vector < long > v;
    cin>>n>>k;
    for(i=0;i<k;++i)
    {
        cin>>x;
        if(a[x]==0)
        {
            a[x]=1;
            v.pb(x-n);
        }
    }
    for(i=-1000;i<1001;++i)
    {
        for(j=0;j<v.size();++j)
            ad[i].pb(i+v[j]);
    }
    cout<<(bfs());
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;

map < long ,vector < long > > ad;
map < long , long > visi,cnt;

long bfs()
{
    long n,i;
    list < long > q;
    q.pb(0);
    while(!q.empty())
    {
        n=q.front();
        q.pop_front();
        visi[n]=1;
        for(i=0;i<ad[n].size();++i)
        {
            if(visi[ad[n][i]] && ad[n][i]==0) return cnt[n]+1;
            q.pb(ad[n][i]);
            cnt[ad[n][i]]=cnt[n]+1;
        }
    }
    return -1;
}
int main()
{
    long n,k,a[1003]={0},i,x,j;
    vector < long > v;
    cin>>n>>k;
    for(i=0;i<k;++i)
    {
        cin>>x;
        if(a[x]==0)
        {
            a[x]=1;
            v.pb(x-n);
        }
    }
    for(i=-1000;i<1001;++i)
    {
        for(j=0;j<v.size();++j)
            ad[i].pb(i+v[j]);
    }
    cout<<(bfs());
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;

map < long ,vector < long > > ad;
map < long , long > visi,cnt;

long bfs()
{
    long n,i;
    list < long > q;
    q.pb(0);
    while(!q.empty())
    {
        n=q.front();
        q.pop_front();
        for(i=0;i<ad[n].size();++i)
        {
            if(visi[ad[n][i]] && ad[n][i]==0) return cnt[n]+1;
            else if(!visi[ad[n][i]])
            {
                visi[ad[n][i]]=1;
                q.pb(ad[n][i]);
                cnt[ad[n][i]]=cnt[n]+1;
            }
        }
    }
    return -1;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long n,k,a[1003]={0},i,x,j;
    vector < long > v;
    cin>>n>>k;
    for(i=0;i<k;++i)
    {
        cin>>x;
        if(a[x]==0)
        {
            a[x]=1;
            v.pb(x-n);
        }
    }
    for(i=-1000;i<1001;++i)
    {
        for(j=0;j<v.size();++j)
            ad[i].pb(i+v[j]);
    }
    cout<<(bfs());
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back

using namespace std;

map < long ,vector < long > > ad;
map < long , long > visi,cnt;

long bfs()
{
    long n,i;
    list < long > q;
    q.pb(0);
    visi[0]=1;
    while(!q.empty())
    {
        n=q.front();
        q.pop_front();
        for(i=0;i<ad[n].size();++i)
        {
            if(visi[ad[n][i]] && ad[n][i]==0) return cnt[n]+1;
            else if(!visi[ad[n][i]])
            {
                visi[ad[n][i]]=1;
                q.pb(ad[n][i]);
                cnt[ad[n][i]]=cnt[n]+1;
            }
        }
    }
    return -1;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long n,k,a[1003]={0},i,x,j;
    vector < long > v;
    cin>>n>>k;
    for(i=0;i<k;++i)
    {
        cin>>x;
        if(a[x]==0)
        {
            a[x]=1;
            v.pb(x-n);
        }
    }
    for(i=-1000;i<1001;++i)
    {
        for(j=0;j<v.size();++j)
            ad[i].pb(i+v[j]);
    }
    cout<<(bfs());
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

constexpr int inf = 0x3f3f3f3f;

bool vis[1001];
int dist[2011];

const int N = 1000;

int main(void) {

    int n, k;
    cin >> n >> k;
    memset(vis, false, sizeof(vis));
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    vector<int> a;
    for (int i = 0; i < k; ++ i) {
        int x;
        cin >> x;
        vis[x] = true;
        q.push(x - n + N);
        dist[x - n + N] = 1;
    }
    for (int i = 0; i <= 1000; ++ i) if (vis[i]) a.push_back(i);

    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i: a) {
            int nu = u + i - n;
            if (nu >= 0 && nu <= 2 * N && dist[nu] > dist[u] + 1) {
                dist[nu] = dist[u] + 1;
                q.push(nu);
            }
        }
    }
    if (dist[N] >= inf)  cout << -1 << endl;
    else cout << dist[N] << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

constexpr int inf = 0x3f3f3f3f;

bool vis[1001];
int dist[3011];

const int N = 1010;

int main(void) {
    ios::sync_with_stdio(false);

    int n, k;
    cin >> n >> k;
    memset(vis, false, sizeof(vis));
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    vector<int> a;
    for (int i = 0; i < k; ++ i) {
        int x;
        cin >> x;
        vis[x] = true;
    }
    for (int i = 0; i <= 1000; ++ i) if (vis[i]) {
        q.push(i - n + N);
        dist[i - n + N] = 1;
        a.push_back(i);
    }

    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i: a) {
            int nu = u + i - n;
            if (nu >= 0 && nu <= 2 * N && dist[nu] > dist[u] + 1) {
                dist[nu] = dist[u] + 1;
                q.push(nu);
            }
        }
    }
    if (dist[N] >= inf)  cout << -1 << endl;
    else cout << dist[N] << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

constexpr int inf = 0x3f3f3f3f;

bool vis[1001];
int dist[2011];

const int N = 1000;

int main(void) {

    int n, k;
    cin >> n >> k;
    memset(vis, false, sizeof(vis));
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    vector<int> a;
    for (int i = 0; i < k; ++ i) {
        int x;
        cin >> x;
        vis[x] = true;
    }
    for (int i = 0; i <= 1000; ++ i) if (vis[i]) {
        q.push(i - n + N);
        dist[i - n + N] = 1;
        a.push_back(i);
    }

    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i: a) {
            int nu = u + i - n;
            if (nu >= 0 && nu <= 2 * N && dist[nu] > dist[u] + 1) {
                dist[nu] = dist[u] + 1;
                q.push(nu);
            }
        }
    }
    if (dist[N] >= inf)  cout << -1 << endl;
    else cout << dist[N] << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

constexpr int inf = 0x3f3f3f3f;

bool vis[1001];
int dist[2011];

const int N = 1000;

int main(void) {

    int n, k;
    cin >> n >> k;
    memset(vis, false, sizeof(vis));
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    for (int i = 0; i < k; ++ i) {
        int x;
        cin >> x;
        vis[x] = true;
        q.push(x - n + N);
        dist[x - n + N] = 1;
    }

    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i = 0; i <= 1000; ++ i) if (vis[i]) {
            int nu = u + i - n;
            if (nu >= 0 && nu <= 2 * N && dist[nu] > dist[u] + 1) {
                dist[nu] = dist[u] + 1;
                q.push(nu);
            }
        }
    }
    if (dist[N] >= inf)  cout << -1 << endl;
    else cout << dist[N] << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

constexpr int inf = 0x3f3f3f3f;

bool vis[1001];
int dist[3011];

const int N = 1010;

int main(void) {

    int n, k;
    cin >> n >> k;
    memset(vis, false, sizeof(vis));
    memset(dist, 0x3f, sizeof(dist));
    queue<int> q;
    vector<int> a;
    for (int i = 0; i < k; ++ i) {
        int x;
        cin >> x;
        vis[x] = true;
    }
    for (int i = 0; i <= 1000; ++ i) if (vis[i]) {
        q.push(i - n + N);
        dist[i - n + N] = 1;
        a.push_back(i);
    }

    while (!q.empty()) {
        int u = q.front(); q.pop();
        for (int i: a) {
            int nu = u + i - n;
            if (nu >= 0 && nu <= 2 * N && dist[nu] > dist[u] + 1) {
                dist[nu] = dist[u] + 1;
                q.push(nu);
            }
        }
    }
    if (dist[N] >= inf)  cout << -1 << endl;
    else cout << dist[N] << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1000006];
map<int,int>mp;
int main()
{
    int n,k,x,cnt=0;
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[cnt++]=x-n;
    }
    cnt=unique(a,a+cnt)-a;
    q.push(0);
    while(!q.empty()){
        x=q.front();q.pop();
        for(int i=0;i<cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0&&x!=0){
                return 0*printf("%d\n",mp[x]+1);
            }
            if(!mp[y]&&y!=0){
                mp[y]=mp[x]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006],vis[1000006];
int main()
{
    int n,k,x,cnt=0,minn=inf,maxn=0;
    memset(vis,0,sizeof(vis));
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        minn=min(minn,x);maxn=max(maxn,x);
        a[++cnt]=x-n;
        q.push(x-n);
        vis[x-n+1001]=1;
    }
    if(vis[1001]) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("-1\n");
    while(true){
        x=q.front();q.pop();
        for(int i=1;i<=cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0){
                return 0*printf("%d\n",vis[x+1001]+1);
            }
            if(!vis[y+1001]){
                vis[y+1001]=vis[x+1001]+1;
                q.push(y);
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1000006];
map<int,int>mp;
int main()
{
    int n,k,x,cnt=0;
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[cnt++]=x-n;
    }
    sort(a,a+cnt);
    cnt=unique(a,a+cnt)-a;
    q.push(0);
    while(!q.empty()){
        x=q.front();q.pop();
        for(int i=0;i<cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0){
                return 0*printf("%d\n",mp[x]+1);
            }
            if(!mp[y]){
                mp[y]=mp[x]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006];
map<int,int>mp;
int main()
{
    int n,k,x,cnt=0,minn=inf,maxn=0;
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        minn=min(minn,x);maxn=max(maxn,x);
        a[++cnt]=x-n;
        q.push(x-n);
        mp[x-n]=1;
    }
    if(mp[0]) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("-1\n");
    while(true){
        x=q.front();q.pop();
        for(int i=1;i<=cnt;i++){
            int y=x+a[i];
            if(y<-1001) continue;
            if(y==0){
                return 0*printf("%d\n",mp[x]+1);
            }
            if(!mp[y]){
                mp[y]=mp[x]+1;
                q.push(y);
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006],vis[1000006];
int main()
{
    int n,k,x,cnt=0;
    memset(vis,0,sizeof(vis));
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[++cnt]=x-n;
        q.push(x-n);
        vis[x-n+1001]=1;
    }
    if(vis[1001]) return 0*printf("1\n");
    while(true){
        x=q.front();q.pop();
        for(int i=1;i<=cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0){
                return 0*printf("%d\n",vis[x+1001]+1);
            }
            if(!vis[y+1001]){
                vis[y+1001]=vis[x+1001]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006],vis[1000006];
int main()
{
    int n,k,x,cnt=0;
    memset(vis,0,sizeof(vis));
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[cnt++]=x-n;
        q.push(x-n);
        vis[x-n+1001]=1;
    }
    cnt=unique(a,a+cnt)-a;
    if(vis[1001]) return 0*printf("1\n");
    while(!q.empty()){
        x=q.front();q.pop();
        for(int i=0;i<cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0){
                return 0*printf("%d\n",vis[x+1001]+1);
            }
            if(!vis[y+1001]){
                vis[y+1001]=vis[x+1001]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int q[1000006];
map<int ,int>mp;
int main()
{
    int n,k,x,lef=1,rig=0,flag=0,minn=inf,maxn=0;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        if(x==n) flag=1;
        minn=min(minn,x);maxn=max(maxn,x);
        q[++rig]=x-n;
        mp[x-n]=1;
    }
    if(flag) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("-1\n");
    while(true){
        x=q[lef];
        int tmp=rig;
        for(int i=lef;i<=tmp;i++){
            int y=x+q[i];
            if(y==0){
                return 0*printf("%d\n",mp[x]+mp[q[i]]);
            }
            if(!mp[y]){
                mp[y]=mp[x]+mp[q[i]];
                q[++rig]=y;
            }
        }
        lef++;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1000006];
map<int,int>mp;
int main()
{
    int n,k,x,cnt=0;
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[cnt++]=x-n;
    }
    cnt=unique(a,a+cnt)-a;
    q.push(0);
    while(!q.empty()){
        x=q.front();q.pop();
        for(int i=0;i<cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0&&a[i]!=0){
                return 0*printf("%d\n",mp[x]+1);
            }
            if(!mp[y]&&y!=0){
                mp[y]=mp[x]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int q[1000006];
map<int ,int>mp;
int main()
{
    int n,k,x,lef=1,rig=0,flag=0,minn=inf,maxn=0;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        if(x==n) flag=1;
        minn=min(minn,x);maxn=max(maxn,x);
        q[++rig]=x-n;
        mp[x-n]=1;
    }
    if(flag) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("0\n");
    while(true){
        x=q[lef];
        int tmp=rig;
        for(int i=lef;i<=tmp;i++){
            int y=x+q[i];
            if(y==0){
                return 0*printf("%d\n",mp[x]+mp[q[i]]);
            }
            if(!mp[y]){
                mp[y]=mp[x]+mp[q[i]];
                q[++rig]=y;
            }
        }
        lef++;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006],vis[1000006];
int main()
{
    int n,k,x,cnt=0,minn=inf,maxn=0;
    memset(vis,0,sizeof(vis));
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        minn=min(minn,x);maxn=max(maxn,x);
        a[++cnt]=x-n;
        q.push(x-n);
        vis[x-n+1001]=1;
    }
    if(vis[1001]) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("-1\n");
    while(true){
        x=q.front();q.pop();
        for(int i=1;i<=cnt;i++){
            int y=x+a[i];
            if(abs(y>1001)) continue;
            if(y==0){
                return 0*printf("%d\n",vis[x+1001]+1);
            }
            if(!vis[y+1001]){
                vis[y+1001]=vis[x+1001]+1;
                q.push(y);
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int q[1000006],vis[1000006];
int main()
{
    int n,k,x,lef=1,rig=0,flag=0,minn=inf,maxn=0;
    memset(vis,0,sizeof(vis));
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        if(x==n) flag=1;
        minn=min(minn,x);maxn=max(maxn,x);
        q[++rig]=x-n;
        vis[x-n+1000]=1;
    }
    if(flag) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("0\n");
    while(true){
        x=q[lef];
        int tmp=rig;
        for(int i=lef;i<=tmp;i++){
            int y=x+q[i];
            if(y==0){
                return 0*printf("%d\n",vis[x+1000]+vis[q[i]+1000]);
            }
            if(!vis[y+1000]){
                vis[y+1000]=vis[x+1000]+vis[q[i]+1000];
                q[++rig]=y;
            }
        }
        lef++;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006];
map<int,int>mp;
int main()
{
    int n,k,x,cnt=0,minn=inf,maxn=0;
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        minn=min(minn,x);maxn=max(maxn,x);
        a[++cnt]=x-n;
        q.push(x-n);
        mp[x-n]=1;
    }
    if(mp[0]) return 0*printf("1\n");
    else if(n<minn||n>maxn) return 0*printf("-1\n");
    while(true){
        x=q.front();q.pop();
        for(int i=1;i<=cnt;i++){
            int y=x+a[i];
            if(abs(y>1001)) continue;
            if(y==0){
                return 0*printf("%d\n",mp[x]+1);
            }
            if(!mp[y]){
                mp[y]=mp[x]+1;
                q.push(y);
            }
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x7fffffff;
int a[1000006],vis[1000006];
int main()
{
    int n,k,x,cnt=0;
    memset(vis,0,sizeof(vis));
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[++cnt]=x-n;
        q.push(x-n);
        vis[x-n+1001]=1;
    }
    if(vis[1001]) return 0*printf("1\n");
    while(!q.empty()){
        x=q.front();q.pop();
        for(int i=1;i<=cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0){
                return 0*printf("%d\n",vis[x+1001]+1);
            }
            if(!vis[y+1001]){
                vis[y+1001]=vis[x+1001]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1000006];
map<int,int>mp;
int main()
{
    int n,k,x,cnt=0;
    queue<int>q;
    scanf("%d%d",&n,&k);
    for(int i=0;i<k;i++){
        scanf("%d",&x);
        a[cnt++]=x-n;
    }
    cnt=unique(a,a+cnt)-a;
    q.push(0);
    while(!q.empty()){
        x=q.front();q.pop();
        for(int i=0;i<cnt;i++){
            int y=x+a[i];
            if(abs(y)>1001) continue;
            if(y==0){
                return 0*printf("%d\n",mp[x]+1);
            }
            if(!mp[y]){
                mp[y]=mp[x]+1;
                q.push(y);
            }
        }
    }
    return 0*printf("-1\n");
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1001, limit = 2003, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    map<int, int> mp;
    vector<int> q(k), A(k);
    for (int i = 0, a; i < k; ++i) {
        cin >> a;
        q[i] = middle + (A[i] = a - n);
        dist[q[i]] = 1;
    }
    while (head < q.size()) {
        int u = q[head++];
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<int> q(k), A(k);
    for (int& a : A) cin >> a;
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    for (int i = 0; i < A.size(); ++i) {
        q[i] = middle + (A[i] -= n);
        dist[q[i]] = 1;
    }
    while (head < q.size()) {
        int u = q[head++];
//        cout << u << ": " << head << " " << q.size() << endl;
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<int> q(k), A(k);
    for (int& a : A) cin >> a;
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    for (int i = 0; i < A.size(); ++i) {
        q[i] = middle + (A[i] -= n);
        dist[q[i]] = 1;
    }
    while (head < q.size() && head < limit) {
        int u = q[head++];
//        cout << u << ": " << head << " " << q.size() << endl;
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<int> A(k);
    for (int& a : A) cin >> a;
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    vector<int> q(A.size());
    for (int i = 0; i < A.size(); ++i) {
        q[i] = middle + (A[i] -= n);
        dist[q[i]] = 1;
    }
    while (head < q.size()) {
        int u = q[head++];
//        cout << u << ": " << head << " " << q.size() << endl;
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<int> q(k), A(k);
    for (int i = 0, a; i < k; ++i) {
        cin >> a;
        q[i] = middle + (A[i] = a - n);
        dist[q[i]] = 1;
    }
    while (head < q.size()) {
        int u = q[head++];
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<int> q(k), A(k);
    for (int& a : A) cin >> a;
    sort(A.begin(), A.end());
    for (int i = 0; i < k; ++i) {
        q[i] = middle + (A[i] -= n);
        dist[q[i]] = 1;
    }
    while (head < q.size() && head < limit) {
        int u = q[head++];
//        cout << u << ": " << head << " " << q.size() << endl;
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<bool> is_a(middle, false);
    for (int i = 0, a; i < k; ++i) {
        cin >> a;
        is_a[a] = true;
    }
    vector<int> A, q;
    for (int i = 0; i < middle; ++i) {
        if (is_a[i]) {
            A.push_back(i - n);
            q.push_back(middle + A.back());
            dist[q.back()] = 1;
        }
    }
    while (head < q.size()) {
        int u = q[head++];
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(int argc, char** argv) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int middle = 1002, limit = 2007, dist[limit];
    fill(dist, dist + limit, 1e9);
    int head = 0, n, k;
    cin >> n >> k;
    vector<int> q(k), A(k);
    for (int& a : A) cin >> a;
    sort(A.begin(), A.end());
    for (int i = 0; i < k; ++i) {
        q[i] = middle + (A[i] -= n);
        dist[q[i]] = 1;
    }
    while (head < q.size()) {
        int u = q[head++];
        for (int a : A) {
            int v = u + a;
            if (v < 0 || v >= limit || dist[v] <= dist[u] + 1) continue;
            dist[v] = dist[u] + 1;
            q.push_back(v);
        }
    }
    cout << (dist[middle] < 1e9? dist[middle] : -1) << endl;
    return 0;
}


#include <cstdio>
#include <bitset>
#include <algorithm>

constexpr int MAX_N = 1000100;
constexpr int MAX = 1111111;

constexpr int INF = 987654321;

using namespace std;

int k, n;
int arr[MAX_N];

int dp[MAX];

int main() {
    scanf(" %d %d", &k, &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &arr[i]);
    }

    sort(arr, arr + n);
    n = unique(arr, arr + n) - arr;

    if (arr[n - 1] < k || arr[0] > k) {
        printf("-1\n");
    } else {
        dp[0] = 0;
        for (int i = 1; i < MAX; i++) {
            dp[i] = INF;
            for (int j = 0; j < n; j++) {
                if (arr[j] > i || dp[i - arr[j]] == INF) continue;
                dp[i] = min(dp[i], dp[i - arr[j]] + 1);
            }
            if (dp[i] * k == i) {
                printf("%d\n", dp[i]);
                break;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <bitset>
#include <algorithm>

constexpr int MAX_N = 1000100;
constexpr int MAX = 2111111;

constexpr int INF = 987654321;

using namespace std;

int k, n;
int arr[MAX_N];

int dp[MAX];

int main() {
    scanf(" %d %d", &k, &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &arr[i]);
    }

    sort(arr, arr + n);
    n = unique(arr, arr + n) - arr;

    if (arr[n - 1] < k || arr[0] > k) {
        printf("-1\n");
    } else {
        dp[0] = 0;
        for (int i = 1; i < MAX; i++) {
            dp[i] = INF;
            for (int j = 0; j < n; j++) {
                if (arr[j] > i || dp[i - arr[j]] == INF) continue;
                dp[i] = min(dp[i], dp[i - arr[j]] + 1);
            }
            if (dp[i] != INF && dp[i] * k == i) {
                printf("%d\n", dp[i]);
                break;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <algorithm>
#include <queue>
#include <unordered_set>

constexpr int MAX_N = 1000100;
constexpr int MAX = 2111111;

constexpr int INF = 987654321;

using namespace std;

int k, n;
int arr[MAX_N];

int main() {
    scanf(" %d %d", &k, &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &arr[i]);
    }

    sort(arr, arr + n);
    n = unique(arr, arr + n) - arr;

    if (arr[0] > k || arr[n - 1] < k) {
        printf("-1\n");
        return 0;
    }

    for (int i = 0; i < n; i++) {
        arr[i] -= k;
    }

    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));

    unordered_set<int> vis;

    while (!q.empty()) {
        pair<int, int> cur = q.front(); q.pop();
        if (vis.find(cur.first) != vis.end()) {
            if (cur.first == 0) {
                printf("%d\n", cur.second);
                break;
            } else {
                continue;
            }
        }
        vis.insert(cur.first);
        for (int i = 0; i < n; i++) {
            q.push(make_pair(cur.first + arr[i], cur.second + 1));
        }
    }

    return 0;
}


#include <cstdio>
#include <bitset>
#include <algorithm>

constexpr int MAX_N = 1000100;
constexpr int MAX = 2111111;

constexpr int INF = 987654321;

using namespace std;

int k, n;
int arr[MAX_N];

int dp[MAX];

int main() {
    scanf(" %d %d", &k, &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &arr[i]);
    }

    sort(arr, arr + n);
    n = unique(arr, arr + n) - arr;

    if (arr[0] == 0 && k == 0) {
        printf("1\n");
    } else if (arr[n - 1] < k || arr[0] > k) {
        printf("-1\n");
    } else {
        dp[0] = 0;
        for (int i = 1; i < MAX; i++) {
            dp[i] = INF;
            for (int j = 0; j < n; j++) {
                if (arr[j] == 0 || arr[j] > i || dp[i - arr[j]] == INF)
                    continue;
                dp[i] = min(dp[i], dp[i - arr[j]] + 1);
            }
            if (dp[i] != INF && dp[i] * k == i) {
                printf("%d\n", dp[i]);
                break;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <bitset>
#include <algorithm>

constexpr int MAX_N = 1000100;
constexpr int MAX = 2111111;

constexpr int INF = 987654321;

using namespace std;

int k, n;
int arr[MAX_N];

int dp[MAX];

int main() {
    scanf(" %d %d", &k, &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &arr[i]);
    }

    sort(arr, arr + n);
    n = unique(arr, arr + n) - arr;

    if (arr[0] == 0 && k == 0) {
        printf("1\n");
    } else if (arr[n - 1] < k || arr[0] > k) {
        printf("-1\n");
    } else {
        dp[0] = 0;
        for (int i = 1; i < MAX; i++) {
            dp[i] = INF;
            for (int j = 0; j < n; j++) {
                if (arr[j] > i || dp[i - arr[j]] == INF) continue;
                dp[i] = min(dp[i], dp[i - arr[j]] + 1);
            }
            if (dp[i] != INF && dp[i] * k == i) {
                printf("%d\n", dp[i]);
                break;
            }
        }
    }

    return 0;
}


#include <cstdio>
#include <bitset>
#include <algorithm>
#include <queue>

constexpr int MAX_N = 1000100;
constexpr int MAX = 2111111;

constexpr int INF = 987654321;

using namespace std;

int k, n;
int arr[MAX_N];

bool vis[MAX_N];

int main() {
    scanf(" %d %d", &k, &n);
    for (int i = 0; i < n; i++) {
        scanf(" %d", &arr[i]);
    }

    sort(arr, arr + n);
    n = unique(arr, arr + n) - arr;

    if (arr[0] > k || arr[n - 1] < k) {
        printf("-1\n");
        return 0;
    }

    for (int i = 0; i < n; i++) {
        arr[i] -= k;
    }

    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));

    while (!q.empty()) {
        pair<int, int> cur = q.front(); q.pop();
        if (vis[cur.first]) {
            if (cur.first == 0) {
                printf("%d\n", cur.second);
                break;
            } else {
                continue;
            }
        }
        vis[cur.first] = true;
        for (int i = 0; i < n; i++) {
            q.push(make_pair(cur.first + arr[i], cur.second + 1));
        }
    }

    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const int N=1e3+3;
int mark[N];
int main()
{
    int n,k;
    cin>>n>>k;int mn=1000000,mx=-1;int m=1e5;int x;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        mn=min(mn,x);
        mx=max(mx,x);
        mark[x]=1;
    }
    if(mark[n]==1)
    {
        cout<<"1"<<endl;
        return 0;
    }
    if(mn>n||mx<n)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int dp1[m],dp2[m];
    for(int i=0;i<m;i++)
    {
        dp1[i]=m;dp2[i]=m;
    }
    dp1[0]=0;dp2[0]=0;int v;
    for(int i=0;i<n;i++)
    {
        if(mark[i])
        {
            v=n-i;
            for(int j=v;j<m;j++)
                if(dp1[j-v]+1<dp1[j])
                    dp1[j]=dp1[j-v]+1;
        }
    }
    for(int i=n+1;i<N;i++)
    {
        if(mark[i])
        {
            v=i-n;
            for(int j=v;j<m;j++)
                if(dp2[j-v]+1<dp2[j])
                    dp2[j]=1+dp2[j-v];
        }
    }
    int ans=m;
    for(int i=1;i<m;i++)
        ans=min(ans,dp1[i]+dp2[i]);
    if(ans==m)
        cout<<"-1"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
vector<int> adj[10000];
set<int>visited;
set<int>::iterator it;
int Time=0;
map<int,int>currentstack;
map<int,int>::iterator it1;
map<int,int>tin;
int ans=1000000000;
void dfs(int u)
{
    visited.insert(u);
    Time++;
    currentstack.insert(m1(Time,u));
    tin.insert(m1(u,Time));int v;
    for(int i=0;i<adj[u].size();i++)
    {
        v=adj[u][i];
        it=visited.find(v);
        if(it==visited.end())
        {
            dfs(v);
        }
        else
        {
            it1=currentstack.find(tin[v]);
            if(it1!=currentstack.end())
            {
                it1=currentstack.lower_bound(tin[v]);int cnt=0;
                while(it1!=currentstack.end())
                {
                    cnt++;
                    it1++;
                }
                ans=min(ans,cnt);
            }
        }
    }
    currentstack.erase(currentstack.find(tin[u]));
}
int main()
{
    int n,k;
    cin>>n>>k;
    int c[1001]={0};
    int x;int v;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        c[x]++;
        if(x==n)
            ans=1;
    }
    if(ans==1)
    {
        cout<<1<<endl;
        return 0;
    }
   // cout<<1<<endl;
    //mp[-3000].pb(1);
    //cout<<mp[-3000][0]<<endl;
    int mx=-1;
    for(int i=-2000;i<=2000;i++)
     {
        //cout<<1<<endl;
        for(int j=0;j<=1000;j++)
        {
            if(c[j])
            {
                x=j-n;
                v=i+x+2001;
                mx=max(mx,v);
                if(v<=4001&&v>=0)
                {
                    //cout<<1<<endl;
                    adj[i+2001].pb(v);
                }
            }
        }
    }
    //for(int i=0;i<adj[2001].size();i++)
    //cout<<adj[2001][i]<<endl;
   // cout<<mx<<endl;
   // cout<<"ok"<<endl;
  //  for(int i=0;i<=4001;i++)
   // {

        //while(!visited.empty())
         //   visited.erase(visited.begin());
        dfs(2001);

    //}
    if(ans==1000000000)
        cout<<-1<<endl;
    else
    cout<<ans<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const int N=1e3+3;
int mark[N];
int main()
{
    int n,k;
    cin>>n>>k;int mn=1000000,mx=-1;int m=1e4;int x;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        mn=min(mn,x);
        mx=max(mx,x);
        mark[x]=1;
    }
    if(mark[n]==1)
    {
        cout<<"1"<<endl;
        return 0;
    }
    if(mn>n||mx<n)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int dp1[m],dp2[m];
    for(int i=0;i<m;i++)
    {
        dp1[i]=m;dp2[i]=m;
    }
    dp1[0]=0;dp2[0]=0;int v;
    for(int i=0;i<n;i++)
    {
        if(mark[i])
        {
            v=n-i;
            for(int j=v;j<m;j++)
                if(dp1[j-v]+1<dp1[j])
                    dp1[j]=dp1[j-v]+1;
        }
    }
    for(int i=n+1;i<N;i++)
    {
        if(mark[i])
        {
            v=i-n;
            for(int j=v;j<m;j++)
                if(dp2[j-v]+1<dp2[j])
                    dp2[j]=1+dp2[j-v];
        }
    }
    int ans=m;
    for(int i=1;i<m;i++)
        ans=min(ans,dp1[i]+dp2[i]);
    if(ans==m)
        cout<<"-1"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const int N=1e3+3;
int mark[N];
int main()
{
    int n,k;IOS;
    cin>>n>>k;int mn=1000000,mx=-1;int m=1e6;int x;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        mn=min(mn,x);
        mx=max(mx,x);
        mark[x]=1;
    }
    if(mark[n]==1)
    {
        cout<<"1"<<endl;
        return 0;
    }
    if(mn>n||mx<n)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int dp1[m],dp2[m];
    for(int i=0;i<m;i++)
    {
        dp1[i]=m;dp2[i]=m;
    }
    dp1[0]=0;dp2[0]=0;int v;
    for(int i=0;i<n;i++)
    {
        if(mark[i])
        {
            v=n-i;
            for(int j=v;j<m;j++)
                if(dp1[j-v]+1<dp1[j])
                    dp1[j]=dp1[j-v]+1;
        }
    }
    for(int i=n+1;i<N;i++)
    {
        if(mark[i])
        {
            v=i-n;
            for(int j=v;j<m;j++)
                if(dp2[j-v]+1<dp2[j])
                    dp2[j]=1+dp2[j-v];
        }
    }
    int ans=m;
    for(int i=1;i<m;i++)
        ans=min(ans,dp1[i]+dp2[i]);
    if(ans==m)
        cout<<"-1"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
vector<int> adj[10000];
set<int>visited;
set<int>::iterator it;
int Time=0;
map<int,int>currentstack;
map<int,int>::iterator it1;
map<int,int>tin;
int ans=1000000000;
void dfs(int u)
{
    visited.insert(u);
    Time++;
    currentstack.insert(m1(Time,u));
    tin.insert(m1(u,Time));int v;
    for(int i=0;i<adj[u].size();i++)
    {
        v=adj[u][i];
        it=visited.find(v);
        if(it==visited.end())
        {
            dfs(v);
        }
        else
        {
            it1=currentstack.find(tin[v]);
            if(it1!=currentstack.end())
            {
                it1=currentstack.lower_bound(tin[v]);int cnt=0;
                while(it1!=currentstack.end())
                {
                    cnt++;
                    it1++;
                }
                ans=min(ans,cnt);
            }
        }
    }
    currentstack.erase(currentstack.find(tin[u]));
}
int main()
{
    int n,k;
    cin>>n>>k;
    int c[1001]={0};
    int x;int v;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        c[x]++;
    }
   // cout<<1<<endl;
    //mp[-3000].pb(1);
    //cout<<mp[-3000][0]<<endl;
    int mx=-1;
    for(int i=-2000;i<=2000;i++)
     {
        //cout<<1<<endl;
        for(int j=0;j<=1000;j++)
        {
            if(c[j])
            {
                x=j-n;
                v=i+x+2001;
                mx=max(mx,v);
                if(v<=4001&&v>=0)
                {
                    //cout<<1<<endl;
                    adj[i+2001].pb(v);
                }
            }
        }
    }
    //for(int i=0;i<adj[2001].size();i++)
    //cout<<adj[2001][i]<<endl;
   // cout<<mx<<endl;
   // cout<<"ok"<<endl;
  //  for(int i=0;i<=4001;i++)
   // {

        //while(!visited.empty())
         //   visited.erase(visited.begin());
        dfs(2001);

    //}
    if(ans==1000000000)
        cout<<-1<<endl;
    else
    cout<<ans<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const int N=1e3+3;
int mark[N];
int main()
{
    int n,k;IOS;
    cin>>n>>k;int mn=1000000,mx=-1;int m=1e5;int x;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        mn=min(mn,x);
        mx=max(mx,x);
        mark[x]=1;
    }
    if(mark[n]==1)
    {
        cout<<"1"<<endl;
        return 0;
    }
    if(mn>n||mx<n)
    {
        cout<<"-1"<<endl;
        return 0;
    }
    int dp1[m],dp2[m];
    for(int i=0;i<m;i++)
    {
        dp1[i]=m;dp2[i]=m;
    }
    dp1[0]=0;dp2[0]=0;int v;
    for(int i=0;i<n;i++)
    {
        if(mark[i])
        {
            v=n-i;
            for(int j=v;j<m;j++)
                if(dp1[j-v]+1<dp1[j])
                    dp1[j]=dp1[j-v]+1;
        }
    }
    for(int i=n+1;i<N;i++)
    {
        if(mark[i])
        {
            v=i-n;
            for(int j=v;j<m;j++)
                if(dp2[j-v]+1<dp2[j])
                    dp2[j]=1+dp2[j-v];
        }
    }
    int ans=m;
    for(int i=1;i<m;i++)
        ans=min(ans,dp1[i]+dp2[i]);
    if(ans==m)
        cout<<"-1"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}


//#include "stdc++.h"
#include<bits/stdc++.h>
#include<iostream>
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define pb push_back
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
inline ll gcd(ll a,ll b){if(a==0) return b;return gcd(b%a,a);}
inline ll power(ll a,ll n,ll m){if(n==0) return 1;ll p=power(a,n/2,m);p=(p*p)%m;if(n%2) return (p*a)%m;else return p; }
#define flush fflush(stdout)
const ll MOD =998244353;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
vector<int> adj[10000];
set<int>visited;
set<int>::iterator it;
int Time=0;
map<int,int>currentstack;
map<int,int>::iterator it1;
map<int,int>tin;
int ans=10000000;
void dfs(int u)
{
    visited.insert(u);
    Time++;
    currentstack.insert(m1(Time,u));
    tin.insert(m1(u,Time));int v;
    for(int i=0;i<adj[u].size();i++)
    {
        v=adj[u][i];
        it=visited.find(v);
        if(it==visited.end())
        {
            dfs(v);
        }
        else
        {
            it1=currentstack.find(tin[v]);
            if(it1!=currentstack.end())
            {
                it1=currentstack.lower_bound(tin[v]);int cnt=0;
                while(it1!=currentstack.end())
                {
                    cnt++;
                    it1++;
                }
                ans=min(ans,cnt);
            }
        }
    }
    currentstack.erase(currentstack.find(tin[u]));
}
int main()
{
    int n,k;
    cin>>n>>k;
    int c[1001]={0};
    int x;int v;
    for(int i=0;i<k;i++)
    {
        cin>>x;
        c[x]++;
    }
   // cout<<1<<endl;
    //mp[-3000].pb(1);
    //cout<<mp[-3000][0]<<endl;
    int mx=-1;
    for(int i=-2000;i<=2000;i++)
     {
        //cout<<1<<endl;
        for(int j=0;j<=1000;j++)
        {
            if(c[j])
            {
                x=j-n;
                v=i+x+2001;
                mx=max(mx,v);
                if(v<=4001&&v>=0)
                {
                    //cout<<1<<endl;
                    adj[i+2001].pb(v);
                }
            }
        }
    }
    //for(int i=0;i<adj[2001].size();i++)
    //cout<<adj[2001][i]<<endl;
   // cout<<mx<<endl;
   // cout<<"ok"<<endl;
  //  for(int i=0;i<=4001;i++)
   // {

        //while(!visited.empty())
         //   visited.erase(visited.begin());
        dfs(2001);

    //}
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;

int n, k;
int d[2002];
vector<int> g[2002];

void bfs(int s) {
	vector<int> q;
	d[s] = 0;
	q.push_back(s);
	for (int i = 0; i < q.size(); i++)
		for (auto v: g[q[i]])
			if (d[v] == -1) {
				d[v] = d[q[i]] + 1;
				q.push_back(v);
			}
}

int main() {
	scanf("%d %d", &n, &k);
	memset(d, -1, sizeof d);
	while (k--) {
		int tmp;
		scanf("%d", &tmp);
		tmp -= n;
		g[2001].push_back(tmp + 1000);
		for (int i = -1000; i <= 1000; i++)
			if (tmp + i <= 1000 && tmp + i >= -1000)
				g[i + 1000].push_back(tmp + i + 1000);
	}
	bfs(2001);
	printf("%d\n", d[1000]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, k;
int d[2001];
vector<int> a;

void bfs() {
	vector<int> q;
	for (auto x: a) {
		q.push_back(x + 1000);
		d[x + 1000] = 1;
	}
	for (int i = 0; i < q.size(); i++)
		for (auto x: a)
			if (q[i] + x <= 2000 && q[i] + x >= 0 && d[q[i] + x] == -1) {
				d[q[i] + x] = d[q[i]] + 1;
				q.push_back(q[i] + x);
			}
}

int main() {
	scanf("%d %d", &n, &k);
	memset(d, -1, sizeof d);
	while (k--) {
		int tmp;
		scanf("%d", &tmp);
		a.push_back(tmp - n);
	}
	bfs();
	printf("%d\n", d[1000]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n, k;
int d[2001];
vector<int> a;

void bfs() {
	vector<int> q;
	for (auto x: a) {
		q.push_back(x + 1000);
		d[x + 1000] = 1;
	}
	for (int i = 0; i < q.size(); i++)
		for (auto x: a)
			if (q[i] + x <= 2000 && q[i] + x >= 0 && d[q[i] + x] == -1) {
				d[q[i] + x] = d[q[i]] + 1;
				q.push_back(q[i] + x);
			}
}

int main() {
	scanf("%d %d", &n, &k);
	memset(d, -1, sizeof d);
	while (k--) {
		int tmp;
		scanf("%d", &tmp);
		a.push_back(tmp - n);
	}
	sort(a.begin(), a.end());
	auto it = unique(a.begin(), a.end());
	a.resize(distance(a.begin(), it));

	bfs();
	printf("%d\n", d[1000]);
	return 0;
}