def compute_left_pos(y0, delta, N): effective = y0 - delta if effective >= 0: return effective else: effective = -effective - 1 cycles = effective // N remainder = effective % N if cycles % 2 == 0: return remainder else: return (N - 1) - remainder def compute_right_pos(y0, delta, N): effective = y0 + delta cycles = effective // N remainder = effective % N if cycles % 2 == 0: return remainder else: return (N - 1) - remainder def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 Q = int(input[ptr]) ptr += 1 fish = [] for _ in range(Q): cmd = input[ptr] ptr += 1 t = int(input[ptr]) ptr += 1 y = int(input[ptr]) ptr += 1 z = int(input[ptr]) ptr += 1 if cmd == 'L' or cmd == 'R': fish.append((t, y, cmd, z)) elif cmd == 'C': a = y b = z current_t = t total = 0 for (t0, y0, direction, z_val) in fish: if t0 > current_t: continue delta = current_t - t0 if direction == 'L': pos = compute_left_pos(y0, delta, N) else: pos = compute_right_pos(y0, delta, N) if a <= pos < b: total += z_val print(total) if __name__ == '__main__': main()