/* 
 * Solution by Christian Kauth - July 2010 - tested only against sample inputs
 * Build graph : - nodes : pairs<walked_distance,actual_busline>
 *				 - edges : connect nodes and have a weight, which is the fee of the next bus to step in
 * Run Dijkstra's shortest (cheapest) path algorithm on this graph
 */

#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;

#define INF 1000000000		// a finite value for infinity ;)
#define N_BUS 100			// maximum number of bus lines
#define N_COR 50			// maximum number of corners per bus line
#define N_WLK 300			// maximum number of blocks you may walk

struct Point
{
	long int x, y;
};

struct Bus
{
	Point corners[N_COR+1];
	long int f;
	int N;
};

int D, R;
Bus buses[N_BUS+2];
long int bbDist[N_BUS+2][N_BUS+2];
long int cost[N_WLK][N_BUS+2];

/*
 * read test case from standard input
 */
void read_case()
{
	cin>>D;

	// start point A
	buses[0].N=1; buses[0].f=0;
	cin>>buses[0].corners[0].x>>buses[0].corners[0].y;
	buses[0].corners[1] = buses[0].corners[0];

	// end point B
	buses[1].N=1; buses[1].f=0;
	cin>>buses[1].corners[0].x>>buses[1].corners[0].y;
	buses[1].corners[1] = buses[1].corners[0];

	// bus lines
	cin>>R;
	for (int i=2; i<R+2; i++)
	{
		cin>>buses[i].N>>buses[i].f;
		for (int j=0; j<buses[i].N; j++)
			cin>>buses[i].corners[j].x>>buses[i].corners[j].y;
		buses[i].corners[buses[i].N] = buses[i].corners[0];
	}
}

/*
 * Computes the Manhattan distance from point C to segment AB
 */
long int seg_point_dist(const Point &a, const Point &b, const Point &c)
{
	if (a.y==b.y)	// AB is a horizontal segment
		return abs(c.y-a.y) + min(abs(c.x-a.x),abs(c.x-b.x))*((a.x-c.x)*(b.x-c.x)>0);
	else			// AB is a vertical segment
		return abs(c.x-a.x) + min(abs(c.y-a.y),abs(c.y-b.y))*((a.y-c.y)*(b.y-c.y)>0);
}

/*
 * Computes the Manhattan distance from segment AB to segment CD
 */
long int seg_seg_dist(const Point &a, const Point &b, const Point &c, const Point &d)
{
	long int d1 = seg_point_dist(a,b,c);
	long int d2 = seg_point_dist(a,b,d);
	long int d3 = seg_point_dist(c,d,a);
	long int d4 = seg_point_dist(c,d,b);
	return ((d1+d2)*(d1+d2) == (c.x-d.x)*(c.x-d.x) + (c.y-d.y)*(c.y-d.y)) ? 0 : min(min(min(d1,d2),d3),d4);
}

/*
 * Computes the minimal Manhattan distance between two bus lines
 */
long int bus_bus_dist(const Bus &b1, const Bus &b2)
{
	long int dist(INF);
	for (int i=0; i<b1.N; i++)
		for (int j=0; j<b2.N; j++)
			dist = min(seg_seg_dist(b1.corners[i],b1.corners[i+1],b2.corners[j],b2.corners[j+1]), dist);
	return dist;
}

/*
 * Build the graph for the city on which we will work
 */
void build_graph()
{
	for (int i=0; i<R+2;i++)
		for (int j=0; j<R+2; j++)
			bbDist[i][j] = bus_bus_dist(buses[i],buses[j]);
}

/*
 * Computes the cost of the cheapest acceptable path using Dijkstra's algorithm
 */
long int cheapest_acceptable_path()
{
	int dd, bb, ddd, fff;
	priority_queue<int>  Q;
	int minVisited[N_BUS+2];
	
	for (int i=0; i<=D; i++)
		for (int j=0; j<R+2; j++)
			cost[i][j] = INF;
	for (int i=0; i<R+2; i++)
		minVisited[i] = INF;

	cost[0][0] = 0;
	Q.push(0);

	while (!Q.empty())
	{
		int next = Q.top(); Q.pop();
		dd = next / (N_BUS+2);
		bb = next % (N_BUS+2);
		if (bb==1) break;

		for (int bbb=1; bbb<R+2; bbb++)
		{
			ddd = dd+bbDist[bb][bbb];
			if (ddd <= D && minVisited[bbb]>ddd)
			{
				fff = cost[dd][bb]+buses[bbb].f;
				minVisited[bbb] = ddd;
				if (fff < cost[ddd][bbb])
				{
					cost[ddd][bbb] = fff;
					Q.push(ddd*(N_BUS+2)+bbb);
				}
			}
		}
		cost[dd][bb] *= -1;
	}

	long int cheapest(INF);
	for (int i=0; i<=D; i++)
		if (cost[i][1]<cheapest)
			cheapest = cost[i][1];
	return cheapest<INF ? cheapest : -1;
}

int main()
{
	read_case();
	build_graph();
	cout<<cheapest_acceptable_path()<<endl;
	return 0;
}

