#include <stdio.h>
#include <stdlib.h>
int clean_scanf(long* n, long* k, long* q)
{
int result = scanf("%ld %ld %ld", n, k, q);
int ch; while ((ch = getc(stdin)) != EOF && ch != '\n');
return result;
}
int F(int n, int k, int q)
{
int ans;
int* nums;
int i, j = 0;
if (n >= 0 && n < k) return 1;
nums = (int*)malloc(k * sizeof(int));
for (i = k - 1; i >= 0; i--) nums = 1;
for (i = 0; i < n; i++)
{
int sum = 0;
int x; for (x = 0; x < k; x++) sum += nums[x];
nums[j] = sum;
j = (j + 1) % k;
}
ans = nums[j];
free(nums);
return ans % q;
}
int main()
{
int caseNum = 0;
long n, k, q;
clean_scanf(&n, &k, &q);
while (n + k + q != 0)
{
int result = F(n, k, q);
printf("Case #%d: %ld\n", ++caseNum, result);
clean_scanf(&n, &k, &q);
}
printf("\n...");
getchar();
return 0;
}