#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class LongLongNim { public:
	int numberOfWins(int maxN, vector <int> moves) 
	{
		int FULL = (1 << *max_element(moves.begin(), moves.end())) - 1;
		int MASK = 0;
		for(int i=0; i<moves.size(); ++i)
			MASK |= 1 << (moves[i]-1);

		vector<int> ss;
		vector<int> visited(FULL+1, -1);
		for(int i=1,s=1; i<=maxN; ++i)
		{
			s = (s<<1 | ((~s&MASK)==MASK)) & FULL;
			if( visited[s] >= 0 )
				return countBits(ss, visited[s], ss.size(), maxN);
			else
			{
				visited[s] = ss.size();
				ss.push_back(s&1);
			}
		}
		return countBits(ss, 0, maxN, maxN);
	}

	int countBits(const vector<int>& b, int lb, int le, int N)
	{
		// Given an infinite seq b[0] ... b[lb-1] (b[lb] ... b[le-1])*,
		// compute \Sigma b[0..N)
		int sum = 0;
		for(int i=0; i<lb; ++i)
			sum += b[i];

		N -= lb;
		int ll = le - lb;
		int Q = N/ll;
		int R = N%ll;
		for(int i=lb; i<le; ++i)
			sum += b[i] * Q;
		for(int i=lb; i<lb+R; ++i)
			sum += b[i];
		return sum;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, LongLongNim().numberOfWins(maxN, moves));}
int main(){

CASE(0)
	int maxN = 20; 
	int moves_[] = {1,2,3};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int _ = 5; 
END
CASE(1)
	int maxN = 999; 
	int moves_[] = {1};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int _ = 499; 
END
CASE(3)
	int maxN = 6543; 
	int moves_[] = {2,4,7,11,20};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int _ = 1637; 
END
CASE(2)
	int maxN = 1000000000; 
	int moves_[] = {1,2};
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int _ = 333333333; 
END
/*
CASE(4)
	int maxN = ; 
	int moves_[] = ;
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int _ = ; 
END
CASE(5)
	int maxN = ; 
	int moves_[] = ;
	  vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
