package yukicoder; import java.util.HashMap; import java.util.Scanner; public class Q469 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); long p1 = 1_000_000 + 7; long m1 = 1_000_000 + 9; long[] pow = new long[n]; pow[0] = 1; for (int i = 1; i < n; ++i) { pow[i] = pow[i - 1] * p1 % m1; } long[] accum = new long[n]; accum[0] = pow[0]; for (int i = 1; i < n; ++i) { accum[i] = accum[i - 1] + pow[i]; } HashMap map = new HashMap<>(); map.put(0L, 0); long h1 = 0; for (int query = 0; query < q; ++query) { String s = sc.next(); if (s.equals("!")) { int l = sc.nextInt(); int r = sc.nextInt(); int k = sc.nextInt(); h1 += (accum[r - 1] - (l > 0 ? accum[l - 1] : 0)) * k; while (h1 < 0) h1 += m1; if (!map.containsKey(h1)) { map.put(h1, query + 1); } } else { System.out.println(map.get(h1)); } } } }