Problem 92

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define SIZE 10000000
int f[SIZE]; //1に分類されるなら1 89に分類されるなら2 不明なら0

//1ならfalse 89ならtrueを返す
bool getf(int n){
  if(n==1)return false;
  if(n==89) return true;
  if(f[n]!=0) return f[n]-1;
  int next = 0;
  while(n){
    next += (n%10)*(n%10);
    n/=10;
  }
  return (f[n] = getf(next) + 1) - 1;
}
int main(){
  int ans = 0;
  for(int i = 2;i < SIZE; i++){
    ans += getf(i);
  }
  cout<<ans<<endl;
  return 0;
}