#include <bits/stdc++.h>
using namespace std;
long long int n, m, haa = 0, yo = 0;
map<long long int, long long int> vis, dist;
priority_queue<pair<long long int, long long int>,
               vector<pair<long long int, long long int> >,
               greater<pair<long long int, long long int> > >
    pq;
vector<long long int> ans;
void dijkstra() {
  while (!pq.empty()) {
    long long int up = pq.top().second;
    pq.pop();
    if (vis[up - 1] == 0) {
      vis[up - 1] = 1;
      haa++;
      dist[up - 1] = dist[up] + 1;
      yo += dist[up - 1];
      ans.push_back(up - 1);
      pq.push({dist[up - 1], up - 1});
      if (haa == m) {
        cout << yo << '\n';
        for (long long int i = 0; i <= m - 1; i++) {
          cout << ans[i] << " ";
        }
        return;
      }
    }
    if (vis[up + 1] == 0) {
      vis[up + 1] = 1;
      haa++;
      dist[up + 1] = dist[up] + 1;
      yo += dist[up + 1];
      ans.push_back(up + 1);
      pq.push({dist[up + 1], up + 1});
      if (haa == m) {
        cout << yo << '\n';
        for (long long int i = 0; i <= m - 1; i++) {
          cout << ans[i] << " ";
        }
        return;
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  vector<long long int> v(n);
  haa = 0;
  for (long long int i = 0; i <= n - 1; i++) {
    cin >> v[i];
    pq.push({0LL, v[i]});
    vis[v[i]] = 1;
    dist[v[i]] = 0;
  }
  dijkstra();
  return 0;
}
