w = {} defdictInit(): for i inrange(0, 10): w[str(i)] = i for i inrange(10, 36): w[chr(ord('a') + i - 10)] = i return w
defcal(ori, h): r = 0 for c in ori: r = r * h + w[c] return r
defbinarySearch(l, r, ori, tar): while l < r: mid = (l + r) >> 1 tmp = cal(ori, mid) if tmp < tar: l = mid + 1 else: r = mid return l if cal(ori, l) == tar else'Impossible'
defmain(): x, y, tag, r = map(str, input().split()) r = int(r) if tag != '1': x, y = y, x dictInit() value = 0 for c in x: value = value * r + w[c] minAns = 0 for c in y: minAns = max(minAns, w[c] + 1) print(binarySearch(minAns, int(1E18), y, value))