def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 M = int(input[ptr]) ptr += 1 post_x = [] destinations = [] for _ in range(M): x = int(input[ptr]) ptr += 1 d = int(input[ptr]) ptr += 1 post_x.append(x) for __ in range(d): y = int(input[ptr]) ptr += 1 destinations.append(y) if not post_x: p_min = p_max = 0 else: p_min = min(post_x) p_max = max(post_x) if not destinations: d_min = d_max = 0 else: d_min = min(destinations) d_max = max(destinations) # Calculate the four components part1 = p_max - p_min part2 = d_max - d_min part3 = max(0, p_max - d_max) part4 = max(0, d_min - p_min) total = part1 + part2 + part3 + part4 print(total) if __name__ == '__main__': main()