#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; using namespace atcoder; typedef long long ll; typedef pair P; int n; ll k; int c[10]; int n1,n2; const ll mask=(1ll<<44)-1; map> mp; //map, int> mp; int ans; void dfs(ll v, ll r, int t){ if(t==n1){ mp[v].push_back(r); return; } ll r1=(r*10+1)&mask; for(int i=1; i<=9; i++){ ll d=((v>>((i-1)<<2))&15); if(c[i]>d){ v+=(1ll<<((i-1)<<2)); dfs(v, r1, t+1); v-=(1ll<<((i-1)<<2)); } r1++; if(r1>=k) r1-=k; } } using lll=__int128_t; ll q; ll c0; void dfs2(ll v, ll r, int t){ if(t==n2){ ll x=(k-((lll)q*r&mask)); if(x>=k) x-=k; ll w=c0-v; auto itr=mp.find(w); if(itr!=mp.end()){ for(auto y:itr->second){ ans=max(ans, __builtin_ffsll(x^y)); } } return; } ll r1=(r*10+1)&mask; for(int i=1; i<=9; i++){ ll d=((v>>((i-1)<<2))&15); if(c[i]>d){ v+=(1ll<<((i-1)<<2)); dfs2(v, r1, t+1); v-=(1ll<<((i-1)<<2)); } r1++; if(r1>=k) r1-=k; } } int main() { cin>>n; k=(1ll<<44); for(int i=1; i<=9; i++){ cin>>c[i]; c0^=((ll)c[i]<<((i-1)*4)); } n1=min(7, n); dfs(0, 0, 0); if(n1==n){ for(auto x:mp[c0]){ ans=max(ans, __builtin_ffsll(x)); } cout<