// Problem   In Danger
// Algorithm Dynamic Programming, Precalculation
// Runtime   O(n)
// Author    Walter Guttmann
// Date      06.05.2001

#include <cassert>
#include <fstream>
#include <iostream>
#include <map>

using namespace std;


int main ()
{
  map<int,int> joseph;
  for (int n=1, j=1 ; n<100000000 ; n++, j++, j%=n, j++)
    for (int q=n ; ; q/=10)
      if (q % 10)
      {
        if (q < 100)
          joseph[n] = j;
        break;
      }
  while (1)
  {
    double d;
    cin >> d;
    int n = (int) d;
    if (n == 0) break;
    assert (joseph.find(n) != joseph.end());
    cout << joseph[n] << endl;
  }
  return 0;
}

