#include<cstdio>
#include<vector>
#include<algorithm>
typedef long long ll;
ll m,a[45];
std::vector<ll> f,g;
inline ll read() {
	ll x=0,f=1;register char s=getchar();
	while(s>'9'||s<'0') {if(s=='-') f=-1;s=getchar();}
	while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();}
	return x*f;
}
inline ll max(const ll &x,const ll &y) {return x>y? x:y;}
void dfs(int L,int R,ll sum,std::vector<ll> &x) {
	if(R==L) {x.push_back(sum);return;}
	dfs(L+1,R,sum,x);
	if(sum+a[L+1]<=m) dfs(L+1,R,sum+a[L+1],x);
}
int main() {
	int n=read(),mid=n>>1;
	ll res=0;m=read();
	for(register int i=1;i<=n;++i) a[i]=read();
	dfs(0,mid,0,f);dfs(mid,n,0,g);
	std::sort(f.begin(),f.end());
	std::sort(g.begin(),g.end());
	for(register int i=0;i<f.size();++i) {
		int pos=std::upper_bound(g.begin(),g.end(),m-f[i])-g.begin()-1;
		if(pos<0||g[pos]+f[i]>m) continue;
		res=max(res,f[i]+g[pos]);
	}//res+=std::upper_bound(g.begin(),g.end(),m-f[i])-g.begin();
	printf("%lld\n",res);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	string X;
	ll M;
	cin >> X >> M;
	int n = X.size();
	vector<ll> b(n);
	ll m = 0;
	for (int i = 0; i < n; i++) {
		b[i] = X[i] - '0';
		m = max(m, b[i]);
	}
	auto test = [&](ll N, ll k) {     //转 k 进制 
		vector<ll> a(0);
		while (N > 0) {
			a.push_back((N % k));
			N /= k;
		}
		reverse(a.begin(), a.end());  //正过来 
		if (a.size() < X.size())
			return false;
		else if (a.size() > X.size())
			return true;
		else {
			int i = 0;
			while (i<n&& a[i] == b[i]) {
				++i;
			}
			if (i == n)
				return true;
			else if (a[i] < b[i])
				return false;
			else
				return true;
		}
	};
	if (n == 1)
		cout << (b[0] <= M) ? '1': '0' ;
	else if (!(test(M, m + 1)))
		cout << 0 << endl;
	else {
		ll ok = m + 1; 
		ll ng = M + 1; 
		ll mid;
		while (ng - ok > 1) {
			mid = (ok + ng) / 2;
			if (test(M, mid))
				ok = mid;
			else
				ng = mid;
		}
		cout << ok - m << endl;
		return 0;
	}
}