import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

class Node
{
	public final static boolean TRACE = false;
	
	public ArrayList<Node> neighbours = new ArrayList<Node>();
	public HashMap<Node,Integer> load = new HashMap<Node,Integer>();
	public double loc;
	
	public Node (double loc)
	{
		this.loc = loc;
	}
	
	public void connect (Node n)
	{
		if (n == this || neighbours.contains (n)) return;
		neighbours.add (n);
	}
	
	double distance (double targ)
	{
		if (loc > targ) return Math.min (loc - targ, targ - loc + 1.0);
		else return Math.min (targ - loc, loc - targ + 1.0);
	}
	
	void incrementLoad (Node n)
	{
		Integer i = load.get (n);
		if (i == null) load.put (n, 1);
		else load.put (n, i + 1);
	}
	
	public int forward (double target, int htl, HashSet<Node> visited)
	{
		if (loc == target) {
			if (TRACE) System.err.println (loc + " Y"); // Success
			return -1;
		}
		if (htl == 0) {
			if (TRACE) System.err.println (loc + " N"); // Failure
			return 0;
		}
		if (!visited.add (this)) {
			if (TRACE) System.err.print (loc + " L "); // Loop
			return htl; // Backtrack
		}
		HashSet<Node> candidates = new HashSet<Node> (neighbours);
		while (htl > 0) {
			if (TRACE) System.err.print (loc + " ");
			double bestDist = Double.POSITIVE_INFINITY;
			Node bestNode = null;
			for (Node n : candidates) {
				double d = n.distance (target);
				if (d < bestDist) {
					bestDist = d;
					bestNode = n;
				}
			}
			if (bestNode == null) {
				if (TRACE) System.err.print ("D "); // Dead-end
				return htl; // Backtrack
			}
			candidates.remove (bestNode);
			incrementLoad (bestNode);
			htl = bestNode.forward (target, htl - 1, visited);
		}
		return htl;
	}
	
	public void printLoad()
	{
		for (Node n : neighbours) {
			Integer i = load.get (n);
			if (i == null) i = 0;
			System.out.println (distance (n.loc) + " " + i);
		}
	}
}
