#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int a, b, c;
  int ans = 0;
  vector<vector<int>> G(N, vector<int>(N, 1000000007));
  for (int i = 0; i < (N); i++) G[i][i] = 0;
  for (int i = 0; i < (M); i++) {
    cin >> a >> b >> c;
    a--;
    b--;
    G[a][b] = G[b][a] = c;
  }
  vector<vector<int>> H = G;
  for (int k = 0; k < (N); k++)
    for (int i = 0; i < (N); i++)
      for (int j = 0; j < (N); j++) G[i][j] = min(G[i][k] + G[k][j], G[i][j]);
  for (int i = 0; i < (N); i++)
    for (int j = 0; j < (N); j++)
      if (H[i][j] != 1e9 + 7 && G[i][j] != H[i][j]) ans++;
  for (int i = 0; i < (N); i++) {
    for (int j = 0; j < (N); j++) cout << G[i][j] << " ";
    cout << endl;
  }
  for (int i = 0; i < (N); i++) {
    for (int j = 0; j < (N); j++) cout << H[i][j] << " ";
    cout << endl;
  }
  cout << ans / 2 << endl;
  return 0;
}
