#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string n1, n2;
long long get_num(char &c){
if(isdigit(c)) return c - '0';
else return c - 'a' + 10;
}
long long get_n(long long &r, string &n){
long long s = 0;
for (int i = 0; i < n.size(); i++) {
s = s * r + get_num(n[i]);
}
return s;
}
int main(){
long long tag, radix;
cin >> n1 >> n2 >> tag >> radix;
if(tag == 2) swap(n1, n2);
long long num1 = get_n(radix, n1);
long long left = 2, right = num1 + 1;
for (int i = 0; i < n2.size(); i++) {
left = max(left, get_num(n2[i])+1);
}
while (left <= right) {
long long mid = (left + right) / 2;
long long num2 = get_n(mid, n2);
if(num2 > num1 || num2 < 0){
right = mid - 1;
}else if(num1 == num2){
cout << mid << endl;
return 0;
}else{
left = mid + 1;
}
}
cout << "Impossible\n";
return 0;
}