#include<stdio.h>
#include<string.h>
#include<math.h>
int ofs=21;

int B[50],R,N,r,l,h,i,j,P[150][2],p;
double ans;

void ins(int y,int x)
{
	P[p][0]=y;
	P[p][1]=x;
	p++;
}

double dis(int y,int x)
{
	double h=sqrt(1.0*R*R-x*x);
	if(h>=R)h=R-h+y;
	else h=2*R-h;
	return h;
}

int main()
{
	for(;scanf("%d%d",&R,&N),R;)
	{
		ans=10000000;
		memset(B,0,sizeof(B));
		for(i=0;i<N;i++)
		{
			scanf("%d%d%d",&r,&l,&h);
			for(j=r+ofs;j<l+ofs;j++)
				B[j]=B[j]>h?B[j]:h;
		}
		for(i=1,p=0;i<41;i++)
		{
			if(B[i]!=B[i-1])
			{
				ins(B[i-1],i-ofs);
				ins(B[i],i-ofs);
			}
		}
		ins(B[ofs],0);
		for(i=0;i<p;i++)
		{
			if(R>abs(P[i][1]))
			{
				double t=dis(P[i][0],P[i][1]);
				ans=ans<t?ans:t;
			}
		}
		printf("%lf\n",ans);
	}
	return 0;
}