#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <functional>
#include <set>
#include <queue>
#include <cctype>
#include <climits>
#include <stack>
#include <map>
  
using namespace std;

double r;
int n;

int main(){
	
	while(cin>>r>>n,r){

		double left,right,h;
		map<int,double> HL,HR;
		for(int i=0;i<n;i++){
			cin>>left>>right>>h;
			for(int x=left;x<=right;x++){
				if(x!=right) HL[x]=max(HL[x],h);
				if(x!=left)  HR[x]=max(HR[x],h);
			}	
		}
		
		double res=1e10;
		//[i,i+1]??????????????????
		for(int i=-r;i<=r-1;i++){
			double t1=r-sqrt(r*r-i*i)+HL[i];
			double t2=r-sqrt(r*r-(i+1)*(i+1))+HR[i+1];
			res=min(res,min(t1,t2));
		}
		printf("%.8f\n",res);
		
	}
	
	return 0;
}