#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
int n;
cin >> n;
vector<int> par(n, 0), sz(n, 1);
set<int> trees;
set<int> not_trees;
for(int i =0; i < n; i++){
par[i] = i;
trees.insert(i);
}
vector<vector<pair<int, int>>> c_edges(n, vector<pair<int, int>>());
auto find = [&](int x, auto && self) -> int {
if(par[x] == x)return x;
return par[x] = self(par[x], self);
};
auto merge = [&](int p, int q) -> void {
int x = find(p, find);
int y = find(q, find);
if(x == y){
c_edges[x].push_back(make_pair(p, q));
not_trees.insert(x);
if(trees.find(x) != trees.end())trees.erase(trees.find(x));
}else{
if(sz[x] < sz[y])swap(x, y);
par[y] = x;
sz[x] += sz[y];
for(auto edge: c_edges[y]){
c_edges[x].push_back(edge);
}
if(not_trees.find(y) != not_trees.end())not_trees.erase(not_trees.find(y));
if(trees.find(y) != trees.end())trees.erase(trees.find(y));
if(c_edges[x].size() == 0){
trees.insert(x);
if(not_trees.find(x) != not_trees.end())not_trees.erase(not_trees.find(x));
}else{
not_trees.insert(x);
if(trees.find(x) != trees.end())trees.erase(trees.find(x));
}
}
};
for(int i = 0; i < n - 1; i++){
int x, y;
cin >> x >> y;
x--;
y--;
merge(x, y);
}
vector<vector<int>> ans;
while(not_trees.size()){
int g = *(not_trees.begin());
int h = *(trees.begin());
auto [r,s] = c_edges[g].back();
c_edges[g].pop_back();
ans.push_back({r, s, r, h});
merge(g, h);
}
cout << ans.size() << "\n";
for(auto x: ans){
for(auto y: x){
cout << y + 1 << " ";
}
cout << "\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7Cglp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