#include <bits/stdc++.h>
using namespace std;
const long long Max = 1010;
set<long long> adj[Max];
bool visited[Max] = {0};
vector<long long> nodesSeen;
long long sizes[Max];
pair<long long, long long> dfs(long long node, long long distance,
                               bool saveNodes) {
  visited[node] = true;
  if (saveNodes) {
    nodesSeen.push_back(node);
  }
  pair<long long, long long> farthest = {node, distance};
  for (long long to : adj[node]) {
    if (visited[to]) continue;
    pair<long long, long long> temp = dfs(to, distance + 1, saveNodes);
    if (temp.second > farthest.second) {
      farthest = temp;
    }
  }
  return farthest;
}
long long dfs2(long long node, long long par) {
  long long maxDepth = 0;
  for (long long to : adj[node]) {
    if (to == par) continue;
    maxDepth = max(maxDepth, 1 + dfs2(to, node));
  }
  return maxDepth;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long u, v;
  for (long long i = 0; i < m; ++i) {
    cin >> u >> v;
    adj[u].insert(v);
    adj[v].insert(u);
  }
  vector<long long> CCToBest(n + 1);
  vector<long long> nodeToCC(n + 1);
  long long ID = 0;
  for (long long i = 1; i <= n; ++i) {
    if (visited[i]) continue;
    nodesSeen.clear();
    dfs(i, 0, true);
    long long smallHeight = 1e8, small = -1;
    for (long long node : nodesSeen) {
      long long tempHeight = dfs2(node, node);
      if (tempHeight < smallHeight) {
        smallHeight = tempHeight;
        small = node;
      }
    }
    for (long long node : nodesSeen) {
      CCToBest[ID] = small;
      nodeToCC[node] = ID;
    }
    ID++;
  }
  long long shortestDiam = 1e8;
  vector<pair<long long, long long>> answerEdges;
  for (long long i = 1; i <= n; ++i) {
    vector<bool> added(ID, false);
    added[nodeToCC[i]] = true;
    vector<pair<long long, long long>> edges;
    for (long long j = 0; j < ID; ++j) {
      if (added[j]) continue;
      added[j] = true;
      edges.push_back({i, CCToBest[j]});
    }
    for (auto &p : edges) {
      u = p.first;
      v = p.second;
      adj[u].insert(v);
      adj[v].insert(u);
    }
    fill(visited, visited + n + 1, false);
    pair<long long, long long> farthest = dfs(1, 0, false);
    fill(visited, visited + n + 1, false);
    farthest = dfs(farthest.first, 0, false);
    if (farthest.second < shortestDiam) {
      shortestDiam = farthest.second;
      answerEdges = edges;
    }
    for (auto &p : edges) {
      u = p.first;
      v = p.second;
      adj[u].erase(v);
      adj[v].erase(u);
    }
  }
  cout << shortestDiam << '\n';
  for (auto &p : answerEdges) {
    u = p.first;
    v = p.second;
    cout << u << ' ' << v << '\n';
  }
  return 0;
}
