#include<iostream>
#include<sstream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cassert>
using namespace std;

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define each(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(),c.end()
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

typedef vector<int> vi;
typedef pair<int,int> pi;
typedef long long ll;
const int inf=(int)1e9;
const double EPS = 1e-9, INF = 1e12, PI = acos(-1.0);
typedef complex<double> P;
namespace std{
	bool operator<(const P& a, const P& b){
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}
inline double cross(const P& a, const P& b){ return imag(conj(a)*b); }
inline 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);
	}
};
typedef vector<P> G;
inline int ccw(P a, P b, P c) {
	b -= a; c -= a;
	if(cross(b, c) > 0)   return +1;		// counter clockwise
	if(cross(b, c) < 0)   return -1;		// clockwise
	if(dot(b, c) < 0)     return +2;		// c--a--b on line
	if(norm(b) < norm(c)) return -2;		// a--b--c on line
	return 0;
}
G convex_hull(G ps) {
  int n = ps.size(), k = 0;
  sort(ps.begin(), ps.end());
  G ch(2*n);
  for (int i = 0; i < n; ch[k++] = ps[i++]) // lower-hull
    while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;
  for (int i = n-2, t = k+1; i >= 0; ch[k++] = ps[i--]) // upper-hull
    while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) <= 0) --k;
  ch.resize(k-1);
  return ch;
}
bool intersectSP(const L &s, const P &p) {
  return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}
P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}
double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}
#define curr(P, i) P[i]
#define next(P, i) P[(i+1)%P.size()]
enum { OUT, ON, IN };
int contains(const G& g, const P& p) {
  bool in = false;
  for (int i = 0; i < g.size(); ++i) {
    P a = curr(g,i) - p, b = next(g,i) - p;
    if (imag(a) > imag(b)) swap(a, b);
    if (imag(a) <= 0 && 0 < imag(b))
      if (cross(a, b) < 0) in = !in;
    if (cross(a, b) == 0 && dot(a, b) <= 0) return ON;
  }
  return in ? IN : OUT;
}
#include<stack>
void visit(const vector<vi> &g, int v, vector<vi>& scc,
    stack<int> &S, vector<bool> &inS,
    vector<int> &low, vector<int> &num, int& time) {
  low[v] = num[v] = ++time;
  S.push(v); inS[v] = true;
  each(e, g[v]) {
    int w = *e;
    if (num[w] == 0) {
      visit(g, w, scc, S, inS, low, num, time);
      low[v] = min(low[v], low[w]);
    } else if (inS[w])
      low[v] = min(low[v], num[w]);
  }
  if (low[v] == num[v]) {
    scc.push_back(vector<int>());
    while (1) {
      int w = S.top(); S.pop(); inS[w] = false;
      scc.back().push_back(w);
      if (v == w) break;
    }
  }
}
void stronglyConnectedComponents(const vector<vi>& g,
    vector<vi>& scc) {
  const int n = g.size();
  vector<int> num(n), low(n);
  stack<int> S;
  vector<bool> inS(n);
  int time = 0;
  rep(u, n) if (num[u] == 0)
    visit(g, u, scc, S, inS, low, num, time);
}

int n, d;
vector<vi> g;

int main(){
	while(cin >> n >> d, n || d){
		g.clear(); g.resize(n);
		vector<G> tri;
		rep(i, n){
			int t = -1;
			G p(3);
			double l[3];
			rep(j, 3) cin >> p[j].real() >> p[j].imag();
			rep(j, 3) l[j] = abs(p[j] - p[(j + 1) % 3]);
			rep(j, 3) if(abs(l[j] - l[(j + 1) % 3]) < EPS){
				t = (j + 1) % 3;
				break;
			}
			assert(t >= 0);
			if(t) swap(p[t], p[0]);
			tri.pb(p);
		}
		
		rep(i, n){
			G &p = tri[i];
			P dir = p[0] - (p[1] + p[2]) * 0.5;
			dir *= d / abs(dir);
			
			G ps;
			ps.pb(p[1]); ps.pb(p[2]);
			ps.pb(p[1] + dir); ps.pb(p[2] + dir);
			ps = convex_hull(ps);
			
			rep(j, n){
				bool ok = 0;
				rep(k, 3){
					rep(l, 4) if(distanceSP(L(ps[l], ps[(l + 1) % 4]), tri[j][k])
						< 0.01) ok = 1;
					if(contains(ps, tri[j][k]) != OUT) ok = 1;
				}
				if(ok) g[i].pb(j);
			}
		}
		
		int ans = 0;
		map<int, int> to;
		vector<vi> scc;
		stronglyConnectedComponents(g, scc);
		int m = scc.size();
		vi in(m);
		
		rep(i, m) rep(j, scc[i].size()){
			to[scc[i][j]] = i;
		}
		rep(i, n) rep(j, g[i].size()){
			int a = to[i], b = to[g[i][j]];
			if(a != b) in[b]++;
		}
		rep(i, m) if(in[i] == 0) ans++;
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 1316