#include <stdio.h>
#include <set>
#include <string>
#include <algorithm>

using namespace std;

FILE *in;
int c, l;

typedef set<string> sset;

int main()
{
  in = stdin;

  while(1)
    {
      fscanf(in, "%d %d ", &c, &l);
      if (c == 0) break;
      
      sset b;

      string x="";
      for(int i=0; i<l; i++) x += '0';
      while(1)
	{
	  string norm = x;
	  string norm1 = norm;
	  for(int i=0; i<l; i++)
	    {
	      norm1 = norm1[l-1] + norm1.erase(l-1);
	      if (norm1<norm) norm = norm1;
	      reverse(norm1.begin(), norm1.end());
	      if (norm1<norm) norm = norm1;
	      reverse(norm1.begin(), norm1.end());
	    }

	  b.insert(norm);
	  for(int i=0; i<l; i++)
	    {
	      x[i]++;
	      if (x[i] != '0'+c)
		{
		  break;
		}
	      x[i] = '0';
	      if (i==l-1) goto weiter;
	    }
	}
      ;weiter:

      printf("%d\n", b.size());
    }

  return 0;
}
