from collections import Counter total = 0 left = [] right = [] with open("input1.txt") as f: for line in f: line = line.rstrip().split() left.append(int(line[0])) right.append(int(line[-1])) left.sort() right.sort() for vl, vr in zip(left, right): total += max(vl, vr) - min(vl, vr) print("Total:", total) total = 0 counter = Counter(right) for vl in left: total += vl * counter[vl] print("Total:", total)