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

using ll = long long;

int MOD;

struct modnum {
private:
	int v;
public:

	modnum() : v(0) {}
	modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
	explicit operator int () const { return v; }
	friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
	friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }

	modnum& operator += (const modnum& o) {
		v += o.v;
		if (v >= MOD) v -= MOD;
		return *this;
	}
	modnum& operator -= (const modnum& o) {
		v -= o.v;
		if (v < 0) v += MOD;
		return *this;
	}
	modnum& operator *= (const modnum& o) {
		v = int(ll(v) * ll(o.v) % MOD);
		return *this;
	}

	friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
	friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
	friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
};

using num = modnum;


int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n;
	cin >> n >> MOD;
	num ans = 0;
	vector<int> small;
	vector<pair<int,int> > big;
	for(int a = 0; a < 2*n; a++){
		int minv = 0;
		int maxv = 2*n-1;
		while(a * a + minv * minv < n*n) minv += 1;
		while(a * a + maxv * maxv > 4*n*n) maxv -= 1;
		assert(minv <= maxv);
		if(minv == 0){
			small.push_back(maxv + 1);
		} else {
			big.push_back({minv, maxv+1});
		}
	}
	for(int k = 0; k <= (int)big.size(); k++){
		vector<num> dp(k+1, 0);
		dp[0] = 1;
		int cur = 0;
		int cur_offset = (int)small.size() + k;
		int big_offset = 2*n;
		for(pair<int,int> q : big){
			while(cur < (int)small.size() && small[cur] >= q.first){
				cur_offset -= 1;
				for(int a = 0; a <= k; a++){
					dp[a] *= (small[cur] - (cur_offset - a));
				}
				cur += 1;
			}
			big_offset -= 1;
			vector<num> newdp(k+1, 0);
			for(int i = 0; i <= k; i++){
				newdp[i] += dp[i] * (q.second - (big_offset + i));
				if(i < k){
					newdp[i+1] += dp[i] * (q.first - (cur_offset - (i+1)));
				}
			}
			dp = newdp;
		}
		assert(cur == (int)small.size());
		if(k & 1) dp[k] *= -1;
		ans += dp[k];
	}
	cout << (int)ans << '\n';
}