#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>

#include <fstream>


using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

ll f(char c){
    ll k=c-'0';
    if(0<=k && k<=9){
        return k;
    }
    ll l=c-'A';
    return 10+l;
}

int main(){fastio
    string N;cin>>N;
    ll K;cin>>K;
    ll n;n=(ll)N.size();
    ll dp[2][n][17];
    ll bol[n][16];rep(i,0,n-1)rep(j,0,15)bol[i][j]=0;
    rep(i,0,n-1){
        if(i>0){
            rep(j,0,15){
                bol[i][j]=bol[i-1][j];
            }
        }
        bol[i][f(N[i])]=1;
    }
    /*
    rep(i,0,15){
        rep(j,0,n-1){
            cout<<bol[j][i]<<' ';
        }
        cout<<endl;
    }
    cout<<endl;
     */
    
    rep(i,0,n-1){
        rep(j,1,15){
            bol[i][j] = bol[i][j-1]+bol[i][j];
        }
    }
    /*
    rep(j,0,15){
        rep(i,0,n-1){
            cout<<bol[i][j]<<' ';
        }
        cout<<endl;
    }
    cout<<endl;
    */
    
    //bol[i][j]=0~jのうちN[i]までで現れたものの個数
    rep(i,0,n-1){
        rep(j,0,16){
            if(j==bol[i][15]){
                dp[0][i][j]=1;
            }else{
                dp[0][i][j]=0;
            }
            if(i==0){
                if(j==0){
                    dp[1][i][j]=1;
                }else if(j==1){
                    dp[1][i][j]=f(N[i])-1;
                }else{
                    dp[1][i][j]=0;
                }
            }else{
                if(j==0){
                    dp[1][i][j]=1;
                }else{
                    if(f(N[i]) == 0){
                        if(j==1){
                            dp[1][i][j] = mod(dp[1][i-1][j-1]*(16-j)+dp[1][i-1][j]*j);
                        }else{
                            dp[1][i][j] = mod(dp[1][i-1][j-1]*(17-j)+dp[1][i-1][j]*j);
                        }
                        
                    }else{
                        if(j==1){
                            dp[1][i][j] = mod(dp[1][i-1][j-1]*(16-j)+dp[1][i-1][j]*j);
                        }else{
                            dp[1][i][j] = mod(dp[1][i-1][j-1]*(17-j)+dp[1][i-1][j]*j);
                        }
                        
                        dp[1][i][j] += dp[0][i-1][j]*(bol[i-1][f(N[i])-1]);
                        dp[1][i][j] = mod(dp[1][i][j]);
                        dp[1][i][j] += dp[0][i-1][j-1]*(f(N[i])-bol[i-1][f(N[i])-1]);
                        dp[1][i][j] = mod(dp[1][i][j]);
                    }
                }
            }
        }
    }
    ll ans=0;
    ans += dp[0][n-1][K];
    ans = mod(ans);
    ans += dp[1][n-1][K];
    ans = mod(ans);
    cout<<ans<<endl;
    
    /*
    rep(i,0,15){
        rep(j,0,n-1){
            cout<<bol[j][i]<<' ';
        }
        cout<<endl;
    }
    cout<<endl;
    
    rep(k,0,1){
        cout<<"k: "<<k<<endl;
        rep(j,0,15){
            rep(i,0,n-1){
                cout<<dp[k][i][j]<<' ';
            }
            cout<<endl;
        }
        cout<<endl;
    }
    */
    
    return 0;
    
}






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

#define all(x) (x).begin(), (x).end()
#define ll long long
const ll mod = 1e9 + 7;

int ri(){
	int n;
	scanf("%d", &n);
	return n;
}
void solve(){
	int n = ri();
	int a[n];
	for(auto &i : a) i = ri();

	int res = INT_MAX;
	for(int i=0; i < (1 << (n-1)); i++){
		int ored = 0, xored = 0;
		for(int j=0; j<=n; j++){
			if(j < n) ored |= a[j];
			if(j == n || (i >> j & 1)) xored ^= ored, ored = 0;
		}
		res = min(res, xored);
	}

	cout << res << "\n" ;
}

int main(){

	int t = 1;
	// cin >> t;
	while(t--){
		solve();
	}
}