1. 
/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS10
* Problem: 10722 - Super Lucky Numbers
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1663
*
* @author Evgeni Pavlidis
* @version 1.0, 06/19/2010
*
* Method : Dynamic prgoramming
* Status : Accepted
* Runtime: 1.588
*/

import java.io.*;
import java.util.*;
import java.math.*;

class Main {

public static void main(String...args)
{
Scanner sc = new Scanner(System.in);
int b,n;
BigInteger B, M, sum;

BigInteger unlucky[][] = new BigInteger[129][101];
BigInteger lucky[][] = new BigInteger[129][101];

// initialization
for(b = 4; b <= 128; b++)
{
unlucky[b][0] = BigInteger.ZERO;
unlucky[b][1] = BigInteger.ZERO;
unlucky[b][2] = BigInteger.ONE;

lucky[b][0] = BigInteger.ZERO;
lucky[b][1] = BigInteger.valueOf(b-1);
lucky[b][2] = BigInteger.valueOf(b).multiply(lucky[b][1]).subtract(BigInteger.ONE);
}


// precalc lucky numbers stepwise
for(b = 4; b <= 128; b++)
{
M = BigInteger.valueOf(b-1);
B = BigInteger.valueOf(b);
sum = unlucky[b][1];

for(n = 3; n <= 100; n++)
{
sum = sum.add(unlucky[b][n-2]);
unlucky[b][n] = M.multiply( unlucky[b][n-1].add(sum) ). add(B.pow(n-2)) .subtract(sum);
lucky[b][n] = B.pow(n-1).multiply(M) .subtract(unlucky[b][n]);
}
}


while(true)
{
b = sc.nextInt();
n = sc.nextInt();

if(b == 0)
break;

System.out.println(lucky[b][n]);
}
}
}