def edit_distance(a, b): dp = [[0 for j in range(len(b) + 1)] for i in range(len(a) + 1)] for i in range(len(a) + 1): dp[i][0] = i for i in range(len(b) + 1): dp[0][i] = i for i in range(len(a)): for j in range(len(b)): dp[i + 1][j + 1] = min([dp[i + 1][j] + 1, dp[i][j] + (a[i] != b[j]), dp[i][j + 1] + 1]) return dp[len(a)][len(b)] n, m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) aa = [] for i in range(n): aa += [0] * a[i] if i != n - 1: aa += [1] bb = [] for i in range(m): bb += [0] * b[i] if i != m - 1: bb += [1] print(edit_distance(aa, bb))