#include<complex>
#include<vector>
#include<iostream>
#include<stack>
#include<cmath>

enum CCW{FRONT=0x01,RIGHT=0x02,BACK=0x04,LEFT=0x08,OVER=0x10};
enum CIRCLE_RELATION{CIRCLE_SAME=0x01,CIRCLE_CONTAIN=0x02,
		     CIRCLE_NO_CROSS=0x04,CIRCLE_ONE_CROSS=0x08,
		     CIRCLE_ONE_INSIDE_CROSS=0x10,CIRCLE_TWO_CROSS=0x20};
#define sc second
#define fr first

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

using namespace std;

typedef double elem;
typedef complex<elem> point, vec;
typedef pair<point, point> line, hline, seg, pp;

const double infty = 1e40;
const double eps = 1e-8;
const double pi = acos(-1.0);
point base(0,0);

bool eq(elem a, elem b){ return abs(a-b) < eps; }
bool eqv(vec a, vec b){ return eq( abs(b-a),0); }

bool far(point a, point b){ return abs(b-a)>0; }
bool near(point a, point b){ return abs(b-a)<=0; }
elem dot(vec a, vec b){ return (a.real() * b.real() + a.imag() * b.imag() ); }
elem cross(vec a, vec b){ return ( a.real() * b.imag() - a.imag() * b.real() ); }

elem dist(point a, point b){ return abs(a-b); }
elem dist_l(line l, point x){ return abs(cross(l.sc-l.fr,x-l.fr)) / abs(l.sc-l.fr); }
elem dist_seg(seg s, point x)
{
  if( dot(s.sc-s.fr,x-s.fr)<0 ) return abs(x-s.fr);
  if( dot(s.fr-s.sc,x-s.sc)<0 ) return abs(x-s.sc);
  return dist_l(s,x);
}

vec uvec(vec a){ return a / abs(a); }
vec nmr(vec a){ return a * vec(0,-1); }
vec nml(vec a){ return a * vec(0,1); }
vec unmr(vec a){ return uvec( nmr(a) ); }
vec unml(vec a){ return uvec( nml(a) ); }

inline int ccw(const point &a, point b, point x){
  b -= a;
  x -= a;
  if( eq(cross(b,x),0.0) && dot(b,x) < 0 ) return BACK;
  if( eq(cross(b,x),0.0) && abs(b) < abs(x) ) return FRONT;
  if( eq(cross(b,x),0.0) ) return OVER;
  if( cross(b,x) > 0 ) return LEFT;
  if( cross(b,x) < 0 ) return RIGHT;
}

inline bool intersectedSS(const seg &a, const seg &b)
{
  int cwaf=ccw(a.fr,a.sc,b.fr);
  int cwbf=ccw(b.fr,b.sc,a.fr);
  int cwas=ccw(a.fr,a.sc,b.sc);
  int cwbs=ccw(b.fr,b.sc,a.sc);
  if( cwaf==OVER || cwas==OVER || cwbf==OVER || cwbs==OVER ) return true;
  return ( cwaf | cwas ) == (LEFT|RIGHT) && ( cwbf | cwbs ) == (LEFT|RIGHT);
}

/* ツ三ツ角ツ形 */
struct triangle{
  vector<point> p;
  triangle():p(3,point(0,0)){}
  triangle(point a,point b,point c){p.push_back(a);p.push_back(b);p.push_back(c);}
  bool inside(const point &t)const{
    int ccw01=ccw(p[0],p[1],t),ccw02=ccw(p[1],p[2],t),ccw03=ccw(p[2],p[0],t);
    return ccw01==ccw02&&ccw02==ccw03;
  }
  bool contain(const point &t)const{
    int ccw01=ccw(p[0],p[1],t),ccw02=ccw(p[1],p[2],t),ccw03=ccw(p[2],p[0],t);
    if((ccw01|ccw02|ccw03)&OVER)return true;
    else return ccw01==ccw02&&ccw02==ccw03;
  }
};

/* ツ凝ゥツ形 */
struct rectangle{
  vector<point> p;
  rectangle():p(4,point(0,0)){}
  rectangle(point a, point b, point c, point d){
    p.push_back(a);  p.push_back(b);  p.push_back(c);  p.push_back(d);
  }
  bool inside(const point &t)const{
    int ccw01=ccw(p[0],p[1],t),ccw02=ccw(p[1],p[2],t),ccw03=ccw(p[2],p[0],t);
    return ccw01==ccw02&&ccw02==ccw03;
  }
  bool contain(const point &t)const{
    int ccw01=ccw(p[0],p[1],t),ccw02=ccw(p[1],p[2],t),ccw03=ccw(p[2],p[3],t),ccw04=ccw(p[3],p[0],t);
    if((ccw01|ccw02|ccw03|ccw04)&OVER)return true;
    else return ccw01==ccw02&&ccw02==ccw03&&ccw03==ccw04;
  }
  bool rayReach(const triangle &t){ // t : traignle
    for(int i = 0; i < (int)t.p.size(); ++i){
      if( this->contain(t.p[i]) ) return true;
    }
    for(int i = 0; i < 4; ++i){
      seg s(p[i],p[(i+1)%4]);
      for(int j = 0; j < (int)t.p.size(); ++j){
	seg ss(t.p[j],t.p[(j+1)%t.p.size()]);
	if( intersectedSS( s, ss ) ) return true;
      }
    }
    return false;
  }
};

rectangle touch(triangle t, elem d)
{
  rectangle ret;
  elem da=abs(t.p[0]-t.p[1]);
  elem db=abs(t.p[1]-t.p[2]);
  elem dc=abs(t.p[2]-t.p[0]);
  
  if(eq(da,db)){
    vec v=d*uvec( t.p[1] - (t.p[2]+t.p[0])/2.0 );
    ret.p[0]=t.p[2]+v;
    ret.p[1]=t.p[2];
    ret.p[2]=t.p[0];
    ret.p[3]=t.p[0]+v;
  }else if(eq(da,dc)){
    vec v=d*uvec( t.p[0] - (t.p[1]+t.p[2])/2.0 );
    ret.p[0]=t.p[1]+v;
    ret.p[1]=t.p[1];
    ret.p[2]=t.p[2];
    ret.p[3]=t.p[2]+v;
  }else if(eq(db,dc)){
    vec v=d*uvec( t.p[2] - (t.p[0]+t.p[1])/2.0 );
    ret.p[0]=t.p[0]+v;
    ret.p[1]=t.p[0];
    ret.p[2]=t.p[1];
    ret.p[3]=t.p[1]+v;
  }
  return ret;
}

struct Node{
  vector<int> con;
};

typedef vector<Node> Graph;

void visit(const Graph &g, int v, vector< vector<int> > &scc,
	   stack<int> &S, vector<int> &inS,
	   vector<int> &low, vector<int> &num, int &time)
{
  low[v] = num[v] = ++time;
  S.push(v);
  inS[v]=1;
  for(int i = 0; i < (int)g[v].con.size(); ++i){
    int w=g[v].con[i];
    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(true){
      int w=S.top(); S.pop(); inS[w]=0;
      scc.back().push_back(w);
      if( v == w ) break;
    }
  }
}
void stronglyConnectedComponents(const Graph &g, vector< vector<int> > &scc)
{
  int n = g.size();
  vector<int> num(n),low(n);
  stack<int> S;
  vector<int> inS(n);
  int time=0;
  for(int i=0;i<n;++i){
    if(num[i]==0)
      visit(g,i,scc,S,inS,low,num,time);
  }
}

int solve(const vector<triangle> &vt, elem d)
{
  Graph G;

  for(int i = 0; i < (int)vt.size(); ++i){
    Node node;
    for(int j = 0; j < (int)vt.size(); ++j){
      if( i != j ){
	rectangle R = touch(vt[i],d);
	if( R.rayReach( vt[j] ) ){
	  node.con.push_back(j);
	}
      }else
	node.con.push_back(j);
    }
    G.push_back(node);
  }
  
  vector< vector<int> > scc;
  stronglyConnectedComponents( G, scc );

  int in[scc.size()];
  for(int i = 0; i < (int)scc.size(); ++i){
    in[i] = 0;
  }
  Graph DAG;
  for(int i = 0; i < (int)scc.size(); ++i){
    Node node;
    for(int ii = 0; ii < (int)scc[i].size(); ++ii){
      int from = scc[i][ii];
      for(int j = 0; j < (int)scc.size(); ++j){
	if(i!=j){
	  for(int jj = 0; jj < (int)scc[j].size(); ++jj){
	    int to = scc[j][jj];
	    for(int k = 0; k < (int)G[from].con.size(); ++k){
	      if( G[from].con[k] == to ){
		node.con.push_back(j);
		in[j]++;
		goto next;
	      }
	    }
	  }
	next:;
	}
      }
    }
    DAG.push_back(node);
  }

  int ret = 0;
  for(int i = 0; i < (int)DAG.size(); ++i){
    if( in[i] == 0 ) ++ret;
  }
  return ret;
}

int main(){
  while(true){
    int n;
    elem d;
    vector<triangle> vt;
    scanf("%d%lf", &n, &d);
    if(n==0)break;
    for(int i = 0; i < n; ++i){
      double x1,y1,x2,y2,x3,y3;
      scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3);
      point ta(x1,y1),tb(x2,y2),tc(x3,y3);
      vt.push_back(triangle(ta,tb,tc));
    }
    int res = solve( vt, d );
    printf("%d\n", res);
  }
  return 0;
}

Measured memory_bytes: 0