#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; typedef complex Point; const ld eps = 1e-11; const ld pi = acos(-1.0); typedef pair LP; typedef pair LDP; typedef unsigned long long ul; int b; string minu(string x, string y) { int len = x.length(); int len2 = y.length(); string res; rep1(i, len) { int t = x[len - i] - '0'; if (len2 - i >= 0) { t -= y[len2 - i] - '0'; } while (t < 0) { t += b; x[len - i - 1]--; } res.push_back('0' + t); } reverse(res.begin(), res.end()); while (!res.empty() && res[0] == '0') { res.erase(res.begin()); } return res; } string make(int n) { string res; if (n == 0)return "0"; else if (n == 1) { res.push_back('0' + b - 1); return res; } else { int c = n - 1; while (c) { res.push_back('0' + (c%b)); c /= b; } reverse(res.begin(), res.end()); rep(i, n - 1) { res.push_back('0' + b - 2); } res.push_back('0' + b - 1); } return res; } bool great(string x, string y) { if (x.length() != y.length()) { return x.length() > y.length(); } return x >= y; } pair q(string x, int d) { int len = x.length(); string res; int c = 0; rep(i, len) { c += x[i] - '0'; res.push_back('0' + (c / d)); c %= d; c *= b; } while (!res.empty() && res[0] == '0') { res.erase(res.begin()); } return { res,c / b }; } string make_base(int n) { string res; res.push_back('1'); rep(i, n - 1) { res.push_back('0'); } return res; } string add(string a, string y) { reverse(a.begin(), a.end()); reverse(y.begin(), y.end()); int len = max(a.length(), y.length()); int rest = 0; string res; rep(i, len + 1) { if (i < (int)a.length()) { rest += a[i] - '0'; } if (i < (int)y.length()) { rest += y[i] - '0'; } res.push_back('0' + (rest % b)); rest /= b; } while (res.length() && res[res.length() - 1] == '0')res.erase(res.end() - 1); reverse(res.begin(), res.end()); return res; } int main() { cin >> b; string s; cin >> s; int le = 0, ri = 100001; while (ri - le > 1) { int mid = (ri + le) >> 1; string nex = make(mid); if (great(nex, s))ri = mid; else le = mid; } //cout << ri << endl; //cout << s << endl; //cout << make(ri - 1) << endl; s = minu(s, make(ri - 1)); //cout << s << endl; pair ans = q(s, ri); //cout << ans.first << endl; ans.first = add(ans.first, make_base(ri)); //cout << ans.first << endl; //cout << ans.second << endl; if (ans.second > 0) { cout << ans.first[ans.second-1] << endl; } else { ans.first = minu(ans.first, "1"); cout << ans.first[ri - 1] << endl; } return 0; }