#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

#define MEMSET(x, WITH) memset(x, (WITH), sizeof(x))
#define FOR(i, E) for (int i=0; i<(E); i++)
typedef long long ll;
const ll MOD = 1000000007;
//const double PI = atan(1) * 4;

int N, K;

bool appeared[1003];
vector<int> num1, num2;

int ju1[250333];
int ju2[250333];

int gcd(int a, int b) {
	if (b == 0) return a;
	else return gcd(b, a%b);
}

int main() {
	scanf("%d%d", &N, &K);

	FOR(zz, K) {
		int a; scanf("%d", &a);
		if (appeared[a]) continue;

		a -= N;
		if (a == 0) {
			puts("1");
			exit(0);
		}
		else if (a > 0)
			num1.push_back(a);
		else
			num2.push_back(-a);
	}

	if (num1.empty() || num2.empty()) {
		puts("-1");
		exit(0);
	}

	const int A1 = *max_element(num1.begin(), num1.end());
	const int A2 = *max_element(num2.begin(), num2.end());

	int lcm = A1 * A2 / gcd(A1, A2);
	int ans = A1 + A2;

	for (int d = 1; d < lcm; d++) {
		ju1[d] = 123456;
		for (int n : num1) if (d - n >= 0)
			ju1[d] = min(ju1[d], 1 + ju1[d - n]);

		ju2[d] = 123456;
		for (int n : num2) if (d - n >= 0)
			ju2[d] = min(ju2[d], 1 + ju2[d - n]);

		ans = min(ans, ju1[d] + ju2[d]);
	}

	printf("%d\n", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

#define MEMSET(x, WITH) memset(x, (WITH), sizeof(x))
#define FOR(i, E) for (int i=0; i<(E); i++)
typedef long long ll;
const ll MOD = 1000000007;
//const double PI = atan(1) * 4;

int N, K;

bool appeared[1003];
vector<int> num1, num2;

int ju1[250333];
int ju2[250333];

int gcd(int a, int b) {
	if (b == 0) return a;
	else return gcd(b, a%b);
}

int main() {
	scanf("%d%d", &N, &K);

	FOR(zz, K) {
		int a; scanf("%d", &a);
		if (appeared[a]) continue;
		appeared[a] = true;

		a -= N;
		if (a == 0) {
			puts("1");
			exit(0);
		}
		else if (a > 0)
			num1.push_back(a);
		else
			num2.push_back(-a);
	}

	if (num1.empty() || num2.empty()) {
		puts("-1");
		exit(0);
	}

	const int A1 = *max_element(num1.begin(), num1.end());
	const int A2 = *max_element(num2.begin(), num2.end());

	int lcm = A1 * A2 ;
	int ans = (A1 + A2) ;

	for (int d = 1; d < lcm; d++) {
		ju1[d] = 123456;
		for (int n : num1) if (d - n >= 0)
			ju1[d] = min(ju1[d], 1 + ju1[d - n]);

		ju2[d] = 123456;
		for (int n : num2) if (d - n >= 0)
			ju2[d] = min(ju2[d], 1 + ju2[d - n]);

		ans = min(ans, ju1[d] + ju2[d]);
	}

	printf("%d\n", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

#define MEMSET(x, WITH) memset(x, (WITH), sizeof(x))
#define FOR(i, E) for (int i=0; i<(E); i++)
typedef long long ll;
const ll MOD = 1000000007;
//const double PI = atan(1) * 4;

int N, K;

bool appeared[1003];
vector<int> num1, num2;

int ju1[250333];
int ju2[250333];

int main() {
	scanf("%d%d", &N, &K);

	FOR(zz, K) {
		int a; scanf("%d", &a);
		if (appeared[a]) continue;

		a -= N;
		if (a == 0) {
			puts("1");
			exit(0);
		}
		else if (a > 0)
			num1.push_back(a);
		else
			num2.push_back(-a);
	}

	if (num1.empty() || num2.empty()) {
		puts("-1");
		exit(0);
	}

	const int A1 = *max_element(num1.begin(), num1.end());
	const int A2 = *max_element(num2.begin(), num2.end());

	int ans = A1 + A2;

	for (int d = 1; d < A1*A2; d++) {
		ju1[d] = 123456;
		for (int n : num1) if (d - n >= 0)
			ju1[d] = min(ju1[d], 1 + ju1[d - n]);

		ju2[d] = 123456;
		for (int n : num2) if (d - n >= 0)
			ju2[d] = min(ju2[d], 1 + ju2[d - n]);

		ans = min(ans, ju1[d] + ju2[d]);
	}

	printf("%d\n", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

#define MEMSET(x, WITH) memset(x, (WITH), sizeof(x))
#define FOR(i, E) for (int i=0; i<(E); i++)
typedef long long ll;
const ll MOD = 1000000007;
//const double PI = atan(1) * 4;

int N, K;

bool appeared[1003];
vector<int> num1, num2;

int ju1[250333];
int ju2[250333];

int gcd(int a, int b) {
	if (b == 0) return a;
	else return gcd(b, a%b);
}

int main() {
	scanf("%d%d", &N, &K);

	FOR(zz, K) {
		int a; scanf("%d", &a);
		if (appeared[a]) continue;
		appeared[a] = true;

		a -= N;
		if (a == 0) {
			puts("1");
			exit(0);
		}
		else if (a > 0)
			num1.push_back(a);
		else
			num2.push_back(-a);
	}

	if (num1.empty() || num2.empty()) {
		puts("-1");
		exit(0);
	}

	const int A1 = *max_element(num1.begin(), num1.end());
	const int A2 = *max_element(num2.begin(), num2.end());

	int lcm = A1 * A2 / gcd(A1, A2);
	int ans = (A1 + A2) / gcd(A1, A2);

	for (int d = 1; d < lcm; d++) {
		ju1[d] = 123456;
		for (int n : num1) if (d - n >= 0)
			ju1[d] = min(ju1[d], 1 + ju1[d - n]);

		ju2[d] = 123456;
		for (int n : num2) if (d - n >= 0)
			ju2[d] = min(ju2[d], 1 + ju2[d - n]);

		ans = min(ans, ju1[d] + ju2[d]);
	}

	printf("%d\n", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
using namespace std;

#define MEMSET(x, WITH) memset(x, (WITH), sizeof(x))
#define FOR(i, E) for (int i=0; i<(E); i++)
typedef long long ll;
const ll MOD = 1000000007;
//const double PI = atan(1) * 4;

int N, K;

bool appeared[1003];
vector<int> num1, num2;

int ju1[250333];
int ju2[250333];

int gcd(int a, int b) {
	if (b == 0) return a;
	else return gcd(b, a%b);
}

int main() {
	scanf("%d%d", &N, &K);

	FOR(zz, K) {
		int a; scanf("%d", &a);
		if (appeared[a]) continue;

		a -= N;
		if (a == 0) {
			puts("1");
			exit(0);
		}
		else if (a > 0)
			num1.push_back(a);
		else
			num2.push_back(-a);
	}

	if (num1.empty() || num2.empty()) {
		puts("-1");
		exit(0);
	}

	const int A1 = *max_element(num1.begin(), num1.end());
	const int A2 = *max_element(num2.begin(), num2.end());

	int lcm = A1 * A2 / gcd(A1, A2);
	int ans = (A1 + A2) / gcd(A1, A2);

	for (int d = 1; d < lcm; d++) {
		ju1[d] = 123456;
		for (int n : num1) if (d - n >= 0)
			ju1[d] = min(ju1[d], 1 + ju1[d - n]);

		ju2[d] = 123456;
		for (int n : num2) if (d - n >= 0)
			ju2[d] = min(ju2[d], 1 + ju2[d - n]);

		ans = min(ans, ju1[d] + ju2[d]);
	}

	printf("%d\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) {
            return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }

    return -1;
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    scanf("%d %d", &n, &k );
    int mxn = INT_MIN, mnn = INT_MAX ;
    for (int i = 0; i < k; ++i)
    {   int x;
        scanf("%d", &x );
        if (x == n)
        {
            /* code */
            cout<<2<<endl;

            return 0;
        }

        mxn = max(mxn, x);
        mnn = min(mnn, x);
        ara[x]++;
        /* code */
    }

    if(mnn>n)
    {
        std::cout << -1 << "\n";

        return 0;
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

    //for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
    return -1;
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;

    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        if(x == n)
        {
            cout<<1<<endl;
            return 0;
        }

        ara[x]++;
        /* code */
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;
    int mxn = INT_MIN, mnn = INT_MAX ;
    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        if (x == n)
        {
            /* code */
            cout<<1<<endl;

            return 0;
        }

        mxn = max(mxn, x);
        mnn = min(mnn, x);
        ara[x]++;
        /* code */
    }

    if(mnn>n)
    {
        std::cout << -1 << "\n";

        return 0;
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;
    bool f = false;
    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        if (x == n)
        {
            /* code */
            cout<<1<<endl;

            return 0;
        }

        if(x<n) f = true;
        ara[x]++;
        /* code */
    }

    if(!f)
    {
        std::cout << -1 << "\n";

        return 0;
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
    return -1;
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;

    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        ara[x]++;
        /* code */
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;
    bool f = false;
    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        if (x == n)
        {
            /* code */
            cout<<1<<endl;

            return 0;
        }

        if(x<n) f = true;
        ara[x]++;
        /* code */
    }

    if(f)
    {
        std::cout << -1 << "\n";

        return 0;
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;

    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        if (x == n)
        {
            /* code */
            cout<<1<<endl;

            return 0;
        }
        ara[x]++;
        /* code */
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    std::cin >> n >> k;

    for (int i = 0; i < k; ++i)
    {   int x;
        cin>>x;
        if (x == n) return 1;
        {
            /* code */
        }
        ara[x]++;
        /* code */
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) {
            return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }

    return -1;
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    scanf("%d %d", &n, &k );
    int mxn = INT_MIN, mnn = INT_MAX ;
    for (int i = 0; i < k; ++i)
    {   int x;
        scanf("%d", &x );
        if (x == n)
        {
            /* code */
            cout<<1<<endl;

            return 0;
        }

        mxn = max(mxn, x);
        mnn = min(mnn, x);
        ara[x]++;
        /* code */
    }

    if(mnn>n)
    {
        std::cout << -1 << "\n";

        return 0;
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

    //for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

std::vector<int> ss;

int bfs()
{
    std::queue<int> Q;

    Q.push(0);
    int d = 0;

    bool f = false;

    map<int,int> vis;

    while (!Q.empty())
    {
        /* code */

        int u = Q.front();

        d = vis[u];

        Q.pop() ;

        f = true;

        for(int i = 0; i < ss.size(); i++)
        {
            int v = u + ss[i];

            if(v==0) { return d+1;}

            if(v>=-1000 && v<= 1000 && vis.count(v) ==0)
            {
                vis[v] = d+1;
                Q.push(v);
            }

        }

    }
}

int main(int argc, char* argv[])
{
    int n, k;

    int ara[1003];

    memset(ara, 0, sizeof ara);

    scanf("%d %d", &n, &k );
    int mxn = INT_MIN, mnn = INT_MAX ;
    for (int i = 0; i < k; ++i)
    {   int x;
        scanf("%d", &x );
        if (x == n)
        {
            /* code */
            cout<<1<<endl;

            return 0;
        }

        mxn = max(mxn, x);
        mnn = min(mnn, x);
        ara[x]++;
        /* code */
    }

    if(mnn>n)
    {
        std::cout << -1 << "\n";

        return 0;
    }

    for(int i = 0; i<=1000; i++)
    {
        if(ara[i]) ss.push_back(i-n);
    }

   // for(int i = 0; i<ss.size(); i++) cout<<ss[i]<<' ';

   // cout<<endl;

    std::cout << bfs() << "\n";
    return 0;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 16050
#define offset 8010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<ll,ll> ,ll> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
ll dp[tam];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    ll m,n;
    cin>>m>>n;
    set<ll> s;
    ll val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    forr(i,0,tam)
    dp[i]=1e18;
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(ll i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(ll i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    if (dp[offset]==1e9)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 16050
#define offset 8010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<ll,ll> ,ll> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
ll dp[tam];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    ll m,n;
    cin>>m>>n;
    set<ll> s;
    ll val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    const ll INF=1e18;
    forr(i,0,tam)
    dp[i]=INF;
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(ll i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(ll i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    if (dp[offset]==INF)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 16050
#define offset 8010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<ll,ll> ,ll> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
ll dp[tam];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    ll m,n;
    cin>>m>>n;
    set<ll> s;
    ll val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    forr(i,0,tam)
    dp[i]=1e9;
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(ll i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(ll i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    if (dp[offset]==1e9)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 8050
#define offset 4010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<ll,ll> ,ll> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
ll dp[tam];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    ll m,n;
    cin>>m>>n;
    set<ll> s;
    ll val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    const ll INF=1e18;
    forr(i,0,tam)
    dp[i]=INF;
    queue<int> cola;
    vector<int> valores;
    for(set<ll>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        valores.pb(valor);
        cola.push(offset+valor);
        dp[offset+valor]=1;
    }
    while(!cola.empty())
    {
        int num=cola.front();cola.pop();
        forr(i,0,valores.size())
        {
            if (valores[i]+num<0|| valores[i]+num>=tam)continue;
            if (dp[valores[i]+num]==INF)
            {
                dp[valores[i]+num]=dp[num]+1;
                cola.push(valores[i]+num);
            }
        }

    }
    if (dp[offset]==INF)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 16050
#define offset 8010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<int,int> ,int> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
int dp[tam];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    int m,n;
    cin>>m>>n;
    set<int> s;
    int val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    forr(i,0,tam)
    dp[i]=1e9;
    for(set<int>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(int i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    if (dp[offset]==1e9)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 12000
#define offset 5010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<int,int> ,int> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
int dp[12000];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    int m,n;
    cin>>m>>n;
    set<int> s;
    int val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    forr(i,0,tam)
    dp[i]=1e9;
    for(set<int>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(int i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    if (dp[offset]==1e9)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define forr(i,p,n) for(ll i=p;i<n;i++)
#define tam 16050
#define offset 8010
using namespace std;
typedef long long  ll;
typedef pair<ll,ll> ii;
typedef pair<map<int,int> ,int> mi;
typedef pair<ii,ii> iiii;
typedef vector<ll> vi;
const ll MOD=1e9+7;
int dp[12000];
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    int m,n;
    cin>>m>>n;
    set<int> s;
    int val,valor;
    forr(i,0,n)
    {
        cin>>val;
        s.insert(val-m);
    }
    forr(i,0,tam)
    dp[i]=1e9;
    for(set<int>::iterator ite=s.begin();ite!=s.end();ite++)
    {
        valor=*ite;
        //cout<<valor<<" ";
        //continue;
        dp[offset+valor]=1;
        if (valor<0)
        {
            for(int i=tam-1;i>=0;i--)
            {
                if (i+valor>=0)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
        else
        {
            forr(i,0,tam)
            {
                if (i+valor<tam)
                    dp[i+valor]=min(dp[i+valor],dp[i]+1);
            }
        }
    }
    if (dp[offset]==1e9)
        cout<<-1<<endl;
    else
        cout<<dp[offset]<<endl;
}


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

bool used[2001];
bool exist[1000];
int dist[2001];
queue<int> q;

int main()
{
	int n,k,a1;
	cin>>n>>k;
	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
	for(int i=0;i<1000;++i){
		exist[i]=0;
	}
	for(int i=0;i<k;++i){
		cin>>a1;
		exist[a1]=1;
		a1-=n;
		dist[a1+1000]=1;
		used[a1+1000]=1;
		q.push(a1);
	}
	while(!q.empty()){
		int tmp=q.front();
		q.pop();
		if(tmp==0) break;
		for(int i=0;i<=1000;++i){
			if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
				used[tmp+i+1000-n]=1;
				q.push(tmp+i-n);
				dist[tmp+i+1000-n]=dist[tmp+1000]+1;
			}
		}
	}
	cout<<dist[1000];
  return 0;
};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int *v;
	queue<int> q;

	int main()
	{
		int n,k;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		v=new int[k];
		for(int i = 0; i <k; i++){
    cin >> v[i];
    v[i]-=n;
    if(v[i] < 0)
      isneg = true;
    else if(v[i] > 0)
      ispos = true;
    else if(!v[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispos == false)){
    cout <<' '<< -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=0;i<k;++i){
			v[i]-=n;
			q.push(v[i]);
			used[v[i]+1000]=1;
			dist[v[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k;++i){
				if(v[i]+tmp>=-1000&&v[i]+tmp<=1000&&(!used[tmp+v[i]+1000])){
					used[v[i]+1000+tmp]=1;
					q.push(tmp+v[i]);
					dist[tmp+v[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			a1-=n;
			dist[a1+1000]=1;
			used[a1+1000]=1;
			if(dist[1000]!=-1) {cout<<dist[1000];return 0;}
			q.push(a1);
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) break;
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=a1-n;
			if(isequal){cout<<1;return 1;}
			if(in>0) ispos=true; else
			if(in<0) isneg=true; else
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			exit(0);
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				exit(0);
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=i-n;
			if(in>0) ispos=true; else
			if(in<0) isneg=true; else
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			return 0;
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				return 0;
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
		}
		for(int i=0;i<=1000;++i){
			dist[i-n+1000]=1;
			used[i-n+1000]=1;
			if(exist[i]) q.push(i-n);
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) break;
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
//#include <fstream>
#include <cstdlib>

using namespace std;

//ifstream cin("data.in");
//ofstream cout("data.out");

int const KMAX = 1000010;
int const VMAX = 2010;
int const DETA = 1000;

int n, k, k2, v2[KMAX], cost[VMAX], v[KMAX];
bool ispoz, isneg, isequal,used[VMAX];

void BFS(int x, int steps){
  for(int i = 1; i <= k; i++){
    if((!cost[x + v[i] + DETA] || steps + 1 < cost[x + v[i] + DETA]) && (x + v[i] <= 1000) && (x + v[i] >= -1000)){
      cost[x + v[i] + DETA] = steps + 1;
      BFS(x + v[i], steps + 1);
    }
  }
}

int main()
{
  std::ios_base::sync_with_stdio (false);
  ispoz = isneg = isequal = false;
  cin >> n >> k2;
  for(int i = 1; i <= k2; i++){
    cin >> v2[i];
    v2[i] -= n;
    if(v2[i] < 0)
      isneg = true;
    else if(v2[i] > 0)
      ispoz = true;
    else if(!v2[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispoz == false)){
    cout << -1 << '\n';
    exit(EXIT_SUCCESS);
  }

  for(int i = 1; i <= k2; i++){
    if(!used[DETA + v2[i]]){
      v[++k] = v2[i];
      used[DETA + v2[i]] = true;
    }
  }

  BFS(0, 0);
  cout << cost[DETA];

  return 0;
}


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i = 1; i <= k; i++){
    cin >> exist[i];
    int in=exist[i]-n;
    if(in < 0)
      isneg = true;
    else if(in > 0)
      ispos = true;
    else if(in)
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispos == false)){
    cout << -1 << '\n';
    exit(EXIT_SUCCESS);
  }
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=a1-n;
			if(in>0) ispos=true;
			if(in<0) isneg=true;
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			return 0;
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				return 0;
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int *v;
	queue<int> q;

	int main()
	{
		int n,k;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		v=new int[k];
		for(int i = 0; i <k; i++){
    cin >> v[i];
    v[i]-=n;
    if(v[i] < 0)
      isneg = true;
    else if(v[i] > 0)
      ispos = true;
    else if(!v[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispos == false)){
    cout <<' '<< -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=0;i<k;++i){
			q.push(v[i]);
			used[v[i]+1000]=1;
			dist[v[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k;++i){
				if(v[i]+tmp>=-1000&&v[i]+tmp<=1000&&(!used[tmp+v[i]+1000])){
					used[v[i]+1000+tmp]=1;
					q.push(tmp+v[i]);
					dist[tmp+v[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
#include <vector>
#include <queue>

using namespace std;

bool used[2001];
bool exist[1000];
int dist[2001];
queue<int> q;

int main()
{
	int n,k,a1;
	cin>>n>>k;
	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
	for(int i=0;i<1000;++i){
		exist[i]=0;
	}
	for(int i=0;i<k;++i){
		cin>>a1;
		exist[a1]=1;
		a1-=n;
		dist[a1+1000]=1;
		used[a1+1000]=1;
		q.push(a1);
	}
	while(!q.empty()){
		int tmp=q.front();
		q.pop();
		if(tmp==0) break;
		for(int i=0;i<1000;++i){
			if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
				used[tmp+i+1000-n]=1;
				q.push(tmp+i-n);
				dist[tmp+i+1000-n]=dist[tmp+1000]+1;
			}
		}
	}
	cout<<dist[1000];
  return 0;
};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int v[100*10001];
	bool ispoz,isneg,isequal;
	int k2
	queue<int> q;

	int main()
	{
	ispoz = isneg = isequal = false;
  cin >> n >> k2;
  for(int i = 1; i <= k2; i++){
    cin >> v2[i];
    v2[i] -= n;
    if(v2[i] < 0)
      isneg = true;
    else if(v2[i] > 0)
      ispoz = true;
    else if(!v2[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispoz == false)){
    cout << -1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispos == false)){
    cout <<' '<< -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=0;i<k2;++i){
			q.push(v[i]);
			used[v[i]+1000]=1;
			dist[v[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k2;++i){
				if(v[i]+tmp>=-1000&&v[i]+tmp<=1000&&(!used[tmp+v[i]+1000])){
					used[v[i]+1000+tmp]=1;
					q.push(tmp+v[i]);
					dist[tmp+v[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=a1-n;
			if(isequal){cout<<1;return 0;}
			if(in>0) ispos=true; else
			if(in<0) isneg=true; else
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			exit(0);
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				exit(0);
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			a1-=n;
			dist[a1+1000]=1;
			used[a1+1000]=1;
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]) q.push(i-n);
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) break;
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				q.push(i-n);
				dist[i-n+1000]=1;
				used[i-n+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				if(in>0) ispos=true;
				if(in<0) isneg=true;
				if(in==0) isequal=true;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		if(isequal){
			cout<<1;
			return 0;
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				return 0;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=a1-n;
			if(in>0) ispos=true; else
			if(in<0) isneg=true; else
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			exit(0);
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				exit(0);
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				q.push(i-n);
				dist[i-n+1000]=1;
				used[i-n+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) break;
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int *v;
	queue<int> q;

	int main()
	{
		int n,k;
		bool ispos=false,isneg=false,isequal=false;
		cin>>n>>k;
		v=new int[k];
		for(int i = 0; i <k; i++){
    cin >> v[i];
    v[i]-=n;
    if(v[i] < 0)
      isneg = true;
    else if(v[i] > 0)
      ispos = true;
    else if(!v[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispos == false)){
    cout <<' '<< -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=0;i<k;++i){
			q.push(v[i]);
			used[v[i]+1000]=1;
			dist[v[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k;++i){
				if(v[i]+tmp>=-1000&&v[i]+tmp<=1000&&(!used[tmp+v[i]+1000])){
					used[v[i]+1000+tmp]=1;
					q.push(tmp+v[i]);
					dist[tmp+v[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			a1-=n;
			dist[a1+1000]=1;
			used[a1+1000]=1;
			q.push(a1);
		}
		if(dist[1000]!=-1) {cout<<dist[1000];return 0;}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) break;
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int *v;
	queue<int> q;

	int main()
	{
		int n,k;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		v=new int[k];
		for(int i = 0; i <k; i++){
    cin >> v[i];
    int in=v[i]-n;
    if(in < 0)
      isneg = true;
    else if(in > 0)
      ispos = true;
    else if(in)
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispos == false)){
    cout <<' '<< -1 << '\n';
    exit(EXIT_SUCCESS);
  }
		for(int i=0;i<k;++i){
			v[i]-=n;
			q.push(v[i]);
			used[v[i]+1000]=1;
			dist[v[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k;++i){
				if(v[i]+tmp>=-1000&&v[i]+tmp<=1000&&(!used[tmp+v[i]+1000])){
					used[v[i]+1000+tmp]=1;
					q.push(tmp+v[i]);
					dist[tmp+v[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=a1-n;
			if(in>0) ispos=true;
			if(in<0) isneg=true;
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			exit(0);
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				exit(0);
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			int in=i-n;
			if(in>0) {ispos=true;} else
			{if(in<0) {isneg=true;} else
			if(in==0) isequal=true;}
		}
		if(isequal){
			cout<<1;
			return 0;
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				return 0;
			}
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]){
				int in=i-n;
				q.push(in);
				dist[in+1000]=1;
				used[in+1000]=1;
			}
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int v2[100*10001];
	bool ispoz,isneg,isequal;
	int k2;
	queue<int> q;
	int n;

	int main()
	{
		std::ios_base::sync_with_stdio (false);
	ispoz = isneg = isequal = false;
  cin >> n >> k2;
  for(int i = 0; i < k2; i++){
    cin >> v2[i];
    v2[i] -= n;
    if(v2[i] < 0)
      isneg = true;
    else if(v2[i] > 0)
      ispoz = true;
    else if(!v2[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispoz == false)){
    cout << -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=0;i<k2;++i){
			q.push(v2[i]);
			used[v2[i]+1000]=1;
			dist[v2[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k2;++i){
				if(v2[i]+tmp>=-1000&&v2[i]+tmp<=1000&&(!used[tmp+v2[i]+1000])){
					used[v2[i]+1000+tmp]=1;
					q.push(tmp+v2[i]);
					dist[tmp+v2[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


	#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q;

	int main()
	{
		int n,k,a1;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			exist[a1]=1;
			a1-=n;
			dist[a1+1000]=1;
			used[a1+1000]=1;
		}
		for(int i=0;i<=1000;++i){
			if(exist[i]) q.push(i);
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) break;
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	queue<int> q,z;

	int main()
	{
		int n,k,a1;
		bool ispos=false,isneg=true,isequal=false;
		cin>>n>>k;
		for(int i=0;i<2001;++i){
			dist[i]=-1;
			used[i]=0;
		}
		for(int i=0;i<1000;++i){
			exist[i]=0;
		}
		for(int i=0;i<k;++i){
			cin>>a1;
			z.push(a1);
			int in=a1-n;
			if(isequal){cout<<1;return 0;}
			if(in>0) ispos=true; else
			if(in<0) isneg=true; else
			if(in==0) isequal=true;
		}
		if(isequal){
			cout<<1;
			exit(0);
		}else{
			if((!isneg)||(!ispos)){
				cout<<-1;
				exit(0);
			}
		}
	while(!z.empty()){
		int i=z.front();
		z.pop();
		int in=i-n;
		q.push(in);
		dist[in+1000]=1;
		used[in+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0) {cout<<dist[1000];return 0;}
			for(int i=0;i<=1000;++i){
				if(exist[i]&&tmp+i-n>=-1000&&tmp+i-n<=1000&&!used[tmp+i-n+1000]){
					used[tmp+i+1000-n]=1;
					q.push(tmp+i-n);
					dist[tmp+i+1000-n]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2002];
	bool exist[1000];
	int dist[2002];
	int v2[100*10001];
	bool ispoz,isneg,isequal;
	int k2;
	queue<int> q;
	int n;

	int main()
	{
	ispoz = isneg = isequal = false;
  cin >> n >> k2;
  for(int i = 1; i <= k2; i++){
    cin >> v2[i];
    v2[i] -= n;
    if(v2[i] < 0)
      isneg = true;
    else if(v2[i] > 0)
      ispoz = true;
    else if(!v2[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispoz == false)){
    cout << -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2002;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=1;i<=k2;++i){
			q.push(v2[i]);
			used[v2[i]+1000]=1;
			dist[v2[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k2;++i){
				if(v2[i]+tmp>=-1000&&v2[i]+tmp<=1000&&(!used[tmp+v2[i]+1000])){
					used[v2[i]+1000+tmp]=1;
					q.push(tmp+v2[i]);
					dist[tmp+v2[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


#include <iostream>
	#include <vector>
	#include <queue>

	using namespace std;

	bool used[2001];
	bool exist[1000];
	int dist[2001];
	int v2[100*10001];
	bool ispoz,isneg,isequal;
	int k2;
	queue<int> q;
	int n;

	int main()
	{
	ispoz = isneg = isequal = false;
  cin >> n >> k2;
  for(int i = 0; i < k2; i++){
    cin >> v2[i];
    v2[i] -= n;
    if(v2[i] < 0)
      isneg = true;
    else if(v2[i] > 0)
      ispoz = true;
    else if(!v2[i])
      isequal = true;
  }

  if(isequal == true){
    cout << 1 << '\n';
    exit(EXIT_SUCCESS);
  }

  if((isneg == false || ispoz == false)){
    cout << -1 << '\n';
    exit(EXIT_SUCCESS);
  }

	for(int i=0;i<2001;++i){
		dist[i]=-1;
		used[i]=0;
	}
		for(int i=0;i<k2;++i){
			q.push(v2[i]);
			used[v2[i]+1000]=1;
			dist[v2[i]+1000]=1;
		}
		while(!q.empty()){
			int tmp=q.front();
			q.pop();
			if(tmp==0){cout<<dist[1000];exit(EXIT_SUCCESS);}
			for(int i=0;i<k2;++i){
				if(v2[i]+tmp>=-1000&&v2[i]+tmp<=1000&&(!used[tmp+v2[i]+1000])){
					used[v2[i]+1000+tmp]=1;
					q.push(tmp+v2[i]);
					dist[tmp+v2[i]+1000]=dist[tmp+1000]+1;
				}
			}
		}
		cout<<dist[1000];
	  return 0;
	};


/* Eat a live frog first thing in the morning,
   and nothing worse will happen to you the rest of the day */

/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define dbg(x,y) cerr << x << " " << y << endl;
#define db(x) cerr << x << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

#define N 20007

int test;

int n,k;

int t[N]; // 1001 - koncowy

queue<int>q;

void solve()
{
       cin >> k >> n;

       FOR(i,1,n)
       {
              int x;
              cin >> x;

              if (!t[x-k+1001])
                     q.push(x-k+1001);

              t[x-k+1001]=1;
       }

       while(!q.empty())
       {
              int v=q.front();
              q.pop();

              FOR(i,1,2001)
              {
                     if ( t[i]==1 && t[v+i-1001]==0 && v+i-1001>=0 )
                     {
                            t[v+i-1001]=t[v]+1;
                            q.push(v+i-1001);
                     }
              }

              if (t[1001]>0)
              {
                     cout << t[1001];
                     return;
              }
       }

       cout << -1;

       return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
         solve();
    }

  return 0;
}


/* Eat a live frog first thing in the morning,
   and nothing worse will happen to you the rest of the day */

/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define dbg(x,y) cerr << x << " " << y << endl;
#define db(x) cerr << x << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

#define N 20007

int test;

int n,k;

int t[N]; // 1001 - koncowy

queue<int>q;

void solve()
{
       cin >> k >> n;

       FOR(i,1,n)
       {
              int x;
              cin >> x;

              if (!t[x-k+1001])
                     q.push(x-k+1001);

              t[x-k+1001]=1;
       }

       while(!q.empty())
       {
              int v=q.front();
              q.pop();

              FOR(i,1,2001)
              {
                     if ( t[i]==1 && t[v+i-1001]==0 && v+i-1001>=0 && v+i-1001<=2001)
                     {
                            t[v+i-1001]=t[v]+1;
                            q.push(v+i-1001);
                     }
              }

              if (t[1001]>0)
              {
                     cout << t[1001];
                     return;
              }
       }

       cout << -1;

       return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
         solve();
    }

  return 0;
}


/* Eat a live frog first thing in the morning,
   and nothing worse will happen to you the rest of the day */

/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define dbg(x,y) cerr << x << " " << y << endl;
#define db(x) cerr << x << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

#define N 2007

int test;

int n,k;

int t[N]; // 1001 - koncowy

queue<int>q;

void solve()
{
       cin >> k >> n;

       FOR(i,1,n)
       {
              int x;
              cin >> x;

              if (!t[x-k+1001])
                     q.push(x-k+1001);

              t[x-k+1001]=1;
       }

       while(!q.empty())
       {
              int v=q.front();
              q.pop();

              FOR(i,1,2001)
              {
                     if ( t[i]>0 && t[v+i-1001]==0 && v+i-1001>0 )
                     {
                            t[v+i-1001]=t[i]+t[v];
                            q.push(v+i-1001);
                     }
              }

              if (t[1001]>0)
              {
                     cout << t[1001];
                     return;
              }
       }

       cout << -1;

       return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
         solve();
    }

  return 0;
}


/* Eat a live frog first thing in the morning,
   and nothing worse will happen to you the rest of the day */

/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

/* Nothing is impossible; impossible itself says "I'm possible" */

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define dbg(x,y) cerr << x << " " << y << endl;
#define db(x) cerr << x << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

#define N 20007

int test;

int n,k;

int t[N]; // 1001 - koncowy

queue<int>q;

void solve()
{
       cin >> k >> n;

       FOR(i,1,n)
       {
              int x;
              cin >> x;

              if (!t[x-k+1001])
                     q.push(x-k+1001);

              t[x-k+1001]=1;
       }

       while(!q.empty())
       {
              int v=q.front();
              q.pop();

              FOR(i,1,2001)
              {
                     if ( t[i]>0 && t[v+i-1001]==0 && v+i-1001>0 )
                     {
                            t[v+i-1001]=t[i]+t[v];
                            q.push(v+i-1001);
                     }
              }

              if (t[1001]>0)
              {
                     cout << t[1001];
                     return;
              }
       }

       cout << -1;

       return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
         solve();
    }

  return 0;
}


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

#define MAXN 1000005
#define ii pair<int,int>
#define ff first
#define ss second

int arr[MAXN];
int vis[MAXN];

int main(){
    int n,k,i,j,ans;
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&arr[i]);
        arr[i] -= n;
    }
    deque<ii> dq;
    vis[0+1005] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        dq.pop_front();
        for(i=0;i<k;i++){
            if((abs(arr[i]+it.ff)<=1000 && !vis[1005+arr[i]+it.ff])){
                vis[1005+arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
            if(arr[i]+it.ff==0){
                if(ans == -1) ans = it.ss+1;
                else ans = min(ans,it.ss+1);
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}


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

#define MAXN 1000005
#define ii pair<int,int>
#define ff first
#define ss second

int vis[MAXN];
std::vector<int> arr;
int v[MAXN];

int main(){
    int n,k,i,j,ans,a;
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&a);
        if(v[a]==1) continue;
        v[a] = 1;
        arr.push_back(a-n);
    }
    deque<ii> dq;
    vis[0+1005] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        dq.pop_front();
        for(i=0;i<arr.size();i++){
            if((abs(arr[i]+it.ff)<=1001 && !vis[1005+arr[i]+it.ff])){
                vis[1005+arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
            if(arr[i]+it.ff==0){
                if(ans == -1) ans = it.ss+1;
                else ans = min(ans,it.ss+1);
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}


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

#define MAXN 1000005
#define ii pair<int,int>
#define ff first
#define ss second

int arr[MAXN];
int vis[MAXN];

int main(){
    int n,k,i,j,ans;
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&arr[i]);
        arr[i] -= n;
    }
    deque<ii> dq;
    vis[0+1005] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        dq.pop_front();
        for(i=0;i<k;i++){
            if((abs(arr[i]+it.ff)<=1001 && !vis[1005+arr[i]+it.ff])){
                vis[1005+arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
            if(arr[i]+it.ff==0){
                if(ans == -1) ans = it.ss+1;
                else ans = min(ans,it.ss+1);
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}


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

#define MAXN 1000005
#define ii pair<int,int>
#define ff first
#define ss second

int main(){
    int n,k,i,j,ans;
    int arr[MAXN];
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&arr[i]);
        arr[i] -= n;
    }
    deque<ii> dq;
    map<int,int> vis;
    vis[0] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        // printf("%d %d\n",it.ff,it.ss);
        if(it.ff==0 && it.ss!=0) {
            ans = it.ss;
            break;
        }
        if(it.ss==1005) break;
        dq.pop_front();
        for(i=0;i<k;i++){
            if((abs(arr[i]+it.ff)<=1000 && !vis[arr[i]+it.ff]) || (arr[i]+it.ff == 0)){
                vis[arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}


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

#define MAXN 1005
#define ii pair<int,int>
#define ff first
#define ss second

int main(){
    int n,k,i,j,ans;
    int arr[MAXN];
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&arr[i]);
        arr[i] -= n;
    }
    deque<ii> dq;
    map<int,int> vis;
    vis[0] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        // printf("%d %d\n",it.ff,it.ss);
        if(it.ff==0 && it.ss!=0) {
            ans = it.ss;
            break;
        }
        if(it.ss==1005) break;
        dq.pop_front();
        for(i=0;i<k;i++){
            if((abs(arr[i]+it.ff)<=1000 && !vis[arr[i]+it.ff]) || (arr[i]+it.ff == 0)){
                vis[arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}


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

#define MAXN 1000005
#define ii pair<int,int>
#define ff first
#define ss second

int arr[MAXN];
map<int,int> vis;

int main(){
    int n,k,i,j,ans;
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&arr[i]);
        arr[i] -= n;
    }
    deque<ii> dq;
    vis[0] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        dq.pop_front();
        for(i=0;i<k;i++){
            if((abs(arr[i]+it.ff)<=1001 && !vis[arr[i]+it.ff])){
                vis[arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
            if(arr[i]+it.ff==0 && ans == -1)
                    ans = it.ss+1;
        }
    }
    printf("%d\n",ans);

    return 0;
}


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

#define MAXN 1000005
#define ii pair<int,int>
#define ff first
#define ss second

int arr[MAXN];
map<int,int> vis;

int main(){
    int n,k,i,j,ans;
    scanf("%d %d",&n,&k);
    for(i=0;i<k;i++){
        scanf("%d",&arr[i]);
        arr[i] -= n;
    }
    deque<ii> dq;
    vis[0] = 1;
    ans = -1;
    dq.push_back(ii(0,0));
    while(!dq.empty()){
        ii it = dq.front();
        dq.pop_front();
        for(i=0;i<k;i++){
            if((abs(arr[i]+it.ff)<=1001 && !vis[arr[i]+it.ff])){
                vis[arr[i]+it.ff] = 1;
                dq.push_back(ii(arr[i]+it.ff,it.ss+1));
            }
            if(arr[i]+it.ff==0){
                if(ans == -1) ans = it.ss+1;
                else ans = min(ans,it.ss+1);
            }
        }
    }
    printf("%d\n",ans);

    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define pp pair<int,int>
int n,k,kk;
int a[1000001];
bool fr[5000];
const int offset = 1002;
queue <pp> q;
void bfs() {
    q.push(pp(0,1));
    memset(fr, true, sizeof fr);
    while (!q.empty()) {
        pp u = q.front();
        q.pop();
        for (int i = 0; i<k; i++) {
            int v = u.first + a[i];
            if ((v<=-1001)||(v>=1001)) continue;
            if (!fr[v+ offset]) continue;
            if (v == 0) {
                cout<<u.second;
                return;
            }
            q.push(pp(v,u.second + 1));
            fr[v + offset] = false;
        }
    }
    cout<<-1;
}
bool check() {
    bool ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] >= 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] <= 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    return false;
}
bool aa[1001];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>kk;
    int x;
    memset(aa, false, sizeof aa);
    for (int i = 0; i<kk; i++) {
        cin>>x;
        aa[x] = true;
    }
    k = 0;
    for (int i = 0; i<=1000; i++) {
        if (aa[i]) {
            a[k] = i - n;
            k++;
        }
    }
    if (check()) {
        cout<< -1;
        return 0;
    }
    bfs();
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define pp pair<int,int>
int n,k;
int a[1000001];
bool fr[5000];
const int offset = 1002;
queue <pp> q;
void bfs() {
    q.push(pp(0,1));
    memset(fr, true, sizeof fr);
    while (!q.empty()) {
        pp u = q.front();
        q.pop();
        for (int i = 0; i<k; i++) {
            int v = u.first + a[i];
            if ((v<=-1001)||(v>=1001)) continue;
            if (!fr[v+ offset]) continue;
            if (v == 0) {
                cout<<u.second;
                return;
            }
            q.push(pp(v,u.second + 1));
            fr[v + offset] = false;
        }
    }
    cout<<-1;
}
bool check() {
    bool ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] >= 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] <= 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for (int i = 0; i<k; i++) {
        cin>>a[i];
        a[i] = a[i] - n;
    }
    if (check()) {
        cout<< -1;
        return 0;
    }
    bfs();
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define pp pair<int,int>
int n,k;
int a[2000];
queue <pp> q;
void bfs() {
    q.push(pp(0,1));
    while (true) {
        pp u = q.front();
        q.pop();
        for (int i = 0; i<k; i++) {
            if (u.first + a[i] == 0) {
                cout<<u.second;
                return;
            }
            q.push(pp(u.first + a[i],u.second + 1));
        }
    }
}
bool check() {
    bool ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] > 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] < 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for (int i = 0; i<k; i++) {
        cin>>a[i];
        a[i] = a[i] - n;
    }
    if (check()) {
        cout<< -1;
        return 0;
    }
    bfs();
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define pp pair<int,int>
int n,k;
int a[2000];
bool fr[5000];
const int offset = 1002;
queue <pp> q;
void bfs() {
    q.push(pp(0,1));
    memset(fr, true, sizeof fr);
    while (!q.empty()) {
        pp u = q.front();
        q.pop();
        for (int i = 0; i<k; i++) {
            int v = u.first + a[i];
            if ((v<=-1001)||(v>=1001)) continue;
            if (!fr[v+ offset]) continue;
            if (v == 0) {
                cout<<u.second;
                return;
            }
            q.push(pp(v,u.second + 1));
            fr[v + offset] = false;
        }
    }
}
bool check() {
    bool ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] > 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] < 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for (int i = 0; i<k; i++) {
        cin>>a[i];
        a[i] = a[i] - n;
    }
    if (check()) {
        cout<< -1;
        return 0;
    }
    bfs();
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

#define pp pair<int,int>
int n,k;
int a[2000];
bool fr[5000];
const int offset = 1002;
queue <pp> q;
void bfs() {
    q.push(pp(0,1));
    memset(fr, true, sizeof fr);
    while (!q.empty()) {
        pp u = q.front();
        q.pop();
        for (int i = 0; i<k; i++) {
            int v = u.first + a[i];
            if ((v<=-1001)||(v>=1001)) continue;
            if (!fr[v+ offset]) continue;
            if (v == 0) {
                cout<<u.second;
                return;
            }
            q.push(pp(v,u.second + 1));
            fr[v + offset] = false;
        }
    }
    cout<<-1;
}
bool check() {
    bool ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] >= 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    ok = true;
    for (int i = 0; i<k; i++) {
        if (a[i] <= 0) {
            ok = false;
            break;
        }
    }
    if (ok) return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for (int i = 0; i<k; i++) {
        cin>>a[i];
        a[i] = a[i] - n;
    }
    if (check()) {
        cout<< -1;
        return 0;
    }
    bfs();
}


    #include <stdio.h>
    int main()
    {int T,m;
    scanf("%d",&T);
    for(m=0;m<T;m++){
    int N,k,i,j,l,min,max,price;
    scanf("%d%d",&N,&k);
    int A[N];
    for(i=0;i<N;i++)
    {
        scanf("%d",&A[i]);
    }
    min=A[0];
    for(j=1;j<N;j++)
    {
        if(A[j]<min)
        {
            min=A[j];
        }
    }
    max=A[N-1];
    for(l=(N-1);l>=0;l--)
    {
        if(A[l]>max)
        {
            max=A[l];
        }
    }
    //now i have min and max
    price=min+k;
     if(price>=max-k)
    {
        printf("%d\n",price);
    }
    else printf("-1\n");
    }

    }


#include <stdio.h>
int main()
{int T,m;
scanf("%d",&T);
for(m=0;m<T;m++){
int N,k,i,j,l,min,max,price;
scanf("%d%d",&N,&k);
int A[N];
for(i=0;i<N;i++)
{
    scanf("%d",&A[i]);
}
min=A[0];
for(j=1;j<N;j++)
{
    if(A[j]<min)
    {
        min=A[j];
    }
}
max=A[N-1];
for(l=(N-1);l>=0;l--)
{
    if(A[l]>max)
    {
        max=A[l];
    }
}
//now i have min and max
price=min+k;
if(max-price > 0 && max-price<=k)
{
    printf("%d\n",price);
}
else if(price-max >0 && price-max<=k)
{
    printf("%d\n",price);
}
else printf("-1\n");
}

}


#include<stdio.h>
int main()
{ int t,n,k,i,max=0,a,min=100000001;
	scanf("%d",&t);
	while(t!=0) {max=0;min=100000001;
		scanf("%d%d",&n,&k);
		for(i=0; i<n; i++) {
			scanf("%d",&a);
			if(a>max) max=a;
			if(a<min) min=a;
		}
		if(min+k>=max-k) printf("%d\n",min+k); else printf("-1\n");
	t--;}
}


#include<stdio.h>

int main()
{
    int t,m;
    scanf("%d",&t);
    m=t-1;
    int arr[t];
    while(t--)
    {
    int n,k,max=0,min=100000000,num,p,d;
    scanf("%d%d",&n,&k);
    int ar[n];
    for(int i=0;i<n;i++)
    {
    scanf("%d",&ar[i]);
    if(ar[i]>max)
    max=ar[i];
    if(ar[i]<min)
    min=ar[i];
    }
    p=max+k;
    while(p>=min)
    {
    num=0;
    for(int i=0;i<n;i++)
    {
    if(ar[i]>p)
    d=ar[i]-p;
    else
    d=p-ar[i];
    if(d>k)
    {
    p--;
    break;
    }
    else
    num++;
    }
    if(num==n)
    break;
    }//wp
    if(num==n)
    arr[t]=p;
    else
    arr[t]=-1;
    }
    for(int i=m;i>=0;i--)
    printf("%d\n",arr[i]);
    return 0;
}


#include<stdio.h>

int main()
{
    int t,m;
    scanf("%d",&t);
    m=t-1;
    int arr[t];
    while(t--)
    {
    int n,k,max=0,min=100000000,num,p,d;
    scanf("%d%d",&n,&k);
    int ar[n];
    for(int i=0;i<n;i++)
    {
    scanf("%d",&ar[i]);
    if(ar[i]>max)
    max=ar[i];
    if(ar[i]<min)
    min=ar[i];
    }
    p=max+k;
    while(p>=min+k)
    {
    num=0;
    for(int i=0;i<n;i++)
    {
    if(ar[i]>p)
    d=ar[i]-p;
    else
    d=p-ar[i];
    if(d>k)
    {
    p--;
    break;
    }
    else
    num++;
    }
    if(num==n)
    break;
    }//wp
    if(num==n)
    arr[t]=p;
    else
    arr[t]=-1;
    }
    for(int i=m;i>=0;i--)
    printf("%d\n",arr[i]);
    return 0;
}


#include<stdio.h>

int main()
{
    int t, m;
    scanf("%d",&t);
    m=t-1;
    int arr[t];
    while(t--)
    {
    int n,k,min=1e8,max=0;
    scanf("%d%d",&n,&k);
    int ar;
    for(int i=0;i<n;i++)
    {
    scanf("%d",&ar);
    if(ar>max)
    max=ar;
    if(ar<min)
    min=ar;
    }
    if(max-min>(2*k))
    arr[t]=-1;
    else
    arr[t]=min+k;
    }
    for(int i=m;i>=0;i--)
    printf("%d\n",arr[i]);
    return 0;
}


#include <stdio.h>

int q, n, k, a[102];

int main(){
    scanf("%d", &q);
    while (q != 0){
        int i;
        scanf("%d%d", &n, &k);
        for (i = 0; i < n; i++) scanf("%d", &a[i]);
        int min = a[0] + k, max = a[0] - k;
        for (i = 1; i < n; i ++){
            if (a[i] + k < min) min = a[i] + k;
            if (a[i] - k > max) max = a[i] - k;
        }
        if (min < max) printf("-1\n");
        else printf("%d\n", min);
        q --;
    }
}


#include <stdio.h>

int q, n, k, a[102];

int main(){
    scanf("%d", &q);
    while (q != 0){
        int i;
        scanf("%d%d", &n, &k);
        for (i = 0; i < n; i++) scanf("%d", &a[i]);
        int min = 100000000, max = -100000000;
        for (i = 0; i < n; i ++){
            if (a[i] + k < min) min = a[i] + k;
            if (a[i] - k > max) max = a[i] - k;
        }
        if (min < max) printf("-1\n");
        else printf("%d\n", min);
        q --;
    }
}


#include<stdio.h>
#include<math.h>
int main()
{
	long long int T,N,K,i,j,l,k,f,min;
	scanf("%lld",&T);
	for(i=0;i<T;i++)
	{
		f=0;
		min=0;
		scanf("%lld %lld",&N,&K);
		long long int ar[N];
		scanf("%lld",&ar[0]);
		min=ar[0];
	//	printf("min=%lld",min);
		for(j=1;j<N;j++)
		{
			scanf("%lld",&ar[j]);
			if(min>ar[j])
			min=ar[j];
		}
		min=min+K;
	//	printf("value %lld\n",min);
		for(k=0;k<N;k++)
		{
			if(abs(ar[k]-min)>K)
			{
				f=1;
				break;
			}
		}
		if(f==1)
		printf("-1\n");
		else
		printf("%lld\n",min);

	}
}


#include<stdio.h>
#include<math.h>
int main()
{
	long long int T,N,K,i,j,l,k,f,min,z;
	scanf("%lld",&T);
	for(i=0;i<T;i++)
	{
		f=0;
		min=0;
		scanf("%lld %lld",&N,&K);
		long long int ar[N];
		scanf("%lld",&ar[0]);
		min=ar[0];
	//	printf("min=%lld",min);
		for(j=1;j<N;j++)
		{
			scanf("%lld",&ar[j]);
			if(min>ar[j])
			min=ar[j];
		}
		min=min+K;
	//	printf("value %lld\n",min);
		for(k=0;k<N;k++)
		{
			if(abs(ar[k]-min)>K)
			{
				f=1;
				break;
			}
		}
		if(f==1)
		printf("-1\n");
		else
	   z=printf("%lld\n",min);
	   printf("%lld\n",z);

	}
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef long long ll;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define max(l,r) ((l)>(r)?l:r)
#define min(l,r) ((l)<(r)?l:r)
#define swap(l,r) {ll tp=l;l=r;r=tp;}
const ll MOD = 1000000007;
const int INF = (1<<30);    // > 10^9
const ll LINF = (1LL<<60);  // > 10^18
int upcmpll(const void *a, const void *b){return(*(ll*)a==*(ll*)b?0:(*(ll*)a>*(ll*)b?1:-1));}
void sort_u(ll*a, ll n){qsort(a,n,sizeof(ll),upcmpll);}

char s[202020];
ll a[202020];
int n,t,k;

ll armax(ll *a, ll n){
    ll tmx = a[0];
    for (ll i = 1; i < n; i++)
        if(a[i] > tmx) tmx = a[i];
    return tmx;
}
ll armin(ll *a, ll n){
    ll tmn = a[0];
    for (ll i = 1; i < n; i++)
        if(a[i] < tmn) tmn = a[i];
    return tmn;
}

ll solve(){
    ll ans = 0;
    ll mx = armax(a,n), mn = armin(a,n);
    if(mx-mn > 2*k){
        ans = -1;
    }else{
        ans = mn+k;
    }
    return ans;
}

//for q-testcase
int main(){
    scanf("%d", &t);
    rep(tes,t){
        scanf("%d%d", &n,&k);
        rep(i, n) scanf("%lld", a+i);

        printf("%lld\n", solve());
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef long long ll;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define max(l,r) ((l)>(r)?l:r)
#define min(l,r) ((l)<(r)?l:r)
#define swap(l,r) {ll tp=l;l=r;r=tp;}
const ll MOD = 1000000007;
const int INF = (1<<30);    // > 10^9
const ll LINF = (1LL<<60);  // > 10^18
ll gcd(ll a,ll b){if(b==0) return llabs(a); return gcd(b,a%b);}
ll lcm(ll a,ll b){if(a==b&&a==0) return 0; return a/gcd(a,b)*b;}
int upcmpll(const void *a, const void *b){return(*(ll*)a==*(ll*)b?0:(*(ll*)a>*(ll*)b?1:-1));}
void sort_u(ll*a, int n){qsort(a,n,sizeof(ll),upcmpll);}

char s[202020];
ll a[202020];
int n,t,k;

ll solve(){
    ll ans = 0;
    sort_u(a,n);
    ans = a[0] + k;
    rep(i,n){
        if(a[i] > ans && a[i] - ans > k){
            ans = -1;
            break;
        }
    }

    return ans;
}

//for q-testcase
int main(){
    scanf("%d", &t);
    rep(tes,t){
        scanf("%d%d", &n,&k);
        rep(i, n) scanf("%lld", a+i);

        printf("%lld\n", solve());
    }
}


#include <stdio.h>

void solve(){
    int n, k;
    scanf("%d%d", &n, &k);
    int mx = 0, mn = 1e8 + 1;
    for(int i = 0; i < n; i++){
        int a;
        scanf("%d", &a);
        if(mx < a) mx = a;
        if(mn > a) mn = a;
    }
    if(mn + 2 * k < mx) printf("-1\n");
    else printf("%d\n", mn + k);
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        solve();
    }
}


#include <stdio.h>

void solve(){
    int n, k;
    scanf("%d%d", &n, &k);
    int mx = 0, mn = 1e8 + 1;
    for(int i = 0; i < n; i++){
        int a;
        scanf("%d", &a);
        if(mx < a) mx = a;
        if(mn > a) mn = a;
    }
    if(mn + k < mx) printf("-1\n");
    else printf("%d\n", mn + k);
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        solve();
    }
}


#include <stdio.h>

int a[200];

void swap(int *a, int *b){
    int tmp;
    tmp = *a; *a = *b; *b = tmp;
}

void bubble_sort(int *a, int sz){
    for(int i = 0; i < sz - 1; i++){
        for(int j = i + 1; j < sz; j++){
            if(a[i] > a[j]) swap(&a[i], &a[j]);
        }
    }
}

void solve(){
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    bubble_sort(a, n);
    if(a[n - 1] - a[0] > 2 * k) printf("-1\n");
    else printf("%d\n", a[0] + k);
}

int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        solve();
    }
}


#include <stdio.h>

int a[150];

void solve(){
    int n, k;
    int mx = 0, mn = 1e8 + 1;
    scanf("%d%d", &n, &k);
    for(int i = 0; i < n; i++){
        int x;
        scanf("%d", &x);
        if(mx < x) mx = x;
        if(mn > x) mn = x;
    }
    if(mx - mn > 2 * k) printf("-1\n");
    else printf("%d\n", mn + k);
}

int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        solve();
    }
}


#include <stdio.h>

int solve () {
	int min, max, x, n, k, i = 0;
	scanf("%d %d", &n, &k);
	for (; i < n; i++) {
		scanf("%d", &x);
		if (i == 0) {
			min = x;
			max = x;
		}
		if (x < min) min = x;
		if (x > max) max = x;
	}
	if (min + k < max - k) return -1;
	return min + k;
}
int main () {
	int q, i = 0;
	scanf("%d", &q);
	for (; i < q; i++) {
		printf("%d\n", solve());
	}
}


#include <stdio.h>

int abs(int x);

int main(){
    int queryNum;
    scanf("%d", &queryNum);
    for (int i = 0; i < queryNum; i++){
        int max = 0, min = 100000000, n, k, b = -1, product[100];
        scanf("%d %d", &n, &k);
        for (int i = 0; i < n; i++){
            scanf("%d", &product[i]);
            if (product[i] > max)
                max = product[i];
            if (product[i] < min)
                min = product[i];
        }

        for (int i = max - k; i <= min + k; i++){
            int check = 0;
            for (int j = 0; j < n; j++){
                int val = product[j] - i;
                if (abs(val) > k){
                    check = 1;
                    break;
                }
            }
            if (check == 0)
                b = i;
        }

        printf("%d\n", b);
    }
}

int abs(int x){
    if (x < 0)
        return x * -1;
    else
        return x;
}


#include <stdio.h>

int abs(int x);

int main(){
    int queryNum;
    scanf("%d", &queryNum);
    for (int i = 0; i < queryNum; i++){
        int max = 0, min = 100000000, n, k, a;
        scanf("%d %d", &n, &k);
        for (int i = 0; i < n; i++){
            scanf("%d", &a);
            if (a > max)
                max = a;
            if (a < min)
                min = a;
        }

        if (max - k > min + k)
            printf("-1\n");
        else
            printf("%d\n", min + k);
    }
}

int abs(int x){
    if (x < 0)
        return x * -1;
    else
        return x;
}


#include <stdio.h>

int abs(int x);

int main(){
    int product[100], queryNum;
    scanf("%d", &queryNum);
    for (int i = 0; i < queryNum; i++){
        int max = 0, min = 100000000, n, k, b = -1;
        scanf("%d %d", &n, &k);
        for (int i = 0; i < n; i++){
            scanf("%d", &product[i]);
            if (product[i] > max)
                max = product[i];
            if (product[i] < min)
                min = product[i];
        }

        for (int i = min - k; i <= max + k; i++){
            int check = 0;
            for (int j = 0; j < n; j++){
                int val = product[j] - i;
                if (abs(val) > k){
                    check = 1;
                    break;
                }
            }
            if (check == 0)
                b = i;
        }
        if (b <= 0)
            b = -1;

        printf("%d\n", b);
    }
}

int abs(int x){
    if (x < 0)
        return x * -1;
    else
        return x;
}


#include <stdio.h>

int main()
{
    int t, n, k, i, maxx, minn;
    int a[105];
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &k);
        for(i=0;i<n;i++)
        {
            scanf("%d", &a[i]);
            if(i==0||a[i]>maxx) maxx = a[i];
            if(i==0||a[i]<minn) minn = a[i];
        }
        if(minn + k < maxx - k) printf("%d\n", -1);
        else printf("%d\n", minn+k);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int valid(int ar[],int n,int k,int mid){
    int i;
    int flag = 0;

    for(i=0;i<n;i++){
        if(mid>=ar[i]){
            if((k+ar[i])<mid){
                flag = 1;
                break;
            }
        }
        else if(mid<ar[i]){
            if((ar[i]-k)>mid){
                flag = 1;
                break;
            }
        }
    }

    if(flag){
        return 0;
    }
    else{
        return 1;
    }

}

int main(){

    int j,q;
    scanf("%d",&q);
    for(j=0;j<q;j++){

        int i,n,k;
        scanf("%d %d",&n,&k);

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

        int mi = ar[0];
        int ma = ar[0];

        for(i=1;i<n;i++){
            if(ar[i]>ma){
                ma = ar[i];
            }
        }

        for(i=1;i<n;i++){
            if(ar[i]<mi){
                mi = ar[i];
            }
        }

        int lo = mi;
        int hi = ma + k;
        int mid,ans=-1;

        while(lo<=hi){
            mid = (lo+hi)/2;
            if(valid(ar,n,k,mid)){
                ans = mid;
                lo = mid+1;
            }
            else{
                hi = mid-1;
            }
        }

        printf("%d\n",ans);

    }
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int valid(int ar[],int n,int k,int mid){
    int i;
    int flag = 0;

    for(i=0;i<n;i++){
        if(mid>=ar[i]){
            if((k+ar[i])<mid){
                flag = 1;
                break;
            }
        }
        else if(mid<ar[i]){
            if((ar[i]-k)>mid){
                flag = 0;
                break;
            }
        }
    }

    if(flag){
        return 0;
    }
    else{
        return 1;
    }

}

int main(){

    int j,q;
    scanf("%d",&q);
    for(j=0;j<q;j++){

        int i,n,k;
        scanf("%d %d",&n,&k);

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

        int mi = ar[0];
        int ma = ar[0];

        for(i=1;i<n;i++){
            if(ar[i]>ma){
                ma = ar[i];
            }
        }

        for(i=1;i<n;i++){
            if(ar[i]<mi){
                mi = ar[i];
            }
        }

        int lo = mi;
        int hi = ma + k;
        int mid,ans=-1;

        while(lo<=hi){
            mid = (lo+hi)/2;
            if(valid(ar,n,k,mid)){
                ans = mid;
                lo = mid+1;
            }
            else{
                hi = mid-1;
            }
        }

        if((ma-mi)>2*k){
            printf("-1\n");
        }
        else{
            printf("%d\n",ans);
        }

    }
return 0;
}


#include<stdio.h>
int main()
{
    long long int t,i,n,k,max,min,j,a,b,s;
    scanf("%lld",&t);
    for(i=0;i<t;i++)
    {
        scanf("%lld %lld",&n,&k);
        max=0;
        min=100000001;
        for(j=0;j<n;j++)
        {
            scanf("%lld",&a);
            if(a>max)
            {
                max=a;
            }
            if(a<min)
            {
                min=a;
            }
        }
        b=min+k;
        s=b-max;
        if(s<0)
        {
            s=-s;
        }
        if(s<=k)
        {
            printf("%lld\n",b);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main()
{
    int q,i,min,max;
    scanf("%d",&q);
    int answ[q],n,k;
    for(int j=0;j<q;j++){
        scanf("%d%d",&n,&k);
        int a[n];
        scanf("%d",&a[0]);
        min=max=a[0];
        for(i=1;i<n;i++){
            scanf("%d",&a[i]);
            if(min>a[i])min=a[i];
            if(max<a[i])max=a[i];
        }
        if((max-min)>2*k)answ[j]=-1;
        else answ[j]=min+k;
    }
    for(i=0;i<q;i++)printf("%d\n",answ[i]);
    return 0;
}


#include<stdio.h>
#include<malloc.h>
void swap(int*x,int*y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void bubblesort(int arr[],int num)
{
    int i,j;
    for(i=0;i<num-1;i++)
    {
        for(j=0;j<num-1-i;j++)
        {
            if(arr[j]>arr[j+1])
            swap(&arr[j],&arr[j+1]);
        }
    }
    /*for(i=0;i<num;i++)
    printf("%d ",arr[i]);
    printf("\n");*/
}
int main()
{
    int n,k,cases,i,j;
    int *arr;
    scanf("%d",&cases);
    for(i=0;i<cases;i++)
    {
        scanf("%d%d",&n,&k);
        arr=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++)
            scanf("%d",&arr[j]);
        bubblesort(arr,n);
        //printf("k=%d\t%d\t%d\n",k,arr[0]+2*k,arr[n-1]);
        if((arr[0]+2*k)>=arr[n-1])
        {
              //if((arr[0]+2*k)>arr[n-1])
              printf("%d\n",arr[0]+k);
              //else
              //printf("%d\n",arr[n-1]-k);
        }
        else
        printf("-1\n");
    }
}


#include<stdio.h>
#include<malloc.h>
void swap(int*x,int*y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void bubblesort(int arr[],int num)
{
    int i,j;
    for(i=0;i<num-1;i++)
    {
        for(j=0;j<num-1-i;j++)
        {
            if(arr[j]>arr[j+1])
            swap(&arr[j],&arr[j+1]);
        }
    }
    /*for(i=0;i<num;i++)
    printf("%d ",arr[i]);
    printf("\n");*/
}
int main()
{
    int n,k,cases,i,j;
    int *arr;
    scanf("%d",&cases);
    for(i=0;i<cases;i++)
    {
        scanf("%d%d",&n,&k);
        arr=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++)
            scanf("%d",&arr[j]);
        bubblesort(arr,n);
        //printf("k=%d\t%d\t%d\n",k,arr[0]+2*k,arr[n-1]);
        if((arr[0]+2*k)>=arr[n-1])
        {
              if((arr[0]+2*k)>arr[n-1])
              printf("%d\n",arr[0]+k);
              else
              printf("%d\n",arr[n-1]-k);
        }
        else
        printf("-1\n");
    }
}


#include<stdio.h>
#include<malloc.h>
void swap(int*x,int*y)
{
    int temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void bubblesort(int arr[],int num)
{
    int i,j;
    for(i=0;i<num-1;i++)
    {
        for(j=0;j<num-1-i;j++)
        {
            if(arr[j]>arr[j+1])
            swap(&arr[j],&arr[j+1]);
        }
    }
    /*for(i=0;i<num;i++)
    printf("%d ",arr[i]);
    printf("\n");*/
}
int main()
{
    int n,k,cases,i,j;
    int *arr;
    scanf("%d",&cases);
    for(i=0;i<cases;i++)
    {
        scanf("%d%d",&n,&k);
        arr=(int*)malloc(n*sizeof(int));
        for(j=0;j<n;j++)
            scanf("%d",&arr[j]);
        bubblesort(arr,n);
        //printf("k=%d\t%d\t%d\n",k,arr[0]+2*k,arr[n-1]);
        if((arr[0]+2*k)>=arr[n-1])
        {
              if((arr[0]+2*k)>arr[n-1])
              printf("%d\n",arr[0]+k);
              //else
              //printf("%d\n",arr[n-1]-k);
        }
        else
        printf("-1\n");
    }
}


#include<stdio.h>

int main()
{ int t,i;
scanf("%d",&t);
for(i=0;i<t;i++)
{ long int n,k;
scanf("%ld %ld",&n,&k);
long int a[n],j,l;
for(j=0;j<n;j++)
{ scanf("%ld",&a[j]);
 }
long int min=a[0],max=a[1];
for(l=0;l<n;l++)
{ if(a[l]>max)
   max=a[l];
  else
  max=max+1-1;
 if(a[l]<min)
  min=a[l];
else
min=min+1-1;
}
if(min+k>=max-k)
printf("%ld\n",min+k);
else
printf("-1\n");

}
    return 0;
}


#include<stdio.h>

int main()
{ int t,i;
scanf("%d",&t);
for(i=0;i<t;i++)
{ long int n,k;
scanf("%ld %ld",&n,&k);
long int a[n],j,l;
for(j=0;j<n;j++)
{ scanf("%ld",&a[j]);
 }
long int min=a[0],max=a[1];
for(l=0;l<n;l++)
{ if(a[l]>max)
   max=a[l];
  else
  max=max+1-1;
 if(a[l]<min)
  min=a[l];
else
min=min+1-1;
}
if(n==1)
printf("%ld\n",a[0]+k);
else
{
if(min+k>=max-k)
printf("%ld\n",min+k);
else
printf("-1\n");}

}
    return 0;
}


#include<stdio.h>

int main()
{ int t,i;
scanf("%d",&t);
for(i=0;i<t;i++)
{ long int n,k;
scanf("%ld %ld",&n,&k);
long int a[n],j,l;
for(j=0;j<n;j++)
{ scanf("%ld",&a[j]);
 }
long int min=a[0],max=a[1];
for(l=0;l<n;l++)
{ if(a[l]>max)
   max=a[l];
  else
  max=max+1-1;
 if(a[l]<min)
  min=a[l];
else
min=min+1-1;
}
if(min+k>=max-k)
printf("%ld\n",min+k);
else
printf("-1");

}
    return 0;
}