#include<cmath>
#include<stack>
#include<cstdio>
#include<vector>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const double EPS=1e-2;
const double PI=acos(-1);

template<class T>
struct point{
	T x,y;
	point &operator+=(const point &a){ x+=a.x; y+=a.y; }
	point &operator-=(const point &a){ x-=a.x; y-=a.y; }
	point operator+(const point &a)const{ return (point){x+a.x,y+a.y}; }
	point operator-(const point &a)const{ return (point){x-a.x,y-a.y}; }
};

template<class T>
point<T> operator*(T c,const point<T> &a){ return (point<T>){c*a.x,c*a.y}; }

template<class T>
point<double> operator/(const point<T> &a,double c){ return (point<double>){a.x/c,a.y/c}; }

template<class T>
T dot(const point<T> &a,const point<T> &b){ return a.x*b.x+a.y*b.y; }

template<class T>
T cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; }

template<class T>
double abs(const point<T> &a){ return sqrt(a.x*a.x+a.y*a.y); }

point<double> rot(const point<double> &a,double theta){
	return (point<double>){a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)};
}

template<class T>
struct segment{ point<T> a,b; };

template<class T>
struct polygon:vector< point<T> >{
	polygon(){}
	polygon(int n):vector< point<T> >(n){}
};

template<class T>
double dist(const point<T> &a,const point<T> &b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

enum {CCW=1,CW=-1,ON=0};
int ccw(const point<double> &a,const point<double> &b,const point<double> &c){
	double rdir=cross(b-a,c-a);
	if(rdir> EPS) return CCW;
	if(rdir<-EPS) return CW;
	return ON;
}

bool intersect(const segment<double> &S1,const segment<double> &S2){
	if(max(S1.a.x,S1.b.x)+EPS<min(S2.a.x,S2.b.x)
	|| max(S1.a.y,S1.b.y)+EPS<min(S2.a.y,S2.b.y)
	|| max(S2.a.x,S2.b.x)+EPS<min(S1.a.x,S1.b.x)
	|| max(S2.a.y,S2.b.y)+EPS<min(S1.a.y,S1.b.y)) return false;
	return ccw(S1.a,S1.b,S2.a)*ccw(S1.a,S1.b,S2.b)<=0
		&& ccw(S2.a,S2.b,S1.a)*ccw(S2.a,S2.b,S1.b)<=0;
}

bool cover(const polygon<double> &G,const point<double> &p){
	int n=G.size();
	bool res=false;
	rep(i,n){
		point<double> v1=G[i]-p,v2=G[(i+1)%n]-p;
		if(v1.y>v2.y) swap(v1,v2);
		if(v1.y<EPS && EPS<v2.y && cross(v1,v2)>EPS) res=!res;
		if(abs(cross(v1,v2))<EPS && dot(v1,v2)<EPS) return true;
	}
	return res;
}

bool cover(const polygon<double> &G,const polygon<double> &H){
	int m=G.size(),n=H.size();
	rep(j,n) if(!cover(G,H[j])) return false;
	return true;
}

bool intersect(const polygon<double> &G,const polygon<double> &H){
	int m=G.size(),n=H.size();
	rep(i,m) rep(j,n) if(intersect((segment<double>){G[i],G[(i+1)%m]},(segment<double>){H[j],H[(j+1)%n]})) return true;
	return cover(G,H) || cover(H,G);
}

const int V_MAX=100;

int ord[V_MAX],low[V_MAX];
void dfs(int u,int &c,stack<int> &S,bool *inS,const vector<int> *G,int &scc_id,int *scc){
	ord[u]=low[u]=c++;
	S.push(u);
	inS[u]=true;

	rep(i,G[u].size()){
		int v=G[u][i];
		if(ord[v]==-1){
			dfs(v,c,S,inS,G,scc_id,scc);
			low[u]=min(low[u],low[v]);
		}
		else if(inS[v]){
			low[u]=min(low[u],ord[v]);
		}
	}

	if(ord[u]==low[u]){
		int v;
		do{
			v=S.top(); S.pop();
			inS[v]=false;
			scc[v]=scc_id;
		}while(v!=u);
		scc_id++;
	}
}

int SCC(int n,const vector<int> *G,int *scc){
	static bool inS[V_MAX];
	rep(u,n){
		ord[u]=-1;
		inS[u]=false;
	}
	stack<int> S;
	int c=0,n_scc=0;
	rep(u,n) if(ord[u]==-1) dfs(u,c,S,inS,G,n_scc,scc);
	return n_scc;
}

int main(){
	int n;
	for(double d;scanf("%d%lf",&n,&d),n;){
		polygon<double> T[100];
		rep(i,n){
			T[i].resize(3);
			scanf("%lf%lf%lf%lf%lf%lf",&T[i][0].x,&T[i][0].y,&T[i][1].x,&T[i][1].y,&T[i][2].x,&T[i][2].y);
		}

		vector<int> G[100];
		rep(i,n){
			int k0,k1,k2; // T[i][k0]-T[i][k1] が底辺
			if     (abs(dist(T[i][0],T[i][2])-dist(T[i][1],T[i][2]))<EPS) k0=0, k1=1, k2=2;
			else if(abs(dist(T[i][1],T[i][0])-dist(T[i][2],T[i][0]))<EPS) k0=1, k1=2, k2=0;
			else                                                          k0=2, k1=0, k2=1;
			// k0-k1-k2 で CCW になるように
			if(ccw(T[i][k0],T[i][k1],T[i][k2])==CW) swap(k0,k1);

			polygon<double> R(4); // ビームを表す長方形
			R[0]=T[i][k0];
			R[1]=T[i][k1];
			R[2]=T[i][k1]+d*rot((T[i][k1]-T[i][k0])/abs(T[i][k1]-T[i][k0]), PI/2);
			R[3]=T[i][k0]+d*rot((T[i][k0]-T[i][k1])/abs(T[i][k0]-T[i][k1]),-PI/2);

			rep(j,n) if(i!=j && intersect(T[j],R)) G[i].push_back(j);
		}

		int scc[100];
		int n_scc=SCC(n,G,scc);

		bool src[100];
		rep(u,n_scc) src[u]=true;
		rep(i,n) rep(j,G[i].size()) if(scc[i]!=scc[G[i][j]]) src[scc[G[i][j]]]=false;
		printf("%d\n",count(src,src+n_scc,true));
	}

	return 0;
}

Measured memory_bytes: 1112