https://www.acmicpc.net/problem/2331
using System;
using System.Collections;
class Program
{
static int make(int a,int p){
int sum=0;
while(a>0){
sum+=(int)Math.Pow(a%10,p);
a/=10;
}
return sum;
}
static void Main() {
ArrayList arr=new ArrayList();
string[] s=Console.ReadLine().Split(' ');
int a=int.Parse(s[0]);
int p=int.Parse(s[1]);
arr.Add(a);
int num=a;
while(true){
num=make(num,p);
if(arr.Contains(num)){
int ans=arr.IndexOf(num);
Console.Write(ans);
break;
}
arr.Add(num);
}
}
}