#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #include <time.h> #include <stack> #include <array> #include <list> #include <atcoder/all> #define popcount __builtin_popcount using namespace std; using namespace atcoder; typedef long long ll; typedef pair<int, int> P; int n; ll k; int c[10]; int n1,n2; const ll mask=(1ll<<44)-1; map<ll, vector<ll>> mp; //map<pair<int, ll>, 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(6, n); dfs(0, 0, 0); if(n1==n){ for(auto x:mp[c0]){ ans=max(ans, __builtin_ffsll(x)); } cout<<ans-1<<endl; return 0; } n2=n-n1; q=1; for(int i=0; i<n1; i++) q=q*10%k; dfs2(0, 0, 0); cout<<ans-1<<endl; return 0; }