import java.util.*; public class magiccube_xiaowuc1 { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int q = sc.nextInt(); char[] ops = new char[q]; int[] theta = new int[q]; int[] slice = new int[q]; int qidx = 0; while(q-- > 0) { char c = sc.next().charAt(0); if(c == 'q') { int x = sc.nextInt()-1; int y = sc.nextInt()-1; int z = sc.nextInt()-1; for(int i = qidx-1; i >= 0; i--) { if(ops[i] == 'x') { if(x < slice[i]) continue; for(int qq = 0; qq < theta[i]; qq++) { int nz = y; int ny = n-1-z; y = ny; z = nz; } } else if(ops[i] == 'y') { if(y < slice[i]) continue; for(int qq = 0; qq < theta[i]; qq++) { int nx = z; int nz = n-1-x; x = nx; z = nz; } } else if(ops[i] == 'z') { if(z < slice[i]) continue; for(int qq = 0; qq < theta[i]; qq++) { int nx = n-1-y; int ny = x; x = nx; y = ny; } } else throw new RuntimeException(); } System.out.println(1+z*n*n+y*n+x); } else { ops[qidx] = c; theta[qidx] = (sc.nextInt() / 90)%4; slice[qidx] = sc.nextInt(); qidx++; } } } }