https://www.acmicpc.net/problem/2331
import java.util.*;
public class Main {
static int make(int a, int p){
int sum=0;
while(a>0){
sum+=Math.pow(a%10,p);
a/=10;
}
return sum;
}
public static void main(String args[]) {
ArrayList<Integer> arr=new ArrayList<>();
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int p=s.nextInt();
arr.add(a);
while(true){
int m=arr.get(arr.size()-1);
int num=make(m,p);
if(arr.contains(num)){
int ans=arr.indexOf(num);
System.out.print(ans);
break;
}
arr.add(num);
}
}
}