#include <bits/stdc++.h>

using namespace std;

int main() {
	int r,n;
	while(cin >> r >> n,r) {
		vector<int> hi(2*r);
		while(n--) {
			int xl,xr,h; cin >> xl >> xr >> h;
			for(int i=max(-r,xl);i<min(r,xr);++i) hi[i+r]=max(hi[i+r],h);
		}
		double ans=1e9;
		for(int i=-r;i<=r;++i) {
			double tmp=sqrt(r*r-i*i);
			if(i!=-r) ans=min(ans,hi[i+r-1]-tmp+r);
			if(i!=r) ans=min(ans,hi[i+r]-tmp+r);
		}
		printf("%.10f\n",ans);
	}
	
	
	return 0;
}
