#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <cassert>
#include <memory>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
using namespace std;

const double EPS = 0.01;

typedef complex<double> P,point;
typedef vector<P> G,polygon;
namespace std {bool operator < (const P& a, const P& b) {return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);} }
double cross(const P& a, const P& b) {return imag(conj(a)*b);}
double dot(const P& a, const P& b) {return real(conj(a)*b);}
struct L : public vector<P> {L(const P &a, const P &b) {push_back(a); push_back(b);} };
int ccw(P a, P b, P c) {
	b -= a; c -= a;
	if (cross(b, c) > 0)   return +1;
	if (cross(b, c) < 0)   return -1;
	if (dot(b, c) < 0) return +2;
	if (norm(b) < norm(c)) return -2;
	return 0;
}

bool intersectSS(const L &s, const L &t) {return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;}

#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]

 
double area(vector<P> p){
	double S = 0 ; 
	p.push_back(p[0]);
	/*　多角形の面積公式 (反時計回りの場合) */
	for(int i = 0 ; i < p.size()-1 ; i++){
		S += (p[i].real() - p[i+1].real()) * (p[i].imag()+p[i+1].imag());
	}
	S /= 2.0;
	return S;
}
bool contains(vector<P> &a,P b){
	//b = (-10000,-10000);
	double ans = 0;
	for(int i = 0 ; i < a.size() ; i++){
		vector<P> w;
		w.push_back(a[i]);
		w.push_back(a[(i+1)%a.size()]);
		w.push_back(b);
		ans += abs(area(w));
	}
	ans = abs(ans);
	//cout << a[0] << " " << a[1] << " " << a[2] << " " << a[3] << " " << b << " " << (fabs(abs(area(a)) - ans ) < EPS) << endl;
	//cout << ans << " " << abs(area(a)) << " " << a.size() << endl;
	return fabs(abs(area(a)) - ans ) < EPS;
}

/*
	幾何: 
		・凸多角形の点包含判定
		・凸多角形同士の線分交差判定
	グラフ:
		・強連結成分分解
		・入次数数える
*/

int n;


bool eq(double a,double b){
	return fabs(a-b) < EPS;
}

int wf[100][100]={};

int main(){
	double d;
	while(cin >> n >> d && n ){
		memset(wf,0,sizeof(wf));
		for(int i = 0 ; i < n ; i++) 
			wf[i][i] = 1;
		vector<G> g(n);
		vector<G> g2(n);
		for(int i = 0 ; i < n ; i++){
			double x1,x2,x3,y1,y2,y3;
			cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
			g[i].push_back(P(x1,y1));
			g[i].push_back(P(x2,y2));
			g[i].push_back(P(x3,y3));
			double A = abs( P(x2,y2) - P(x1,y1) );
			double B = abs( P(x2,y2) - P(x3,y3) );
			double C = abs( P(x3,y3) - P(x1,y1) );
			L l(P(-1,-1),P(-1,-1));
			if( eq(A,B) ) l = L( P(x3,y3) , P(x1,y1) );
			else if( eq(A,C) ) l = L( P(x2,y2) , P(x3,y3) );
			else if( eq(B,C) ) l = L( P(x2,y2) , P(x1,y1) );
			else while(1){}
			
			assert( !( eq(A,B) && eq(B,C) ) );
			P r = l[1] - l[0];
			r = P(-r.imag(),r.real());
			r /= abs(r);
			
			//cout << A << " " << B << " " << C << " " << l[0] << "-" << l[1] << " " << r << endl;
			for(int coef = -1 ; coef <= 1 ; coef += 2 ){
				vector<P> XX;
				XX.push_back(l[0]);
				XX.push_back(l[0] + coef * d * r );
				XX.push_back(l[1] + coef * d * r );			
				XX.push_back(l[1]);
				int ok = 1;
				
				for(int j = 0 ; j < 3 ; j++){
					if( !contains(XX,g[i][j]) ){
						ok = 0;
					}
				}
				if( ok ) {
					if( g2[i].size() ) assert(0);
					g2[i] = XX;
				}
			}
		}
		for(int i = 0 ; i < n ; i++){
			for(int j = 0 ; j < n ; j++){
				if( i != j ) {
					int ok = 0;
					for(int l = 0 ; l < 3 ; l++){
						if( contains(g2[i],g[j][l]) ){
							ok = 1;
						}
					}
					for(int k = 0 ; k < 4 ; k++){
						for(int l = k+1 ; l < 4 ; l++){
							for(int m = 0 ; m < 3 ; m++){
								for(int o = m+1 ; o < 3 ; o++){
									if( intersectSS(L(g2[i][k],g2[i][l]),L(g[j][m],g[j][o])) ){
										ok = 1;
										
									}
								}
							}
						}
					}
					if( ok ) {
						wf[i][j] = 1;
					}
				}
			}
		}
		for(int i = 0 ; i < n ; i++)
			for(int j = 0 ; j < n ; j++)
				for(int k = 0 ; k < n ; k++)
					wf[j][k] |= wf[j][i] & wf[i][k];
	
		
		int in[100] = {} , id[100] = {} , cur = 0;
		for(int i = 0 ; i < n ; i++)
			id[i] = -1;
		
		for(int i = 0 ; i < n ; i++){
			if( id[i] == -1 ){
				for(int j = 0 ; j < n ; j++){
					if( wf[i][j] & wf[j][i] ){
						id[j] = cur;
					}
				}
				cur++;
			}
		}
		for(int i = 0 ; i < n ; i++){
			for(int j = 0 ; j < n ; j++){
				if(wf[i][j] && id[i] != id[j]) in[id[j]]++;
			}
		}
		int ans = 0;
		for(int i = 0 ; i < cur ; i++){
			if( !in[i] ){
				ans++;
			}
		}
		cout << ans << endl;
	}
}

Measured memory_bytes: 1044