//* Problem  : 1224 - HTML
//* Contest  : acm.tju.edu.cn
//* Origin   : University of Ulm Local Contest 1999
//* Type     : Solution
//* Date     : 2008.06.06
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 8.0

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <iostream>
using namespace std;
#define int64 long long
#define INF 1000000000
#define EPS 1E-8

#define ALL(a) a.begin(), a.end()
#define PB(a, v) a.push_back(v)
#define MP(a,b) make_pair(a, b)
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORO(i, obj) for (int i = 0; i < (int)(obj.size); i++)

int n, m, res;

int a[64], u[1<<17];

int cmp(int *aa, int *bb, int n)
{
//	for (int i = 0, k; i < n; i++)
//		if (k = aa[i] - bb[i]) return k;
	return 0;
}

void check()
{
//	for (int i = 0; i < n; i++)
//		if (cmp(a, &a[i], n) > 0) return;
	int v0 = -1;
	for (int i = 0; i < n; i++)
	{
		int v = 0, p = 1;
		for (int j = 0; j < n; j++)
		{
			v += a[i+j] * p; p *= m;
		}
		v0 = max(v0, v);
	}
	for (int i = 2*n - 1; i >= n; i--)
	{
		int v = 0, p = 1;
		for (int j = 0; j < n; j++)
		{
			v += a[i-j] * p; p *= m;
		}
		v0 = max(v0, v);
	}
	if (u[v0]) return;
	u[v0] = 1;
	/*for (int i = 0; i < n; i++)
		printf("%d ", a[i]);
	printf("\n");*/
	res++;
}

void recurse(int d)
{
	if (d == n) check();
	else
		for (int v = 0; v < m; v++)
		{
			a[d] = a[d + n] = v;
			recurse(d + 1);
		}
}

int solve()
{
	res = 0;
	memset(u, 0, sizeof(u));
	recurse(0);
	return res;
}

int main()
{
#ifdef _DEBUG
	freopen("1166.in", "r", stdin);
//	freopen("1166.out", "w", stdout);
#endif
	while (scanf("%d%d", &m, &n) == 2 && m)
		printf("%d\n", solve());
	return 0;
}
