import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;

void main()
{
	
	while (1) {
		auto rn = readln.split.map!(to!int);
		auto r = rn[0];
		auto n = rn[1];
		if (r == 0 && n == 0) break;

		int hs[int];
		foreach (i; 0..n) {
			auto xh = readln.split.map!(to!int);
			foreach (j; xh[0]..xh[1]) {
				hs[j] = max(hs.get(j, 0), xh[2]);
			} 
		}

		auto res = double.max;
		foreach (i; -r..r) {
			res = min(res, hs.get(i, 0) - (r^^2 - i^^2).to!double.sqrt + r);
			res = min(res, hs.get(i, 0) - (r^^2 - (i+1)^^2).to!double.sqrt + r);
		}

		res.writeln;
	}
}