﻿namespace topCoder
{
	using System;
	using System.Collections.Generic;

	class ObtainingDigitK
	{
		public int minNumberToAdd(string originalNumber, int k)
		{
			for (int i = 0; i < originalNumber.Length; i++)
			{
				if (int.Parse(originalNumber[i] + "") == k)
				{
					return 0;
				}
			}

			int n = originalNumber.Length;
			for (int i = 1; i <= 9; i++)
			{
				int c = i;
				int j = n - 1;
				do
				{
					int d = int.Parse(originalNumber[j] + "");
					int r = d + c;
					if (r % 10 == k)
						return i;
					c = r / 10;
					j--;

					if (j < 0 && c == k && c != 0)
						return i;

				} while (c != 0 && j >= 0);
			}

			return -1;
		}
	}
}
