<filename>codes/CF/CF_552C.cpp

//chtholly and emilia will carry me to cm
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <cassert>
#include <algorithm>
#include <vector>
#include <array>
#include <tuple>

#define ll long long
#define lb long double
#define sz(vec) ((int)(vec.size()))
#define all(x) x.begin(), x.end()

const lb eps = 1e-9;
const ll mod = 1e9 + 7, ll_max = 1e18;
//const ll mod = (1 << (23)) * 119 +1;
const int MX = 2e5 +10, int_max = 0x3f3f3f3f;

using namespace std;

//i will learn from moo.
/* Input */
template<class T> void read(T &x) { cin >> x; }
template<class H, class T> void read(pair<H, T> &p) { cin >> p.first >> p.second; }
template<class T, size_t S> void read(array<T, S> &a) { for (T &i : a) read(i); }
template<class T> void read(vector<T> &v) { for (T &i : v) read(i); }
 
template<class H, class... T> void read(H &h, T &...t) { read(h); read(t...); }
 
/* Output */
template<class H, class T> ostream &operator<<(ostream &o, pair<H, T> &p) { o << p.first << " " << p.second; return o; }
template<class T, size_t S> ostream &operator<<(ostream &o, array<T, S> &a) { string s; for (T i : a) o << s << i, s = " "; return o; }
template<class T> ostream &operator<<(ostream &o, vector<T> &v) { string s; for (T i : v) o << s << i, s = " "; return o; }
 
template<class T> void write(T x) { cout << x; }
template<class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); }
 
void print() { write('\n'); }
template<class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); }
 
/* Misc */
template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); }
ll w, m, we[30];
int work(ll a){
	ll p = 1, b = 0, c = 0;
	while(p <= a){ p *= w; b++; }
	int ans = 0;
	print(w, m, a, p, b, c);
	while(p > 0){
		if(a/p > 1){ ans = 0; break;}
		if(a/p == 1){ a -= p;
		ans += (1ll << (b - c)); c++;
		}
		p /= w;
	}
	return ans;
}

void solve(){

	read(w, m);
	if(w == 2 || w == 3){
		print("YES"); return ;
	}
	ll p = 1;
	while(p <= 1e9) p *= w;
	while(p > 0){
		//if there is a value of 1/-1 in the log_w(p)'th position, this will subtract it properly
		//if there is a 0, the ckmin will not change anything
		ckmin(m, abs(p - m));
		p /= w;
	}
	if(m == 0) print("YES");
	else print("NO");
}

int main(){
  cin.tie(0) -> sync_with_stdio(0);
	int T = 1;
	//cin >> T;
  while(T--){
		solve();
	}
	return 0;
}



