/* Chris Chen
   Legion of Learners
   Senior Division
   Contest #1 2019-2020
*/

#include 
#include 
#include 
#include 

using namespace std;

bool prime(unsigned long long number){
   for (int i = 2; i <= sqrt(number); i += 1){
      if ((number % i) == 0){
         return false;
      }
   }
   return true;
}

int prime_factors(unsigned long long number){
   int result = 0;
   for (int i = 2; i <= sqrt(number); i += 1){
      if ((number % i) == 0){
         if (prime(i)) result += 1;
         if (prime(number/i)) result += 1;
      }
   }
   if (result == 0) return 1;
   return result;
}

int at_position(unsigned long long number, int position){
   for (int i = 0; i < position-1; i += 1){
      number /= 10;
   }
   return number % 10;
}

string transform(unsigned long long number, int position){
   int i = 1;
   string result = "";
   int digit = at_position(number, position);
   // cout << "DIGIT " << digit << "\n";
   int factors = prime_factors(number);
   while (number != 0){
      // cout << result << " " << number << number%10 << "\n";
      if (i < position) result = to_string(abs((int)(number%10) - digit))+result;
      if (i > position) result = to_string(number%10 + digit)+result;
      if (i == position) result = to_string(factors)+result;
      i += 1;
      number /= 10;
   }
   return result;
}

int main(){
   unsigned long long number = 0;
   int position = 0;
   ifstream fin("testdata.txt");
   for (int i = 0; i < 5; i += 1){
      fin >> number >> position;
      cout << transform(number, position) << "\n";
   }
}