// alex lo import java.util.*; class queens { public static void main(String[] args) { Scanner inputScanner = new Scanner(System.in); while (inputScanner.hasNext()) { int boardDimension = inputScanner.nextInt(); if (boardDimension == 0) return; int nTestCases = inputScanner.nextInt(); int collisions = 0; List xCords = new ArrayList(); List yCords = new ArrayList(); List dUpCords = new ArrayList(); List dDownCords = new ArrayList(); for(int b = 0; b <= boardDimension; b++) { xCords.add(b, new MyInt()); yCords.add(b, new MyInt()); } for(int b = 0; b <= boardDimension * 2 - 1; b++) { dUpCords.add(b, new MyInt()); dDownCords.add(b, new MyInt()); // these go from -n+1 to n-1 including 0 // in dUp // (7, 1) ought to fall in the first one (-6 adjustment) // 1, 1 middle // (1, 7) ought to fall in last one (+6 adjustment) // x - y + n = index // in dDown // 1, 1, ought to be at beginning (1-1 = 0 // 7, 1 ought to be in middle 1 - 7 = -6 // 7, 7 ought to be at end // x + y - 1 = index } for(int c = 0; c < nTestCases; c++) { int numberQueens = inputScanner.nextInt(); int x = inputScanner.nextInt(); int y = inputScanner.nextInt(); int s = inputScanner.nextInt(); int t = inputScanner.nextInt(); //System.out.println(boardDimension + " " + k + " " + x + " " + y + " " + s + " " + t); List pairs = new ArrayList(); for (int q = 0; q < numberQueens; q++) { pairs.add(new Pair(x + q * s, y + q * t)); } for(Pair p : pairs) { int collisionsForThisPoint = 0; MyInt xResult = xCords.get(p._x); if(xResult.val > 0) collisionsForThisPoint++; xResult.val++; MyInt yResult = yCords.get(p._y); if(yResult.val > 0) collisionsForThisPoint++; yResult.val++; MyInt duResult = dUpCords.get(p._x - p._y + boardDimension); if(duResult.val > 0) collisionsForThisPoint++; duResult.val++; MyInt ddResult = dDownCords.get(p._x + p._y - 1); if(ddResult.val > 0) collisionsForThisPoint++; ddResult.val++; //System.out.println(p._x + " " + p._y + " Collisions for this point = " + collisionsForThisPoint); collisions += collisionsForThisPoint; } } System.out.println(collisions); } } } class MyInt { public int val = 0; } class Pair { public Pair( int x, int y) { _x = x; _y = y; } public int _x; public int _y; }