1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;


public final class ple {
	public static InputStream INPUT_STREAM = System.in;
	public static PrintStream PRINT_STREAM = System.out;
	
	private final static Comparator<Tribe> comparatorByXY = new Comparator<Tribe>() {
		@Override
		public int compare(Tribe arg0, Tribe arg1) {
			int byFirst = comparatorByX.compare(arg0, arg1);
			if (byFirst == 0) {
				return comparatorByY.compare(arg0, arg1);
			}
			return byFirst;
		}
	};

	private final static Comparator<Tribe> comparatorByX = new Comparator<Tribe>() {
		@Override
		public int compare(Tribe arg0, Tribe arg1) {
			int byFirst = arg0.x1 - arg1.x1;
			if (byFirst == 0) {
				return arg0.x2 - arg1.x2;
			}
			return byFirst;
		}
	};

	private final static Comparator<Tribe> comparatorByY = new Comparator<Tribe>() {
		@Override
		public int compare(Tribe arg0, Tribe arg1) {
			int byFirst = arg0.y1 - arg1.y1;
			if (byFirst == 0) {
				return arg0.y2 - arg1.y2;
			}
			return byFirst;
		}
	};

	public final static class Tribe {

		public Tribe(int x1, int x2, int y1, int y2) {
			// this.id = id;
			this.x1 = x1;
			this.x2 = x2;
			this.y1 = y1;
			this.y2 = y2;
		}

		int id;
		int x1;
		int x2;
		int y1;
		int y2;

		int xNum;
		int yNum;

		@Override
		public String toString() {
			StringBuilder builder = new StringBuilder();
			builder.append("Tribe [id=").append(id).append(": ").append(x1).append(", ").append(x2).append(", ")
					.append(y1).append(", ").append(y2).append("]");
			return builder.toString();
		}

	}

	static int debug = 1;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(INPUT_STREAM));

		int tribesNumber = Integer.parseInt(br.readLine());

		Tribe[] tribes = new Tribe[tribesNumber];

		for (int t = 0; t < tribesNumber; t++) {

			StringTokenizer st = new StringTokenizer(br.readLine());

			int x1 = Integer.parseInt(st.nextToken());
			int x2 = Integer.parseInt(st.nextToken());
			int y1 = Integer.parseInt(st.nextToken());
			int y2 = Integer.parseInt(st.nextToken());

			Tribe tribe = new Tribe(x1, x2, y1, y2);

			tribes[t] = tribe;

		}

		solve(tribes);

	}

	
	
	private static void solve(Tribe[] tribesIN) {
		 finalTribes = new ArrayList<Tribe>();
		addNumData(tribesIN);

		solve();

	

		for (Tribe t : sortedByX) {
			if (t != null) {
				finalTribes.add(t);
			}
		}

		PRINT_STREAM.println(finalTribes.size());

		
		
		Collections.sort(finalTribes, comparatorByXY);
		
		for (Tribe t : finalTribes) {
			printTeritory(t);
		}

	}

	private static ArrayList<Tribe> finalTribes;
	public static Tribe[] sortedByX;
	public static Tribe[] sortedByY;

	private static void addNumData(Tribe[] tribes) {
		sortedByX = tribes;
		Arrays.sort(sortedByX, comparatorByX);

		sortedByY = new Tribe[tribes.length];

		System.arraycopy(sortedByX, 0, sortedByY, 0, tribes.length);
		Arrays.sort(sortedByY, comparatorByY);

		for (int i = 0; i < sortedByX.length; i++) {
			sortedByX[i].xNum = i;
		}

		for (int i = 0; i < sortedByY.length; i++) {
			sortedByY[i].yNum = i;
		}
	}

	private static void solve() {

		boolean anyUpdate = true;

		while (anyUpdate) {
			anyUpdate = false;
			for (int i = 0; i < sortedByX.length; ++i) {
				boolean grow = true;
				
				while(grow) {
					grow = false;
					
					if (sortedByX[i] != null) {
						Tribe tribe = sortedByX[i];
						Set<Integer> xNeig = getNeigX(i, tribe.x1, tribe.x2, sortedByX);
						Set<Integer> yNeig = getNeigY(tribe.yNum, tribe.y1, tribe.y2, sortedByY);

						if (xNeig.isEmpty() && yNeig.isEmpty()) {
							finalTribes.add(sortedByX[i] );						
							sortedByY[sortedByX[i].yNum] = null;
							sortedByX[i] = null;
						}
						
						Set<Integer> intersect = new TreeSet<Integer>(xNeig);
						intersect.retainAll(yNeig);

						if (!intersect.isEmpty()) {
							anyUpdate = true;
							grow = true;
							merge(i, intersect);
						}
					}
				}
				


			}
		}

	}

	private static void merge(int i, Set<Integer> intersect) {
		//System.out.println(String.format("Merge: %s  <_ %s", i, intersect));

		intersect.add(i);

		Tribe newTribe = createNewCountry(intersect);

		for (Integer in : intersect) {
			sortedByY[sortedByX[in].yNum] = null;
			sortedByX[in] = null;
		}

		sortedByX[newTribe.xNum] = newTribe;
		sortedByY[newTribe.yNum] = newTribe;

	}

	private static Set<Integer> getNeigX(int i, int min, int max, Tribe[] sortedByD) {
		Set<Integer> result = new HashSet<Integer>();

		int j = i - 1;
		while (j >= 0 ) {
			if (sortedByD[j] != null &&  sortedByD[j].x2 > min)
				result.add(j);
			j--;
		}

		j = i + 1;
		while (j < sortedByD.length && (sortedByD[j] == null || sortedByD[j].x1 < max)) {
			if (sortedByD[j] != null)
				result.add(j);
			j++;
		}

		return result;
	}

	private static Set<Integer> getNeigY(int i, int min, int max, Tribe[] sortedByD) {
		Set<Integer> result = new HashSet<Integer>();

		int j = i - 1;
		while (j >= 0 ) {
			if (sortedByD[j] != null && sortedByD[j].y2 > min)
				result.add(sortedByD[j].xNum);
			j--;
		}

		j = i + 1;
		while (j < sortedByD.length && (sortedByD[j] == null || sortedByD[j].y1 < max)) {
			if (sortedByD[j] != null)
				result.add(sortedByD[j].xNum);
			j++;
		}

		return result;
	}

	// / TESTED - print, create result


	private static Tribe createNewCountry(Set<Integer> set) {
		if (debug > 3)
			System.out.println("New Country composition: " + set);

		Iterator<Integer> iterator = set.iterator();
		Tribe firstTribe = sortedByX[iterator.next()];
		int minX = firstTribe.x1;
		int xNum = firstTribe.xNum;
		int maxX = firstTribe.x2;
		int minY = firstTribe.y1;
		int yNum = firstTribe.yNum;
		int maxY = firstTribe.y2;
		iterator.remove();

		for (Integer i : set) {
			if (sortedByX[i].x1 < minX) {
				minX = sortedByX[i].x1;
				xNum = sortedByX[i].xNum;
			}

			if (sortedByX[i].x2 > maxX) {
				maxX = sortedByX[i].x2;
			}

			if (sortedByX[i].y1 < minY) {
				minY = sortedByX[i].y1;
				yNum = sortedByX[i].yNum;
			}

			if (sortedByX[i].y2 > maxY) {
				maxY = sortedByX[i].y2;
			}

		}

		firstTribe.x1 = minX;
		firstTribe.x2 = maxX;
		firstTribe.y1 = minY;
		firstTribe.y2 = maxY;

		firstTribe.xNum = xNum;
		firstTribe.yNum = yNum;

		return firstTribe;
	}

	private static final String SEPARATOR = " ";

	private static void printTeritory(Tribe country) {

		StringBuilder builder = new StringBuilder();
		builder.append(country.x1).append(SEPARATOR).append(country.x2).append(SEPARATOR).append(country.y1)
				.append(SEPARATOR).append(country.y2);

		PRINT_STREAM.println(builder.toString());

	}

}