#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 */
int cost(int, int, int *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  while (1) {
    int n, m, c;
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    int heads[n], knights[m];
    for (int i = 0; i < n; i++)
      scanf("%d", heads + i);
    for (int i = 0; i < m; i++)
      scanf("%d", knights + i);
    if ((c = cost(n, m, heads, knights)) == -1)
      printf("Loowater is doomed!\n");
    else
      printf("%d\n", c);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    116e <main+0x3e>
cmpl   $0x0,-0xc(%rbp)
jne    116e <main+0x3e>
jmp    1280 <main+0x150>
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11f3 <main+0xc3>
mov    -0x40(%rbp),%rsi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11bc <main+0x8c>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1231 <main+0x101>
mov    -0x38(%rbp),%rsi
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11fa <main+0xca>
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%rdx
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <cost@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    1263 <main+0x133>
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1274 <main+0x144>
mov    -0x10(%rbp),%esi
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
jmp    113f <main+0xf>
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xea8(%rip),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
xor    %eax,%eax
call   1060 <scanf@plt>
mov    -0x30(%rbp),%eax
mov    -0x2c(%rbp),%ecx
or     %eax,%ecx
jne    1182 <main+0x42>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe7a(%rip),%r13
jmp    11c7 <main+0x87>
nopl   0x0(%rax,%rax,1)
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
lea    0xe52(%rip),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
xor    %eax,%eax
call   1060 <scanf@plt>
mov    -0x30(%rbp),%eax
mov    -0x2c(%rbp),%ecx
or     %eax,%ecx
je     1171 <main+0x31>
mov    %rsp,%rcx
mov    %eax,%eax
mov    %rsp,%r12
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    -0x2c(%rbp),%esi
mov    %rsp,%r15
lea    0xf(,%rsi,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    -0x30(%rbp),%edi
test   %edi,%edi
mov    %rcx,-0x38(%rbp)
jle    1231 <main+0xf1>
mov    %r12,%r14
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r13,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%rbx
movslq -0x30(%rbp),%rdi
add    $0x4,%r14
cmp    %rdi,%rbx
jl     1210 <main+0xd0>
mov    -0x2c(%rbp),%esi
test   %esi,%esi
jle    1261 <main+0x121>
mov    %r15,%r14
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%rbx
movslq -0x2c(%rbp),%rsi
add    $0x4,%r14
cmp    %rsi,%rbx
jl     1240 <main+0x100>
mov    -0x30(%rbp),%edi
mov    %r12,%rdx
mov    %r15,%rcx
call   1050 <cost@plt>
cmp    $0xffffffff,%eax
jne    1190 <main+0x50>
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
jmp    11a0 <main+0x60>

LLVM IR (-O0):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [21 x i8] c"Loowater is doomed!\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
br label %10
10: ; preds = %0, %61
%11 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %2, i32* %3)
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %14
br label %63
18: ; preds = %14, %10
%19 = load i32, i32* %2
%20 = zext i32 %19 to i64
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %5
%22 = alloca i32, i64 %20
store i64 %20, i64* %6
%23 = load i32, i32* %3
%24 = zext i32 %23 to i64
%25 = alloca i32, i64 %24
store i64 %24, i64* %7
store i32 0, i32* %8
br label %26
26: ; preds = %35, %18
%27 = load i32, i32* %8
%28 = load i32, i32* %2
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i32, i32* %22, i64 %32
%34 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %33)
br label %35
35: ; preds = %30
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %26
38: ; preds = %26
store i32 0, i32* %9
br label %39
39: ; preds = %48, %38
%40 = load i32, i32* %9
%41 = load i32, i32* %3
%42 = icmp slt i32 %40, %41
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i32, i32* %9
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %25, i64 %45
%47 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %46)
br label %48
48: ; preds = %43
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %39
51: ; preds = %39
%52 = load i32, i32* %2
%53 = load i32, i32* %3
%54 = call i32 @cost(i32 %52, i32 %53, i32* %22, i32* %25)
store i32 %54, i32* %4
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %61
58: ; preds = %51
%59 = load i32, i32* %4
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %59)
br label %61
61: ; preds = %58, %56
%62 = load i8*, i8** %5
br label %10
63: ; preds = %17
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"%d %d\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [20 x i8] c"Loowater is doomed!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%6 = load i32, i32* %1
%7 = icmp eq i32 %6, 0
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
%10 = select i1 %7, i1 %9, i1 false
br i1 %10, label %61, label %11
11: ; preds = %0, %54
%12 = phi i32 [ %56, %54 ], [ %6, %0 ]
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
%15 = alloca i32, i64 %13
%16 = load i32, i32* %2
%17 = zext i32 %16 to i64
%18 = alloca i32, i64 %17
%19 = load i32, i32* %1
%20 = icmp sgt i32 %19, 0
br i1 %20, label %27, label %23
21: ; preds = %27
%22 = load i32, i32* %2
br label %23
23: ; preds = %21, %11
%24 = phi i32 [ %32, %21 ], [ %19, %11 ]
%25 = phi i32 [ %22, %21 ], [ %16, %11 ]
%26 = icmp sgt i32 %25, 0
br i1 %26, label %42, label %37
27: ; preds = %11, %27
%28 = phi i64 [ %31, %27 ], [ 0, %11 ]
%29 = getelementptr inbounds i32, i32* %15, i64 %28
%30 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %29)
%31 = add nuw nsw i64 %28, 1
%32 = load i32, i32* %1
%33 = sext i32 %32 to i64
%34 = icmp slt i64 %31, %33
br i1 %34, label %27, label %21
35: ; preds = %42
%36 = load i32, i32* %1
br label %37
37: ; preds = %35, %23
%38 = phi i32 [ %24, %23 ], [ %36, %35 ]
%39 = phi i32 [ %25, %23 ], [ %47, %35 ]
%40 = call i32 @cost(i32 %38, i32 %39, i32* %15, i32* %18)
%41 = icmp eq i32 %40, -1
br i1 %41, label %50, label %52
42: ; preds = %23, %42
%43 = phi i64 [ %46, %42 ], [ 0, %23 ]
%44 = getelementptr inbounds i32, i32* %18, i64 %43
%45 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %44)
%46 = add nuw nsw i64 %43, 1
%47 = load i32, i32* %2
%48 = sext i32 %47 to i64
%49 = icmp slt i64 %46, %48
br i1 %49, label %42, label %35
50: ; preds = %37
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %54
52: ; preds = %37
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %54
54: ; preds = %50, %52
%55 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32* %1, i32* %2)
%56 = load i32, i32* %1
%57 = icmp eq i32 %56, 0
%58 = load i32, i32* %2
%59 = icmp eq i32 %58, 0
%60 = select i1 %57, i1 %59, i1 false
br i1 %60, label %61, label %11
61: ; preds = %54, %0
ret i32 0
}