#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <complex>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define EPS 1e-8
#define DEB 0

typedef complex<double> P;

int n,d,cnt,num[128];
int edge[128][128],redge[128][128];
int visi[128],revisi[128];

class Triangle{
  public:
	vector<P> v,sikaku;
};

Triangle data[100];

double cross(P a, P b){
	return a.real()*b.imag() - a.imag()*b.real();
}
double dot(P a, P b){
	return a.real()*b.real() + a.imag()*b.imag();
}

int ccw(P p0, P p1, P p2){
	P a = p1 - p0;
	P b = p2 - p0;
	if( cross(a,b)>EPS ) return 1;  //left
	if( cross(a,b)<-EPS ) return -1;  //right
	if( dot(a,b)<-EPS ) return 2;
	if( abs(a) < abs(b) ) return -2;
	return 0;
}
bool isinside(vector<P>& v, vector<P>& s){
	rep(i,v.size()){
		rep(j,s.size()){
			if( ccw(s[j],s[(j+1)%4],v[i])!=-1 ) return false;
		}
	}
	return true;
}
bool isintersept(P a1, P a2, P b1, P b2){
	return ( ccw(a1,a2,b1)*ccw(a1,a2,b2)<=0 &&
			 ccw(b1,b2,a1)*ccw(b1,b2,a2)<=0 );
}

bool ok(vector<P>& v , vector<P>& s){
	rep(i,s.size()){
		rep(j,v.size()){
			if( isintersept(s[i],s[(i+1)%4],v[j],v[(j+1)%3]) ) return true;
		}
	}
	return isinside(v,s);
}

void dfs(int node){
	rep(i,n)if( !visi[i] && edge[node][i]>0 ){
		visi[i] = 1;
		dfs(i);
	}
	num[cnt++] = node;
	return;
}

void dfs_t(int node){
	rep(i,n)if( !revisi[i] && redge[node][i]>0 ){
		revisi[i] = 1;
		dfs_t(i);
	}
}


int main(){

	while(scanf("%d%d",&n,&d),n|d){
		cnt = 0;
		rep(i,128)visi[i]=revisi[i]=num[i]=0;
		rep(i,128)rep(j,128)edge[i][j]=redge[i][j]=0;
		rep(i,100){
			data[i].v.clear();
			data[i].sikaku.clear();
		}
			  
		rep(i,n){
			int x[3],y[3];
			scanf("%d%d%d%d%d%d",x,y,x+1,y+1,x+2,y+2);
			rep(j,3){
				int x1 = x[(j+1)%3] - x[j];
				int y1 = y[(j+1)%3] - y[j];
				int x2 = x[(j+2)%3] - x[(j+1)%3];
				int y2 = y[(j+2)%3] - y[(j+1)%3];
				if( x1*x1+y1*y1 == x2*x2+y2*y2 ){
#if DEB
					printf("j:%d\n",j);
#endif

					P p0 = P(x[j], y[j]);
					P p1 = P(x[(j+2)%3], y[(j+2)%3]);
					P p2 = P(x[(j+1)%3], y[(j+1)%3]);
					if( ccw(p0,p1,p2)==1 ){
						P tmp = (p1-p0)*P(0,1);
						tmp = tmp/abs(tmp) * P(d,0);
#if DEB
						printf("ccw==1\n");
						cout << tmp << endl;

						cout << p0+tmp << endl;
						cout << p1+tmp << endl;
#endif

						//p0,(p0+tmp),(p1+tmp),p1

						data[i].v.push_back(p0);
						data[i].v.push_back(p2);
						data[i].v.push_back(p1);
						data[i].sikaku.push_back(p0);
						data[i].sikaku.push_back(p0+tmp);
						data[i].sikaku.push_back(p1+tmp);
						data[i].sikaku.push_back(p1);

#if DEB
						rep(k,4){
							cout << ":: " << data[i].sikaku[k] << endl;
						}
#endif						
						
					}else if( ccw(p0,p1,p2)==-1 ){
						P tmp = (p0-p1)*P(0,1);
						tmp = tmp/abs(tmp) * P(d,0);
#if DEB
						puts("ccw==-1");
						cout << tmp << endl;
						cout << p0+tmp << endl;
						cout << p1+tmp << endl;
#endif
						//p1,(p1+tmp),(p0+tmp),p0

						data[i].v.push_back(p1);
						data[i].v.push_back(p2);
						data[i].v.push_back(p0);
						data[i].sikaku.push_back(p1);
						data[i].sikaku.push_back(p1+tmp);
						data[i].sikaku.push_back(p0+tmp);
						data[i].sikaku.push_back(p0);
						
					}else{
						puts("error"); exit(0);
					}

					break;
				}
			}
		}

		rep(i,n){
			rep(j,n)if( i!=j ){
				if( ok(data[j].v, data[i].sikaku) ){
					edge[i][j] = 1;
					redge[j][i] = 1;
				}
			}
		}

#if DEB
		puts("normal");
		rep(i,n){
			rep(j,n){
				printf("%d ",edge[i][j]);
			}
			puts("");
		}
		puts("\nreverse");
		rep(i,n){
			rep(j,n){
				printf("%d ",redge[i][j]);
			}
			puts("");
		}
#endif

		cnt = 0;
		rep(i,n)if( !visi[i] ){
			visi[i] = 1;
			dfs(i);
		}
#if DEB
		puts("num");
		rep(i,n){
			printf("num[%d]:%d\n",i,num[i]);
		}
#endif		
		int ans = 0;
		memset(visi,0,sizeof(visi));
		for(int i=cnt-1; i>=0; i--)if( !visi[num[i]] ){
			visi[num[i]] = 1;
			ans++;
			dfs(num[i]);
		}

		printf("%d\n",ans);
	}
	return 0;
}

Measured memory_bytes: 0