#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
long long bs_radix(char *, int, long long);
int max_radix(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *, char *, int *, int *);
long long str_to_val(char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int main() {
  char s1[12], s2[12], temp[12];
  int tag, radix;
  scanf("%s%s%d%d", s1, s2, &tag, &radix);
  if (tag == 2) {
    strcpy(temp, s1);
    strcpy(s1, s2);
    strcpy(s2, temp);
  }
  long long val = str_to_val(s1, radix);
  if (val == -1) {
    printf("Impossible\n");
    return 0;
  }
  int is_find = 0;
  long long a[1000];
  for (int i = 2; i < 1000; ++i) {
    a[i] = str_to_val(s2, i);
    if (a[i] == -1)
      continue;
    if (a[i] == val) {
      is_find = 1;
      printf("%d\n", i);
      break;
    }
  }
  if (is_find)
    return 0;
  int len = strlen(s2);
  long long ans;
  if (len == 1)
    ans = bs_radix(s2, 80, val);
  else
    ans = bs_radix(s2, max_radix(len), val);
  if (ans == -1) {
    printf("Impossible\n");
  } else {
    printf("%lld\n", ans);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1fa0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    0xe6f(%rip),%rdi
lea    -0x2c(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1080 <scanf@plt>
cmpl   $0x2,-0x2c(%rbp)
jne    11cf <main+0x5f>
lea    -0x28(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x1c(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1060 <str_to_val@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0xffffffffffffffff,-0x38(%rbp)
jne    1204 <main+0x94>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    136f <main+0x1ff>
movl   $0x0,-0x3c(%rbp)
movl   $0x2,-0x1f84(%rbp)
cmpl   $0x3e8,-0x1f84(%rbp)
jge    12b3 <main+0x143>
lea    -0x1c(%rbp),%rdi
mov    -0x1f84(%rbp),%esi
call   1060 <str_to_val@plt>
mov    %rax,%rcx
movslq -0x1f84(%rbp),%rax
mov    %rcx,-0x1f80(%rbp,%rax,8)
ff
movslq -0x1f84(%rbp),%rax
cmpq   $0xffffffffffffffff,-0x1f80(%rbp,%rax,8)
ff ff
jne    1261 <main+0xf1>
jmp    129f <main+0x12f>
movslq -0x1f84(%rbp),%rax
mov    -0x1f80(%rbp,%rax,8),%rax
ff
cmp    -0x38(%rbp),%rax
jne    129a <main+0x12a>
movl   $0x1,-0x3c(%rbp)
mov    -0x1f84(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b3 <main+0x143>
jmp    129f <main+0x12f>
mov    -0x1f84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1f84(%rbp)
jmp    1215 <main+0xa5>
cmpl   $0x0,-0x3c(%rbp)
je     12c9 <main+0x159>
movl   $0x0,-0x4(%rbp)
jmp    136f <main+0x1ff>
lea    -0x1c(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x1f88(%rbp)
cmpl   $0x1,-0x1f88(%rbp)
jne    1303 <main+0x193>
lea    -0x1c(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    $0x50,%esi
call   1070 <bs_radix@plt>
mov    %rax,-0x1f90(%rbp)
jmp    1332 <main+0x1c2>
lea    -0x1c(%rbp),%rax
mov    %rax,-0x1f98(%rbp)
mov    -0x1f88(%rbp),%edi
call   1090 <max_radix@plt>
mov    -0x1f98(%rbp),%rdi
mov    %eax,%esi
mov    -0x38(%rbp),%rdx
call   1070 <bs_radix@plt>
mov    %rax,-0x1f90(%rbp)
cmpq   $0xffffffffffffffff,-0x1f90(%rbp)
ff
jne    1353 <main+0x1e3>
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1368 <main+0x1f8>
mov    -0x1f90(%rbp),%rsi
lea    0xcb8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1fa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
lea    0x18(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
lea    0x4(%rsp),%r8
call   1090 <scanf@plt>
cmpl   $0x2,0x8(%rsp)
jne    11df <main+0x5f>
lea    0x24(%rsp),%r14
lea    0x18(%rsp),%rbp
mov    %r14,%rdi
mov    %rbp,%rsi
call   1030 <strcpy@plt>
lea    0xc(%rsp),%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    0x4(%rsp),%esi
lea    0x18(%rsp),%rdi
call   1070 <str_to_val@plt>
cmp    $0xffffffffffffffff,%rax
je     1285 <main+0x105>
mov    %rax,%r14
mov    $0x2,%ebp
lea    0xc(%rsp),%rbx
jmp    121b <main+0x9b>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x3e8,%ebp
je     1242 <main+0xc2>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <str_to_val@plt>
cmp    $0xffffffffffffffff,%rax
je     1210 <main+0x90>
cmp    %r14,%rax
jne    1210 <main+0x90>
lea    0xdd2(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1291 <main+0x111>
lea    0xc(%rsp),%rdi
call   1050 <strlen@plt>
mov    $0x50,%esi
cmp    $0x1,%eax
je     125f <main+0xdf>
mov    %eax,%edi
call   10a0 <max_radix@plt>
mov    %eax,%esi
lea    0xc(%rsp),%rdi
mov    %r14,%rdx
call   1080 <bs_radix@plt>
cmp    $0xffffffffffffffff,%rax
je     1285 <main+0x105>
lea    0xd94(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1291 <main+0x111>
lea    0xd87(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"%s%s%d%d\00"
@.str.1 = [12 x i8] c"Impossible\0A\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8]
%3 = alloca [12 x i8]
%4 = alloca [12 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca [1000 x i64]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i64
store i32 0, i32* %1
%13 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%14 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%15 = call i32 @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14, i32* %5, i32* %6)
%16 = load i32, i32* %5
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %28
18: ; preds = %0
%19 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%21 = call i32 @strcpy(i8* %19, i8* %20)
%22 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%23 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%24 = call i32 @strcpy(i8* %22, i8* %23)
%25 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%26 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%27 = call i32 @strcpy(i8* %25, i8* %26)
br label %28
28: ; preds = %18, %0
%29 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%30 = load i32, i32* %6
%31 = call i64 @str_to_val(i8* %29, i32 %30)
store i64 %31, i64* %7
%32 = load i64, i64* %7
%33 = icmp eq i64 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %95
36: ; preds = %28
store i32 0, i32* %8
store i32 2, i32* %10
br label %37
37: ; preds = %64, %36
%38 = load i32, i32* %10
%39 = icmp slt i32 %38, 1000
br i1 %39, label %40, label %67
40: ; preds = %37
%41 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%42 = load i32, i32* %10
%43 = call i64 @str_to_val(i8* %41, i32 %42)
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [1000 x i64], [1000 x i64]* %9, i64 0, i64 %45
store i64 %43, i64* %46
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [1000 x i64], [1000 x i64]* %9, i64 0, i64 %48
%50 = load i64, i64* %49
%51 = icmp eq i64 %50, -1
br i1 %51, label %52, label %53
52: ; preds = %40
br label %64
53: ; preds = %40
%54 = load i32, i32* %10
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [1000 x i64], [1000 x i64]* %9, i64 0, i64 %55
%57 = load i64, i64* %56
%58 = load i64, i64* %7
%59 = icmp eq i64 %57, %58
br i1 %59, label %60, label %63
60: ; preds = %53
store i32 1, i32* %8
%61 = load i32, i32* %10
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %61)
br label %67
63: ; preds = %53
br label %64
64: ; preds = %63, %52
%65 = load i32, i32* %10
%66 = add nsw i32 %65, 1
store i32 %66, i32* %10
br label %37
67: ; preds = %60, %37
%68 = load i32, i32* %8
%69 = icmp ne i32 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %67
store i32 0, i32* %1
br label %95
71: ; preds = %67
%72 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%73 = call i32 @strlen(i8* %72)
store i32 %73, i32* %11
%74 = load i32, i32* %11
%75 = icmp eq i32 %74, 1
br i1 %75, label %76, label %80
76: ; preds = %71
%77 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%78 = load i64, i64* %7
%79 = call i64 @bs_radix(i8* %77, i32 80, i64 %78)
store i64 %79, i64* %12
br label %86
80: ; preds = %71
%81 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%82 = load i32, i32* %11
%83 = call i32 @max_radix(i32 %82)
%84 = load i64, i64* %7
%85 = call i64 @bs_radix(i8* %81, i32 %83, i64 %84)
store i64 %85, i64* %12
br label %86
86: ; preds = %80, %76
%87 = load i64, i64* %12
%88 = icmp eq i64 %87, -1
br i1 %88, label %89, label %91
89: ; preds = %86
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %94
91: ; preds = %86
%92 = load i64, i64* %12
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %92)
br label %94
94: ; preds = %91, %89
store i32 0, i32* %1
br label %95
95: ; preds = %94, %70, %34
%96 = load i32, i32* %1
ret i32 %96
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s%s%d%d\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [6 x i8] c"%lld\0A\00"
@str.4 = [11 x i8] c"Impossible\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = alloca [12 x i8]
%3 = alloca [12 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = call i32 @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %7, i32* %4, i32* %5)
%12 = load i32, i32* %4
%13 = icmp eq i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = call i32 @strcpy(i8* %8, i8* %6)
%16 = call i32 @strcpy(i8* %6, i8* %7)
%17 = call i32 @strcpy(i8* %7, i8* %8)
br label %18
18: ; preds = %14, %0
%19 = load i32, i32* %5
%20 = call i64 @str_to_val(i8* %6, i32 %19)
%21 = icmp eq i64 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
br label %48
24: ; preds = %18, %32
%25 = phi i32 [ %33, %32 ], [ 2, %18 ]
%26 = call i64 @str_to_val(i8* %7, i32 %25)
%27 = icmp ne i64 %26, -1
%28 = icmp eq i64 %26, %20
%29 = select i1 %27, i1 %28, i1 false
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %25)
br label %48
32: ; preds = %24
%33 = add nuw nsw i32 %25, 1
%34 = icmp eq i32 %33, 1000
br i1 %34, label %35, label %24
35: ; preds = %32
%36 = call i32 @strlen(i8* %7)
%37 = icmp eq i32 %36, 1
br i1 %37, label %40, label %38
38: ; preds = %35
%39 = call i32 @max_radix(i32 %36)
br label %40
40: ; preds = %35, %38
%41 = phi i32 [ %39, %38 ], [ 80, %35 ]
%42 = call i64 @bs_radix(i8* %7, i32 %41, i64 %20)
%43 = icmp eq i64 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
br label %48
46: ; preds = %40
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i64 %42)
br label %48
48: ; preds = %30, %46, %44, %22
ret i32 0
}