#include <stdio.h>

int main()
{
	int N, M;
	scanf("%d %d", &N, &M);
	if (M == 1) {
		if (N == 1) printf("1\n");
		else printf("0\n");
		return 0;
	} else if (M == 2) {
		if (N == 1) printf("2\n");
		else if (N == 2) printf("1\n");
		else printf("0\n");
		return 0;
	} else if (N == 1) {
		printf("%lld\n", (long long)(M - 2) * (M - 1) / 2 + 3);
		return 0;
	}
	
	int i, cur, prev, F[2], sum = 2, num[200001] = {1, 2}, flag = 0, tmp[200001] = {};
	for (i = 3, F[0] = 1, F[1] = 1, cur = 1, prev = 0; i <= M - 2; i++, cur ^= 1, prev ^= 1) {
		F[prev] += F[cur];
		if (F[prev] >= N) F[prev] -= N;
		if (sum == 0 && F[prev] == 0 && F[cur] == 1) {
			if (flag == 0) flag = i;
			else {
				flag = i - flag;
				break;
			}
		}
		sum += F[prev];
		if (sum >= N) sum -= N;
		num[sum]++;
		if (flag > 0) tmp[sum]++;
	}
	
	int j, k = (M - 1 - i) / flag;
	for (j = 0; j < N; j++) num[j] += k * tmp[j];
	i += k * flag;
	for (num[sum]++, i++, cur ^= 1, prev ^= 1; i <= M - 2; i++, cur ^= 1, prev ^= 1) {
		F[prev] += F[cur];
		if (F[prev] >= N) F[prev] -= N;
		sum += F[prev];
		if (sum >= N) sum -= N;
		num[sum]++;
	}
	
	long long ans = -1;
	for (i = 0; i < N; i++) ans += (long long)num[i] * (num[i] - 1) / 2;
	F[prev] += F[cur];
	if (F[prev] % N == 0) ans++;
	if ((F[prev] + F[cur]) % N == 0) ans += 2;
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}