Problem: Vampire

Mr. C is a vampire. If he is exposed to the sunlight directly, he turns into ash. Nevertheless, last night, he attended to the meeting of Immortal and Corpse Programmers Circle, and he has to go home in the near dawn. Fortunately, there are many tall buildings around Mr. C's home, and while the sunlight is blocked by the buildings, he can move around safely. The top end of the sun has just reached the horizon now. In how many seconds does Mr. C have to go into his safe coffin?

To simplify the problem, we represent the eastern dawn sky as a 2-dimensional x-y plane, where the x axis is horizontal and the y axis is vertical, and approximate building silhouettes by rectangles and the sun by a circle on this plane.

The x axis represents the horizon. We denote the time by t, and the current time is t=0. The radius of the sun is r and its center is at (0, -r) when the time t=0. The sun moves on the x-y plane in a uniform linear motion at a constant velocity of (0, 1) per second.

The sunlight is blocked if and only if the entire region of the sun (including its edge) is included in the union of the silhouettes (including their edges) and the region below the horizon (y ≤ 0).

Write a program that computes the time of the last moment when the sunlight is blocked.

The following figure shows the layout of silhouettes and the position of the sun at the last moment when the sunlight is blocked, that corresponds to the first dataset of Sample Input below. As this figure indicates, there are possibilities that the last moment when the sunlight is blocked can be the time t=0.


<image>


The sunlight is blocked even when two silhouettes share parts of their edges. The following figure shows the layout of silhouettes and the position of the sun at the last moment when the sunlight is blocked, corresponding to the second dataset of Sample Input. In this dataset the radius of the sun is 2 and there are two silhouettes: the one with height 4 is in -2 ≤ x ≤ 0, and the other with height 3 is in 0 ≤ x ≤ 2.


<image>


Input

The input consists of multiple datasets. The first line of a dataset contains two integers r and n separated by a space. r is the radius of the sun and n is the number of silhouettes of the buildings. (1 ≤ r ≤ 20, 0 ≤ n ≤ 20)

Each of following n lines contains three integers xli, xri, hi (1 ≤ i ≤ n) separated by a space.

These three integers represent a silhouette rectangle of a building. The silhouette rectangle is parallel to the horizon, and its left and right edges are at x = xli and x = xri, its top edge is at y = hi, and its bottom edge is on the horizon. (-20 ≤ xli < xri ≤ 20, 0 < hi ≤ 20)

The end of the input is indicated by a line containing two zeros separated by a space.

Note that these silhouettes may overlap one another.

Output

For each dataset, output a line containing the number indicating the time t of the last moment when the sunlight is blocked. The value should not have an error greater than 0.001. No extra characters should appear in the output.

Sample Input


2 3
-2 -1 3
0 1 3
2 3 3
2 2
-2 0 4
0 2 3
2 6
-3 3 1
-2 3 2
-1 3 3
0 3 4
1 3 5
2 3 6
2 6
-3 3 1
-3 2 2
-3 1 3
-3 0 4
-3 -1 5
-3 -2 6
0 0


Output for the Sample Input


0.0000
3.0000
2.2679
2.2679






Example

Input

2 3
-2 -1 3
0 1 3
2 3 3
2 2
-2 0 4
0 2 3
2 6
-3 3 1
-2 3 2
-1 3 3
0 3 4
1 3 5
2 3 6
2 6
-3 3 1
-3 2 2
-3 1 3
-3 0 4
-3 -1 5
-3 -2 6
0 0


Output

0.0000
3.0000
2.2679
2.2679

Solution (in JAVA):

Solution:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	static int[][] map;
	static int[][] directions8= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
	static int[][] directions4= {{-1,0},{1,0},{0,-1},{0,1}};
	static double ans;

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while(true) {

			int r = sc.nextInt();
			int n = sc.nextInt();

			if((n|r)==0) {
				break;
			}

			//[x軸][y軸]
			boolean[][] plusArea = new boolean[20][20];
			boolean[][] minusArea = new boolean[20][20];

			for(int i = 0;i<n ; i++) {
				int startX = sc.nextInt();
				int endX = sc.nextInt();
				int height = sc.nextInt();

			if(startX >= 0) {
				for(int h = 0;h<height ; h++) {
					for(int j = startX; j<endX; j++) {
						plusArea[j][h] = true;
					}
				}
			}else if (endX <= 0) {
				startX *= -1;
				endX *= -1;
				for(int h = 0;h<height ; h++) {
					for(int j = endX; j< startX; j++) {
						minusArea[j][h] = true;
					}
				}
			}else {
				startX *= -1;
				for(int h = 0;h<height ; h++) {
					for(int j = startX-1; j>=0; j--) {
						minusArea[j][h] = true;
					}
					for(int j = 0; j<endX; j++) {
						plusArea[j][h] = true;
					}
				}
			}
			}

			//x軸、y軸。象限両用
			double dist[][] = new double[20][20];

			for(int i = 0;i<20 ; i++) {
			Arrays.fill(dist[i], Double.MAX_VALUE);
			}

			//全ての升について必要になる時間を出す
			for(int i = 0;i<r ; i++) {
				for(int j = 0;j<20 ; j++) {

					if(i == 0) {
						dist[i][j] = j;
						continue;
					}else if(j==0) {
						dist[i][j] = j;
					}

					if(j == 0) {
						dist[i][j] = r-Math.sqrt(r*r-i*i);
						continue;
					}
					dist[i][j] = dist[i][j-1]+1;
				}
			}
			ans = 20;
			for(int i = 0;i<r ; i++) {
				for(int j = 0;j<20 ; j++) {
					if(!plusArea[i][j] || !minusArea[i][j]) {
						if(ans > dist[i][j]) {
							ans = dist[i][j];
						}
					}
				}
			}

			System.out.println(ans);

		}


	}

	//BFS用に二つの配列を足し算する
	static int[] addArrayElms(int[] a, int[] b) {
		int[] c = new int[a.length];
		for(int i = 0; i < a.length; i++) {
			c[i] = a[i] + b[i];
		}
		return c;
	}

	//	//二分探索
	//k <= num となる最小の配列要素kのインデックスを返す
	static private int binarySearch(long num, long[] orderedArray){
		int lowerBorder = -1;
		int upperBorder = orderedArray.length;
		int mid;

		while(upperBorder - lowerBorder >1) {
			mid = (upperBorder + lowerBorder)/2;
			if(orderedArray[mid]<=num) {
				lowerBorder = mid;
			}else {
				upperBorder = mid;
			}
		}
		return lowerBorder;
	}

	//二分探索
	//k <= num となる最小のList要素kのインデックスを返す
	static private int binarySearch(long num, ArrayList<Long> orderedList){
		int lowerBorder = -1;
		int upperBorder = orderedList.size();
		int mid;

		while(upperBorder - lowerBorder >1) {
			mid = (upperBorder + lowerBorder)/2;
			if(orderedList.get(mid)<=num) {
				lowerBorder = mid;
			}else {
				upperBorder = mid;
			}
		}
		return lowerBorder;
	}

	//aとbの最小公倍数を求める
	public static int gcd(int a, int b) {
		return b == 0 ? a: gcd(b, a % b);
	}
	public static long gcd(long a, long b) {
		return b == 0 ? a: gcd(b, a % b);
	}
}