#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 /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int m, n, x, y, k;
  int dir;
  int i;
  int x_atual, y_atual;
  int minutos;
  scanf(" %d %d", &m, &n);
  scanf(" %d %d", &x, &y);
  scanf(" %d", &k);
  minutos = 0;
  x_atual = 0;
  y_atual = 0;
  for (i = 0; i < k; i++) {
    scanf(" %d", &dir);
    switch (dir) {
    case 1:
      y_atual++;
      break;
    case 2:
      y_atual--;
      break;
    case 3:
      x_atual++;
      break;
    case 4:
      x_atual--;
      break;
    }
    if (x_atual >= x - 1 && x_atual <= x + 1 && y_atual >= y - 1 &&
        y_atual <= y + 1)
      minutos++;
  }
  printf("%d\n", minutos);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeda(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
lea    0xec4(%rip),%rdi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
lea    0xeb1(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1263 <main+0x143>
lea    0xe77(%rip),%rdi
xor    %eax,%eax
lea    -0x1c(%rbp),%rsi
call   1040 <scanf@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x3,%eax
ja     1203 <main+0xe3>
mov    -0x38(%rbp),%rax
lea    0xe39(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1203 <main+0xe3>
mov    -0x28(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
jmp    1203 <main+0xe3>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1203 <main+0xe3>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1250 <main+0x130>
mov    -0x24(%rbp),%eax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jg     1250 <main+0x130>
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jl     1250 <main+0x130>
mov    -0x28(%rbp),%eax
mov    -0x14(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jg     1250 <main+0x130>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1255 <main+0x135>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1189 <main+0x69>
mov    -0x2c(%rbp),%esi
lea    0xdaa(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xedb(%rip),%r14
xor    %r13d,%r13d
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <scanf@plt>
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <scanf@plt>
lea    0xeac(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,(%rsp)
jle    1247 <main+0x127>
lea    0x4(%rsp),%r15
lea    0xe79(%rip),%r12
xor    %r13d,%r13d
xor    %ebp,%ebp
xor    %ebx,%ebx
xor    %r14d,%r14d
jmp    11ae <main+0x8e>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%r14d
cmp    (%rsp),%r14d
jge    1247 <main+0x127>
lea    0xe5e(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x4(%rsp),%eax
add    $0xffffffff,%eax
cmp    $0x3,%eax
ja     1210 <main+0xf0>
movslq (%r12,%rax,4),%rax
add    %r12,%rax
jmp    *%rax
add    $0x1,%ebp
jmp    1210 <main+0xf0>
nopl   0x0(%rax)
add    $0xffffffff,%ebp
jmp    1210 <main+0xf0>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%ebx
jmp    1210 <main+0xf0>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
cmp    %ecx,%ebx
jl     11a0 <main+0x80>
add    $0x1,%eax
cmp    %eax,%ebx
jg     11a0 <main+0x80>
mov    0x8(%rsp),%eax
lea    -0x1(%rax),%ecx
cmp    %ecx,%ebp
setge  %cl
add    $0x1,%eax
cmp    %eax,%ebp
setle  %al
and    %cl,%al
movzbl %al,%eax
add    %eax,%r13d
jmp    11a0 <main+0x80>
lea    0xdc9(%rip),%rdi
mov    %r13d,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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